Filter SDK/avs2yuv

From Avisynth wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "avs2yuv reads a script and outputs raw video (YUV or RGB). It's a stripped down version of the famous avs2yuv. Here's avs2yuv.cpp: #include <stdio.h> #include <Windows.h> ...")
 
Line 13: Line 13:
 
  int __cdecl main(int argc, const char* argv[])
 
  int __cdecl main(int argc, const char* argv[])
 
  {
 
  {
const char* infile = NULL;
+
const char* infile = NULL;
const char* outfile = NULL;
+
const char* outfile = NULL;
FILE* out_fh;
+
FILE* out_fh;
 
 
 
 
if (!strcmp(argv[1], "-h")) {
+
if (!strcmp(argv[1], "-h")) {
fprintf(stderr, MY_VERSION "\n"
+
    fprintf(stderr, MY_VERSION "\n"
"Usage: avs2yuv in.avs out.raw\n"
+
            "Usage: avs2yuv in.avs out.raw\n");
);
+
    return 2;
return 2;
+
} else {
} else {
+
    infile = argv[1];
infile = argv[1];
+
    outfile = argv[2];
outfile = argv[2];
+
}
}
+
 
   
 
   
try {
+
try {
char* colorformat;
+
    char* colorformat;
IScriptEnvironment* (__stdcall *CreateEnv)(int);
+
    IScriptEnvironment* (__stdcall *CreateEnv)(int);
HMODULE avsdll = LoadLibrary("avisynth.dll");
+
    HMODULE avsdll = LoadLibrary("avisynth.dll");
if (!avsdll) {
+
    if (!avsdll) {
fprintf(stderr, "failed to load avisynth.dll\n");
+
      fprintf(stderr, "failed to load avisynth.dll\n");
return 2;
+
      return 2;
}
+
    }
 
   
 
   
CreateEnv = (IScriptEnvironment *(__stdcall *)(int))GetProcAddress(avsdll, "CreateScriptEnvironment");  
+
    CreateEnv = (IScriptEnvironment *(__stdcall *)(int))GetProcAddress(avsdll, "CreateScriptEnvironment");  
if (!CreateEnv) {
+
    if (!CreateEnv) {
fprintf(stderr, "failed to load CreateScriptEnvironment()\n");
+
      fprintf(stderr, "failed to load CreateScriptEnvironment()\n");
return 1;
+
      return 1;
}
+
    }
 
 
 
 
IScriptEnvironment* env = CreateEnv(AVISYNTH_INTERFACE_VERSION);
+
    IScriptEnvironment* env = CreateEnv(AVISYNTH_INTERFACE_VERSION);
env->CheckVersion(5);
+
    env->CheckVersion(5);
AVS_linkage = env->GetAVSLinkage();
+
    AVS_linkage = env->GetAVSLinkage();
AVSValue arg(infile);
+
    AVSValue arg(infile);
AVSValue res = env->Invoke("Import", AVSValue(&arg, 1));
+
    AVSValue res = env->Invoke("Import", AVSValue(&arg, 1));
if (!res.IsClip()) {
+
    if (!res.IsClip()) {
fprintf(stderr, "Error: '%s' didn't return a video clip.\n", infile);
+
      fprintf(stderr, "Error: '%s' didn't return a video clip.\n", infile);
return 1;
+
      return 1;
}
+
    }
PClip clip = res.AsClip();
+
VideoInfo vi = clip->GetVideoInfo();
+
    PClip clip = res.AsClip();
 +
    VideoInfo vi = clip->GetVideoInfo();
 
 
 
 
fprintf(stderr, " %s:\n", infile);
+
    fprintf(stderr, " %s:\n", infile);
fprintf(stderr, " %dx%d,\n", vi.width, vi.height);
+
    fprintf(stderr, " %dx%d,\n", vi.width, vi.height);
fprintf(stderr, " %d/%d fps,\n", vi.fps_numerator, vi.fps_denominator);
+
    fprintf(stderr, " %d/%d fps,\n", vi.fps_numerator, vi.fps_denominator);
fprintf(stderr, " %d frames,\n", vi.num_frames);
+
    fprintf(stderr, " %d frames,\n", vi.num_frames);
if (vi.IsYUV()) {
+
    if (vi.IsYUV()) {
colorformat = "YUV";
+
      colorformat = "YUV";
} else {
+
    } else {
colorformat = "RGB";
+
      colorformat = "RGB";
}
+
    }
fprintf(stderr, " %s color format", colorformat);
+
    fprintf(stderr, " %s color format", colorformat);
 
   
 
   
out_fh = fopen(outfile, "wb");
+
    out_fh = fopen(outfile, "wb");
if (!out_fh) {
+
    if (!out_fh) {
fprintf(stderr, "fopen(\"%s\") failed", outfile);
+
      fprintf(stderr, "fopen(\"%s\") failed", outfile);
return 1;
+
      return 1;
}
+
    }
 
   
 
   
for (int frm = 0; frm < vi.num_frames; ++frm) {
+
    for (int frm = 0; frm < vi.num_frames; ++frm) {
PVideoFrame f = clip->GetFrame(frm, env);
+
      PVideoFrame f = clip->GetFrame(frm, env);
+
 
static const int planes[] = {PLANAR_Y, PLANAR_U, PLANAR_V};
+
      static const int planes[] = {PLANAR_Y, PLANAR_U, PLANAR_V};
  int wrote = 0;
+
      int wrote = 0;
 
   
 
   
for (int p=0; p<3; p++) { // for interleaved formats only the first plane (being the whole frame) is written
+
      for (int p=0; p<3; p++) { // for interleaved formats only the first plane (being the whole frame) is written
int w = vi.width  >> vi.GetPlaneWidthSubsampling(planes[p]);
+
          int w = vi.width  >> vi.GetPlaneWidthSubsampling(planes[p]);
int h = vi.height >> vi.GetPlaneHeightSubsampling(planes[p]);
+
          int h = vi.height >> vi.GetPlaneHeightSubsampling(planes[p]);
int pitch = f->GetPitch(planes[p]);
+
          int pitch = f->GetPitch(planes[p]);
const BYTE* data = f->GetReadPtr(planes[p]);
+
          const BYTE* data = f->GetReadPtr(planes[p]);
for (int y=0; y<h; y++) {
+
          for (int y=0; y<h; y++) {
wrote += fwrite(data, 1, w, out_fh);
+
            wrote += fwrite(data, 1, w, out_fh);
data += pitch;
+
            data += pitch;
}
+
          }
}
+
      }
}
+
    }
 
   
 
   
env->DeleteScriptEnvironment();
+
    env->DeleteScriptEnvironment();
FreeLibrary(avsdll);
+
    FreeLibrary(avsdll);
 
   
 
   
} catch(AvisynthError err) {
+
} catch(AvisynthError err) {
fprintf(stderr, "\nAvisynth error:\n%s\n", err.msg);
+
    fprintf(stderr, "\nAvisynth error:\n%s\n", err.msg);
return 1;
+
    return 1;
}
+
}
 
   
 
   
AVS_linkage = 0;
+
AVS_linkage = 0;
fclose(out_fh);
+
fclose(out_fh);
return 0;
+
return 0;
 
  }
 
  }

Revision as of 18:58, 2 January 2014

avs2yuv reads a script and outputs raw video (YUV or RGB). It's a stripped down version of the famous avs2yuv.

Here's avs2yuv.cpp:

#include <stdio.h>
#include <Windows.h>
#include "avisynth.h"

#define MY_VERSION "Avs2YUV 0.24"

const AVS_Linkage *AVS_linkage = 0;

int __cdecl main(int argc, const char* argv[])
{
const char* infile = NULL;
const char* outfile = NULL;
FILE* out_fh;
	
if (!strcmp(argv[1], "-h")) {
   fprintf(stderr, MY_VERSION "\n"
           "Usage: avs2yuv in.avs out.raw\n");
   return 2;
} else {
   infile = argv[1];
   outfile = argv[2];
}

try {
   char* colorformat;
   IScriptEnvironment* (__stdcall *CreateEnv)(int);
   HMODULE avsdll = LoadLibrary("avisynth.dll");
   if (!avsdll) {
      fprintf(stderr, "failed to load avisynth.dll\n");
      return 2;
   }

   CreateEnv = (IScriptEnvironment *(__stdcall *)(int))GetProcAddress(avsdll, "CreateScriptEnvironment"); 
   if (!CreateEnv) {
      fprintf(stderr, "failed to load CreateScriptEnvironment()\n");
      return 1;
   }
	
   IScriptEnvironment* env = CreateEnv(AVISYNTH_INTERFACE_VERSION);
   env->CheckVersion(5);
   AVS_linkage = env->GetAVSLinkage();
   AVSValue arg(infile);
   AVSValue res = env->Invoke("Import", AVSValue(&arg, 1));
   if (!res.IsClip()) {
      fprintf(stderr, "Error: '%s' didn't return a video clip.\n", infile);
      return 1;
   }

   PClip clip = res.AsClip();
   VideoInfo vi = clip->GetVideoInfo();
	
   fprintf(stderr, " %s:\n", infile);
   fprintf(stderr, " %dx%d,\n", vi.width, vi.height);
   fprintf(stderr, " %d/%d fps,\n", vi.fps_numerator, vi.fps_denominator);
   fprintf(stderr, " %d frames,\n", vi.num_frames);
   if (vi.IsYUV()) {
      colorformat = "YUV";
   } else {
      colorformat = "RGB";
   }
   fprintf(stderr, " %s color format", colorformat);

   out_fh = fopen(outfile, "wb");
   if (!out_fh) {
      fprintf(stderr, "fopen(\"%s\") failed", outfile);
      return 1;
   }

   for (int frm = 0; frm < vi.num_frames; ++frm) {
      PVideoFrame f = clip->GetFrame(frm, env);
 	
      static const int planes[] = {PLANAR_Y, PLANAR_U, PLANAR_V};
      int wrote = 0;

      for (int p=0; p<3; p++) { // for interleaved formats only the first plane (being the whole frame) is written
         int w = vi.width  >> vi.GetPlaneWidthSubsampling(planes[p]);
         int h = vi.height >> vi.GetPlaneHeightSubsampling(planes[p]);
         int pitch = f->GetPitch(planes[p]);
         const BYTE* data = f->GetReadPtr(planes[p]);
         for (int y=0; y<h; y++) {
            wrote += fwrite(data, 1, w, out_fh);
            data += pitch;
         }
      }
   }

   env->DeleteScriptEnvironment();
   FreeLibrary(avsdll);

} catch(AvisynthError err) {
   fprintf(stderr, "\nAvisynth error:\n%s\n", err.msg);
   return 1;
}

AVS_linkage = 0;
fclose(out_fh);
return 0;
}
Personal tools