├── src ├── resource.h ├── avs2pipemod.rc ├── avs2pipemod.manifest ├── utils.h ├── avs2pipemod.h ├── wave.cpp ├── wave.h ├── getopt.h ├── main.cpp ├── utils.cpp ├── avs2pipemod.cpp └── getopt.c ├── .gitignore ├── README.txt ├── readme_original.txt ├── vs2022 └── avs2pipemod.vcxproj └── gpl-3.0.txt /src/resource.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chikuzen/avs2pipemod/HEAD/src/resource.h -------------------------------------------------------------------------------- /src/avs2pipemod.rc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chikuzen/avs2pipemod/HEAD/src/avs2pipemod.rc -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.exe 2 | *.y4m 3 | *.bgr 4 | *.bgra 5 | *.yuv 6 | *.raw 7 | *.wav 8 | *.pcm 9 | 10 | *.log 11 | 12 | *.avs 13 | *.avsi 14 | *.avi 15 | *.flv 16 | *.mp4 17 | *.mov 18 | *.nut 19 | *.3gp 20 | *.3g2 21 | *.mkv 22 | *.264 23 | *.h264 24 | 25 | *.zip 26 | *.lzh 27 | *.7z 28 | *.rar 29 | *.gz 30 | *.bz2 31 | *.xz 32 | *.lzma 33 | 34 | *.diff 35 | *.patch 36 | 37 | *.db 38 | *.opendb 39 | *.sln 40 | *.filters 41 | *.user 42 | *.aps 43 | 44 | /archives/ 45 | 46 | /vs2022/.vs/ 47 | /vs2022/avs2pipemod/ 48 | /vs2022/x64/ 49 | -------------------------------------------------------------------------------- /src/avs2pipemod.manifest: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | true 7 | UTF-8 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /README.txt: -------------------------------------------------------------------------------- 1 | avs2pipemod is a modified version of avs2pipe basically only for my purpose. 2 | Thanks to Chris Beswick who releases avs2pipe under GPL. 3 | 4 | source code : https://github.com/chikuzen/avs2pipemod 5 | 6 | Differences with original 7 | * Display total elapsed time. 8 | * contents of 'info' 9 | * New option 'benchmark'. 10 | * New option 'trim'. 11 | * New option 'dumptxt' 12 | * New option 'filters' 13 | * New option 'dumpprops' 14 | * 'y4mp', 'y4mt', 'y4mb' and 'rawvideo' options instead of 'video'. 15 | * FieldBased input will be corrected to framebased on yuv4mpeg2 output modes. 16 | * Colorspace conversion that takes colormatrix and interlace into consideration. 17 | * 'wav', 'extwav' and 'rawaudio' option instead of 'audio'. 18 | * Convert bit depth function in audio output mode. 19 | 20 | 21 | Usage 22 | Execute avs2pipemod.exe without any options. 23 | Then help will be displayed. 24 | 25 | Requirement 26 | - Avisynth2.6.0final / Avisynth+ 3.7.5 or later. 27 | - Windows 7 sp1 or later. 28 | - Microsoft Visual C++ Redistributable v14. 29 | 30 | 31 | written by Oka Motofumi -------------------------------------------------------------------------------- /src/utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Oka Motofumi 3 | * 4 | * This file is part of avs2pipemod. 5 | * 6 | * avs2pipemod is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * avs2pipemod is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with avs2pipemod. If not, see . 18 | * 19 | */ 20 | 21 | 22 | #ifndef A2PM_UTILS_H 23 | #define A2PM_UTILS_H 24 | 25 | #include 26 | #include 27 | 28 | template 29 | static inline void validate(bool cond, T msg) 30 | { 31 | if (cond) { 32 | throw std::runtime_error(msg); 33 | } 34 | } 35 | 36 | 37 | enum { 38 | LOG_INFO, 39 | LOG_REPEAT, 40 | LOG_WARNING, 41 | }; 42 | 43 | void a2pm_log(int level, const char* message, ...); 44 | 45 | const char* get_string_info(int pix_type); 46 | 47 | const char* get_string_video_out(int pix_type); 48 | 49 | const char* get_string_y4mheader(int pix_type, int chromaloc); 50 | 51 | int get_sample_bits(int pixel_type); 52 | 53 | int get_num_planes(int pixel_type); 54 | 55 | void convert_channelmask_to_string(uint32_t cm, std::string& cmstr); 56 | 57 | //const char* get_string_filter(int pix_type); 58 | 59 | class Buffer { 60 | void* buff; 61 | public: 62 | Buffer(size_t size, size_t align = 16); 63 | ~Buffer(); 64 | void* data(); 65 | }; 66 | 67 | #endif 68 | 69 | -------------------------------------------------------------------------------- /src/avs2pipemod.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012-2016 Oka Motofumi 3 | * 4 | * This file is part of avs2pipemod. 5 | * 6 | * avs2pipemod is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * avs2pipemod is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with avs2pipemod. If not, see . 18 | * 19 | */ 20 | 21 | #ifndef AVS2PIPEMOD_H 22 | #define AVS2PIPEMOD_H 23 | 24 | #if defined (_WIN32) 25 | #define WIN32_LEAN_AND_MEAN 26 | #define NOMINMAX 27 | #define NOGDI 28 | #include 29 | #include 30 | #else 31 | #include 32 | #endif 33 | 34 | #define A2PM_VERSION "1.3.1" 35 | 36 | enum action_t { 37 | A2PM_ACT_NOTHING = 0, 38 | A2PM_ACT_INFO, 39 | A2PM_ACT_BENCHMARK, 40 | A2PM_ACT_AUDIO, 41 | A2PM_ACT_VIDEO, 42 | A2PM_ACT_DUMP_PIXEL_VALUES_AS_TXT, 43 | A2PM_ACT_DUMP_FRAME_PROPERTIES_AS_JSON, 44 | A2PM_ACT_FILTERS, 45 | #if 0 46 | A2PM_ACT_X264BD, 47 | A2PM_ACT_X264RAW, 48 | #endif 49 | }; 50 | 51 | enum format_type_t{ 52 | FMT_NOTHING = 0, 53 | FMT_RAWVIDEO, 54 | FMT_RAWVIDEO_VFLIP, 55 | FMT_YUV4MPEG2, 56 | FMT_RAWAUDIO, 57 | FMT_WAVEFORMATEX, 58 | FMT_WAVEFORMATEXTENSIBLE, 59 | #if 0 60 | FMT_HDBD, 61 | FMT_SDBD, 62 | FMT_WITHOUT_TCFILE, 63 | FMT_WITH_TCFILE, 64 | #endif 65 | }; 66 | 67 | 68 | struct Params { 69 | action_t action; 70 | format_type_t format_type; 71 | int sarnum; 72 | int sarden; 73 | int trimstart; 74 | int trimend; 75 | char frame_type; 76 | char* bit; 77 | int yuv_depth; 78 | const char* dll_path; 79 | uint32_t channel_mask; 80 | int colorrange; 81 | int colorprim; 82 | int transfer; 83 | int colormatrix; 84 | int chromaloc; 85 | Params() : action(A2PM_ACT_NOTHING), format_type(FMT_NOTHING), sarnum(0), 86 | sarden(0), trimstart(0), trimend(0), frame_type(0), bit(nullptr), 87 | yuv_depth(0), dll_path(nullptr), channel_mask(0), 88 | colorrange(-1), colorprim(2), transfer(2), colormatrix(2), 89 | chromaloc(-1) { } 90 | }; 91 | 92 | 93 | typedef IScriptEnvironment ise_t; 94 | 95 | class Avs2PipeMod { 96 | HMODULE dll; 97 | Params& params; 98 | ise_t* env; 99 | PClip clip; 100 | VideoInfo vi; 101 | float version; 102 | const char* versionString; 103 | const char* input; 104 | int sampleBits; 105 | int numPlanes; 106 | 107 | void invokeFilter(const char* filter, AVSValue args, const char** names=nullptr); 108 | void trim(); 109 | void prepareY4MOut(); 110 | template int writeFrames(); 111 | template int writePixValuesAsText(); 112 | public: 113 | Avs2PipeMod(HMODULE dll, ise_t* env, PClip clip, const char* input, Params& p); 114 | ~Avs2PipeMod(); 115 | void info(bool act_info); 116 | void benchmark(); 117 | void outAudio(); 118 | void outVideo(); 119 | void dumpPixValues(); 120 | void dumpPluginFiltersList(); 121 | void dumpFrameProps(); 122 | /* 123 | void x264bd(Params& params); 124 | void x264raw(Params& params); 125 | */ 126 | static Avs2PipeMod* create(const char* input, Params& p); 127 | }; 128 | 129 | #endif /* AVS2PIPEMOD_H */ 130 | -------------------------------------------------------------------------------- /readme_original.txt: -------------------------------------------------------------------------------- 1 | avs2pipe is a tool to output y4m video, wav audio, dump some info about the 2 | input avs clip or suggest x264 blu-ray encoding settings. 3 | 4 | Usage: avs2pipe [audio|video|info|x264] input.avs 5 | audio - output wav extensible format audio to stdout. 6 | video - output yuv4mpeg2 format video to stdout. 7 | info - output information about aviscript clip. 8 | x264bd - suggest x264 arguments for blu-ray disc encoding. 9 | 10 | 11 | It simply takes a path to an avs script that returns a clip with audio and/or 12 | video and outputs it as a y4m (--video) or wav (--audio) stream to stdout. It 13 | can also generate information about the clip (--info) in an easily parsable 14 | format for gui's. 15 | 16 | The advantage of avs2pipe over other solutions, other than it doing multiple 17 | things in one and supporting more colorspaces is that it links directly with 18 | avisynth.dll via the C interface, so it compiles with both Visual Studio and 19 | MinGW and when used with wine in linux simply requires that the avisynth dll's 20 | are in PATH, so no need to have vfw configured correctly. Also as it uses 21 | WAVE_FORMAT_EXTENSIBLE for wav output and directly takes data from avisynth 22 | it will output exactly the same data as avisynth uses internally. 23 | 24 | 25 | Source is provided in the src folder and is released under the GPL-3.0. 26 | In addition to the source is: 27 | 28 | src\avisynth25 - header and link lib from AviSynth 2.5.8 29 | src\avisynth26 - header from http://git.videolan.org/?p=x264.git;a=blob;f=extras/avisynth_c.h;hb=HEAD 30 | link lib from AviSynth AVS 2.6.0 Alpha 2 [090927] 31 | 32 | 33 | Projects and scripts to build the source are located in: 34 | 35 | vs2010 - Project for Visual Studio 2010 Express 36 | mingw - Batch file MinGW on Windows, sh script for MinGW under Linux 37 | 38 | 39 | The YUV4MPEG2 output is inspired by Avs2YUV by Loren Merritt, it is basically 40 | the same method, converted from C++ to C. The changes are: 41 | 42 | Interlaced Support - Picks up the flags for interlaced content from 43 | AssumeFieldBased(), AssumeTFF() etc. and will pass this 44 | on to programs such as x264 eliminating the need to add 45 | flags to the encoder command. 46 | 47 | Extended Color Spaces - Thanks to Chikuzen when compiled against AviSynth 2.6 48 | there is support for a whole host of extra colorspaces. 49 | 50 | 51 | The WAV output is coded from scratch using specs from "the internet" and so 52 | could be full of problems, altho I have not found any in testing yet. 53 | 54 | 55 | 56 | Examples: 57 | 58 | avs2pipe info input.avs 59 | avs2pipe info input.avs > info.txt 60 | 61 | avs2pipe video input.avs | x264 --stdin y4m - --output video.h264 62 | avs2pipe audio input.avs | neroAacEnc -q 0.25 -if - -of audio.aac 63 | 64 | avs2pipe audio input.avs > output.wav 65 | 66 | 67 | Included Binaries: 68 | 69 | avs2pipe_gcc.exe - compiled with MinGW gcc 4.5.2 on Windows 70 | avs2pipe_vs.exe - compiled with Visual Studio 2010 Express cl 16.00.30319.01 71 | 72 | avs2pipe26_gcc.exe - gcc compiled for AviSynth 2.6 Alpha 2 73 | avs2pipe26_vs.exe - VS2010 compiled for AviSynth 2.6 Alpha 2 74 | 75 | 76 | Download: 77 | 78 | http://doobrymedia.com/avs2pipe-0.0.3.zip 79 | 80 | 81 | Changes: 82 | 83 | Version 0.0.3 - Added Blu-Ray x264 command generator based on specs from 84 | http://sites.google.com/site/x264bluray/ 85 | Added Support for new Colorspaces in AviSynth 2.6 with code 86 | written by Chikuzen 87 | Version 0.0.2 - Added Support for Interlaced Output, AssumeFieldBased etc. 88 | Version 0.0.1 - Removed Progress Output for "Speed" 89 | Pre-Alpha - Initial Release on doom10.org 90 | 91 | 92 | Forum Links: 93 | 94 | Original - http://doom10.org/index.php?topic=759 95 | Doom9 Continuation - http://forum.doom9.org/showthread.php?t=160383 96 | 97 | -------------------------------------------------------------------------------- /src/wave.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2016 Chris Beswick 3 | * Oka Motofumi 4 | * 5 | * This file is part of avs2pipemod. 6 | * 7 | * avs2pipemod is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * avs2pipemod is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with avs2pipemod. If not, see . 19 | * 20 | */ 21 | 22 | #include "stdlib.h" 23 | #include "utils.h" 24 | #include "wave.h" 25 | 26 | 27 | static uint32_t get_channel_mask(uint16_t channels) 28 | { 29 | switch (channels) { 30 | case 1: 31 | return WAV_FC; 32 | case 2: 33 | return WAV_FL | WAV_FR; 34 | case 3: 35 | return WAV_FL | WAV_FR | WAV_BC; 36 | case 4: 37 | return WAV_FL | WAV_FR | WAV_BL | WAV_BR; 38 | case 5: 39 | return WAV_FL | WAV_FR | WAV_FC | WAV_BL | WAV_BR; 40 | case 6: 41 | return WAV_FL | WAV_FR | WAV_FC | WAV_LF | WAV_BL | WAV_BR; 42 | case 7: 43 | return WAV_FL | WAV_FR | WAV_FC | WAV_LF | WAV_BL | WAV_BR | WAV_BC; 44 | case 8: 45 | return WAV_FL | WAV_FR | WAV_FC | WAV_LF | WAV_BL | WAV_BR | WAV_FLC | WAV_FRC; 46 | default: 47 | return 0; 48 | } 49 | } 50 | 51 | 52 | WaveRiffHeader::WaveRiffHeader(wave_args_t& a, size_t header_size) 53 | { 54 | uint32_t fact_samples = static_cast(a.samples); 55 | if(a.samples > UINT32_MAX) { 56 | a2pm_log(LOG_WARNING, "audio sample number over 32bit limit.\n"); 57 | fact_samples = UINT32_MAX; 58 | } 59 | 60 | int64_t ds64 = fact_samples * a.channels * a.byte_depth; 61 | int64_t rs64 = ds64 + header_size - sizeof(WaveChunkHeader); 62 | uint32_t data_size = static_cast(ds64); 63 | uint32_t riff_size = static_cast(rs64); 64 | if (rs64 > UINT32_MAX) { 65 | a2pm_log(LOG_WARNING, "audio size over 32bit limit (4GB), clients may truncate audio.\n"); 66 | data_size = UINT32_MAX; 67 | riff_size = UINT32_MAX; 68 | } 69 | 70 | riff.header.id = WAVE_FOURCC("RIFF"); 71 | riff.header.size = riff_size; 72 | riff.type = WAVE_FOURCC("WAVE"); 73 | 74 | format.header.id = WAVE_FOURCC("fmt "); 75 | format.header.size = sizeof(format) - sizeof(format.header); 76 | format.tag = a.format; 77 | format.channels = static_cast(a.channels); 78 | format.sample_rate = a.sample_rate; 79 | format.byte_rate = a.channels * a.sample_rate * a.byte_depth; 80 | format.block_align = a.channels * a.byte_depth; 81 | format.bit_depth = a.byte_depth * 8; 82 | format.ext_size = 0; 83 | data.header.id = WAVE_FOURCC("data"); 84 | data.header.size = data_size; 85 | } 86 | 87 | WaveRiffExtHeader::WaveRiffExtHeader(wave_args_t& a) 88 | { 89 | auto wrh = WaveRiffHeader(a, sizeof(WaveRiffExtHeader)); 90 | 91 | riff = wrh.riff; 92 | format = wrh.format; 93 | data = wrh.data; 94 | 95 | format.tag = WAVE_FORMAT_EXTENSIBLE; 96 | format.ext_size = sizeof(valid_bits) + sizeof(channel_mask) + sizeof(sub_format); 97 | format.header.size += format.ext_size; 98 | valid_bits = a.byte_depth * 8; 99 | channel_mask = a.channel_mask ? a.channel_mask : get_channel_mask(a.channels); 100 | 101 | WaveGuid sf = { 102 | a.format, 0x0000, 0x0010, 103 | {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71} 104 | }; 105 | sub_format = sf; 106 | 107 | fact.header.id = WAVE_FOURCC("fact"); 108 | fact.header.size = sizeof(fact) - sizeof(fact.header); 109 | fact.samples = a.samples > UINT32_MAX ? UINT32_MAX : static_cast(a.samples); 110 | } 111 | 112 | -------------------------------------------------------------------------------- /src/wave.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2016 Oka Motofumi 3 | * Chris Beswick 4 | * 5 | * This file is part of avs2pipemod. 6 | * 7 | * avs2pipemod is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * avs2pipemod is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with avs2pipemod. If not, see . 19 | * 20 | */ 21 | 22 | // Wave File Specifications 23 | // http://www-mmsp.ece.mcgill.ca/documents/audioformats/wave/wave.html 24 | 25 | #ifndef WAVE_H 26 | #define WAVE_H 27 | 28 | #include 29 | 30 | 31 | 32 | 33 | static inline uint32_t WAVE_FOURCC(const char* str) 34 | { 35 | return *reinterpret_cast(str); 36 | } 37 | 38 | 39 | enum WaveFormatType : uint32_t { 40 | WAVE_FORMAT_PCM = 0x0001, // samples are ints 41 | WAVE_FORMAT_IEEE_FLOAT = 0x0003, // samples are floats 42 | WAVE_FORMAT_EXTENSIBLE = 0xFFFE // not a real type. 43 | }; 44 | 45 | 46 | enum speaker_position : uint32_t { 47 | WAV_FL = 0x000001, 48 | WAV_FR = 0x000002, 49 | WAV_FC = 0x000004, 50 | WAV_LF = 0x000008, 51 | WAV_BL = 0x000010, 52 | WAV_BR = 0x000020, 53 | WAV_FLC = 0x000040, 54 | WAV_FRC = 0x000080, 55 | WAV_BC = 0x000100, 56 | WAV_SL = 0x000200, 57 | WAV_SR = 0x000400, 58 | WAV_TC = 0x000800, 59 | WAV_TFL = 0x001000, 60 | WAV_TFC = 0x002000, 61 | WAV_TFR = 0x004000, 62 | WAV_TBL = 0x008000, 63 | WAV_TBC = 0x010000, 64 | WAV_TBR = 0x020000, 65 | }; 66 | 67 | 68 | struct wave_args_t { 69 | WaveFormatType format; 70 | int channels; 71 | int sample_rate; 72 | int byte_depth; 73 | int64_t samples; 74 | uint32_t channel_mask; 75 | }; 76 | 77 | 78 | // set packing alignment to 1 byte so we can just fwrite structs 79 | // gcc docs say it supports this to be compatable with vs. 80 | #pragma pack(push, 1) 81 | 82 | // docs state a chunk should be [id, size, [data, ...]] but this way 83 | // means I can get size = sizeof(chunk) - sizeof(chunk.header) 84 | 85 | // just a uuid, but this works so... 86 | struct WaveGuid { 87 | uint32_t d1; 88 | uint16_t d2; 89 | uint16_t d3; 90 | uint8_t d4[8]; 91 | }; 92 | 93 | // really RiffChunkHeader, but Wave* naming seems neater 94 | struct WaveChunkHeader { 95 | uint32_t id; // FOURCC 96 | uint32_t size; // size of chunk data 97 | }; 98 | 99 | // first riff chunk at the start of the file 100 | struct WaveRiffChunk { 101 | WaveChunkHeader header; // id = RIFF, size = total size - sizeof(header) 102 | uint32_t type; // WAVE 103 | }; 104 | 105 | // data size chunk for RF64 format 106 | struct WaveDs64Chunk { 107 | WaveChunkHeader header; // id = ds64, size = sizeof(WaveRf64Chunk) - sizeof(header) 108 | uint64_t riff_size; // replaces WaveRiffHeader.size when latter = -1 109 | uint64_t data_size; // replaces WaveDataHeader.size when latter = -1 110 | uint64_t fact_samples; // replaces WaveFactChunk.samples when latter = -1 111 | uint32_t table_size; // 0, spec does not say what this is for, anyone??? 112 | }; 113 | 114 | // wave format chunk based on WAVE_FORMAT 115 | struct WaveFormatChunk { 116 | WaveChunkHeader header; 117 | uint16_t tag; 118 | uint16_t channels; 119 | uint32_t sample_rate; 120 | uint32_t byte_rate; 121 | uint16_t block_align; 122 | uint16_t bit_depth; 123 | uint16_t ext_size; 124 | }; 125 | 126 | 127 | struct WaveFormatExtChunk { 128 | uint16_t valid_bits; // equal to bit_depth if uncompressed 129 | uint32_t channel_mask; // speaker position mask 130 | WaveGuid sub_format; // guid of sub format eg. pcm, float, ... 131 | }; 132 | 133 | // wave fact chunk for WAVE_FORMAT_EXTENSIBLE, required when not PCM, always used in this imp 134 | struct WaveFactChunk { 135 | WaveChunkHeader header; // id = FACT, size = sizeof(WaveFactChunk) - sizeof(header) 136 | uint32_t samples; // number of channels * total samples per channel 137 | }; 138 | 139 | // partial data chunk, just the header, would be stupid to place ALL data into a struct 140 | struct WaveDataChunk { 141 | WaveChunkHeader header; // id = FACT, size = sizeof(data) 142 | }; 143 | 144 | // RIFF header for a WAVE_FORMAT file 145 | struct WaveRiffHeader { 146 | WaveRiffChunk riff; 147 | WaveFormatChunk format; 148 | WaveDataChunk data; 149 | WaveRiffHeader(wave_args_t& a, size_t header_size=sizeof(WaveRiffHeader)); 150 | }; 151 | 152 | // complete RIFF header for a WAVE_FORMAT_EXTENSIBLE file 153 | struct WaveRiffExtHeader { 154 | WaveRiffChunk riff; 155 | WaveFormatChunk format; 156 | uint16_t valid_bits; 157 | uint32_t channel_mask; 158 | WaveGuid sub_format; 159 | WaveFactChunk fact; 160 | WaveDataChunk data; 161 | WaveRiffExtHeader(wave_args_t& a); 162 | }; 163 | 164 | 165 | // pop previous packing alignment 166 | #pragma pack(pop) 167 | 168 | 169 | #endif // WAVE_H 170 | -------------------------------------------------------------------------------- /src/getopt.h: -------------------------------------------------------------------------------- 1 | /* Getopt for Microsoft C 2 | This code is a modification of the Free Software Foundation, Inc. 3 | Getopt library for parsing command line argument the purpose was 4 | to provide a Microsoft Visual C friendly derivative. This code 5 | provides functionality for both Unicode and Multibyte builds. 6 | 7 | Date: 02/03/2011 - Ludvik Jerabek - Initial Release 8 | Version: 1.1 9 | Comment: Supports getopt, getopt_long, and getopt_long_only 10 | and POSIXLY_CORRECT environment flag 11 | License: LGPL 12 | 13 | Revisions: 14 | 15 | 02/03/2011 - Ludvik Jerabek - Initial Release 16 | 02/20/2011 - Ludvik Jerabek - Fixed compiler warnings at Level 4 17 | 07/05/2011 - Ludvik Jerabek - Added no_argument, required_argument, optional_argument defs 18 | 08/03/2011 - Ludvik Jerabek - Fixed non-argument runtime bug which caused runtime exception 19 | 08/09/2011 - Ludvik Jerabek - Added code to export functions for DLL and LIB 20 | 02/15/2012 - Ludvik Jerabek - Fixed _GETOPT_THROW definition missing in implementation file 21 | 08/01/2012 - Ludvik Jerabek - Created separate functions for char and wchar_t characters so single dll can do both unicode and ansi 22 | 10/15/2012 - Ludvik Jerabek - Modified to match latest GNU features 23 | 06/19/2015 - Ludvik Jerabek - Fixed maximum option limitation caused by option_a (255) and option_w (65535) structure val variable 24 | 09/24/2022 - Ludvik Jerabek - Updated to match most recent getopt release 25 | 09/25/2022 - Ludvik Jerabek - Fixed memory allocation (malloc call) issue for wchar_t* 26 | 27 | **DISCLAIMER** 28 | THIS MATERIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 29 | EITHER EXPRESS OR IMPLIED, INCLUDING, BUT Not LIMITED TO, THE 30 | IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 31 | PURPOSE, OR NON-INFRINGEMENT. SOME JURISDICTIONS DO NOT ALLOW THE 32 | EXCLUSION OF IMPLIED WARRANTIES, SO THE ABOVE EXCLUSION MAY NOT 33 | APPLY TO YOU. IN NO EVENT WILL I BE LIABLE TO ANY PARTY FOR ANY 34 | DIRECT, INDIRECT, SPECIAL OR OTHER CONSEQUENTIAL DAMAGES FOR ANY 35 | USE OF THIS MATERIAL INCLUDING, WITHOUT LIMITATION, ANY LOST 36 | PROFITS, BUSINESS INTERRUPTION, LOSS OF PROGRAMS OR OTHER DATA ON 37 | YOUR INFORMATION HANDLING SYSTEM OR OTHERWISE, EVEN If WE ARE 38 | EXPRESSLY ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 39 | */ 40 | #ifndef __GETOPT_H_ 41 | #define __GETOPT_H_ 42 | 43 | #ifdef _GETOPT_API 44 | #undef _GETOPT_API 45 | #endif 46 | 47 | #if defined(EXPORTS_GETOPT) && defined(STATIC_GETOPT) 48 | #error "The preprocessor definitions of EXPORTS_GETOPT and STATIC_GETOPT can only be used individually" 49 | #elif defined(STATIC_GETOPT) 50 | #pragma message("Warning static builds of getopt violate the Lesser GNU Public License") 51 | #define _GETOPT_API 52 | #elif defined(EXPORTS_GETOPT) 53 | #pragma message("Exporting getopt library") 54 | #define _GETOPT_API __declspec(dllexport) 55 | #else 56 | #pragma message("Importing getopt library") 57 | #define _GETOPT_API __declspec(dllimport) 58 | #endif 59 | 60 | // Change behavior for C\C++ 61 | #ifdef __cplusplus 62 | #define _BEGIN_EXTERN_C extern "C" { 63 | #define _END_EXTERN_C } 64 | #define _GETOPT_THROW throw() 65 | #else 66 | #define _BEGIN_EXTERN_C 67 | #define _END_EXTERN_C 68 | #define _GETOPT_THROW 69 | #endif 70 | 71 | // Standard GNU options 72 | #define null_argument 0 /*Argument Null*/ 73 | #define no_argument 0 /*Argument Switch Only*/ 74 | #define required_argument 1 /*Argument Required*/ 75 | #define optional_argument 2 /*Argument Optional*/ 76 | 77 | // Shorter Options 78 | #define ARG_NULL 0 /*Argument Null*/ 79 | #define ARG_NONE 0 /*Argument Switch Only*/ 80 | #define ARG_REQ 1 /*Argument Required*/ 81 | #define ARG_OPT 2 /*Argument Optional*/ 82 | 83 | #include 84 | #include 85 | 86 | _BEGIN_EXTERN_C 87 | 88 | extern _GETOPT_API int optind; 89 | extern _GETOPT_API int opterr; 90 | extern _GETOPT_API int optopt; 91 | 92 | // Ansi 93 | struct option_a 94 | { 95 | const char* name; 96 | int has_arg; 97 | int *flag; 98 | int val; 99 | }; 100 | extern _GETOPT_API char *optarg_a; 101 | extern _GETOPT_API int getopt_a(int argc, char *const *argv, const char *optstring) _GETOPT_THROW; 102 | extern _GETOPT_API int getopt_long_a(int argc, char *const *argv, const char *options, const struct option_a *long_options, int *opt_index) _GETOPT_THROW; 103 | extern _GETOPT_API int getopt_long_only_a(int argc, char *const *argv, const char *options, const struct option_a *long_options, int *opt_index) _GETOPT_THROW; 104 | 105 | // Unicode 106 | struct option_w 107 | { 108 | const wchar_t* name; 109 | int has_arg; 110 | int *flag; 111 | int val; 112 | }; 113 | extern _GETOPT_API wchar_t *optarg_w; 114 | extern _GETOPT_API int getopt_w(int argc, wchar_t *const *argv, const wchar_t *optstring) _GETOPT_THROW; 115 | extern _GETOPT_API int getopt_long_w(int argc, wchar_t *const *argv, const wchar_t *options, const struct option_w *long_options, int *opt_index) _GETOPT_THROW; 116 | extern _GETOPT_API int getopt_long_only_w(int argc, wchar_t *const *argv, const wchar_t *options, const struct option_w *long_options, int *opt_index) _GETOPT_THROW; 117 | 118 | _END_EXTERN_C 119 | 120 | #undef _BEGIN_EXTERN_C 121 | #undef _END_EXTERN_C 122 | #undef _GETOPT_THROW 123 | #undef _GETOPT_API 124 | 125 | #ifdef _UNICODE 126 | #define getopt getopt_w 127 | #define getopt_long getopt_long_w 128 | #define getopt_long_only getopt_long_only_w 129 | #define option option_w 130 | #define optarg optarg_w 131 | #else 132 | #define getopt getopt_a 133 | #define getopt_long getopt_long_a 134 | #define getopt_long_only getopt_long_only_a 135 | #define option option_a 136 | #define optarg optarg_a 137 | #endif 138 | #endif // __GETOPT_H_ 139 | -------------------------------------------------------------------------------- /vs2022/avs2pipemod.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {C230224B-8923-4E25-BB27-F54DC90FFA2A} 23 | Win32Proj 24 | 10.0 25 | 26 | 27 | 28 | Application 29 | true 30 | v143 31 | MultiByte 32 | 33 | 34 | Application 35 | false 36 | v143 37 | MultiByte 38 | true 39 | 40 | 41 | Application 42 | true 43 | v143 44 | MultiByte 45 | 46 | 47 | Application 48 | false 49 | v143 50 | MultiByte 51 | true 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | true 73 | 74 | 75 | false 76 | 77 | 78 | $(ProjectName)64 79 | 80 | 81 | false 82 | 83 | 84 | 85 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 86 | C:\my_projects\AviSynthPlus\avs_core\include;%(AdditionalIncludeDirectories) 87 | MultiThreadedDebugDLL 88 | Level3 89 | ProgramDatabase 90 | Disabled 91 | 92 | 93 | MachineX86 94 | true 95 | Console 96 | 97 | 98 | 99 | 100 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 101 | C:\my_projects\AviSynthPlus\avs_core\include;%(AdditionalIncludeDirectories) 102 | MultiThreadedDLL 103 | Level3 104 | ProgramDatabase 105 | true 106 | Speed 107 | true 108 | true 109 | StreamingSIMDExtensions2 110 | Fast 111 | false 112 | 113 | 114 | MachineX86 115 | true 116 | Console 117 | true 118 | true 119 | true 120 | 121 | 122 | 123 | 124 | C:\AviSynth+\FilterSDK\include;%(AdditionalIncludeDirectories) 125 | true 126 | Speed 127 | true 128 | true 129 | Fast 130 | stdcpp20 131 | stdc17 132 | AnySuitable 133 | 134 | 135 | UseLinkTimeCodeGeneration 136 | false 137 | 138 | 139 | ..\src\avs2pipemod.manifest 140 | 141 | 142 | 143 | 144 | C:\AviSynth+\FilterSDK\include;%(AdditionalIncludeDirectories) 145 | Fast 146 | stdcpp20 147 | stdc17 148 | 149 | 150 | true 151 | 152 | 153 | ..\src\avs2pipemod.manifest 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012-2016 Oka Motofumi 3 | * 4 | * This file is part of avs2pipemod. 5 | * 6 | * avs2pipemod is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * avs2pipemod is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with avs2pipemod. If not, see . 18 | * 19 | */ 20 | 21 | #include 22 | #include 23 | #include "avs2pipemod.h" 24 | #include "utils.h" 25 | #include "getopt.h" 26 | 27 | 28 | static void usage() 29 | { 30 | fprintf(stderr, "avs2pipemod ver %s\nbuilt on %s %s\n\n", 31 | A2PM_VERSION, __DATE__, __TIME__); 32 | fprintf(stderr, 33 | "Usage: avs2pipemod [option] input.avs\n" 34 | " e.g. avs2pipemod -wav=24bit input.avs > output.wav\n" 35 | " avs2pipemod -y4mt=10:11 input.avs | x264 - --demuxer y4m -o tff.mkv\n" 36 | " avs2pipemod -rawvideo -trim=1000,0 input.avs > output.yuv\n" 37 | "\n" 38 | " -wav[=8bit|16bit|24bit|32bit|float default unset]\n" 39 | " output wav format audio(WAVEFORMATEX) to stdout.\n" 40 | " if optional arg is set, audio sample type of input will be converted\n" 41 | " to specified value.\n" 42 | "\n" 43 | " -extwav[=8bit|16bit|24bit|32bit|float default unset]\n" 44 | " output wav extensible format audio(WAVEFORMATEXTENSIBLE) containing\n" 45 | " channel-mask to stdout.\n" 46 | " if optional arg is set, audio sample type of input will be converted\n" 47 | " to specified value.\n" 48 | "\n" 49 | " -rawaudio[=8bit|16bit|24bit|32bit|float default unset]\n" 50 | " output raw pcm audio(without any header) to stdout.\n" 51 | " if optional arg is set, audio sample type of input will be converted\n" 52 | " to specified value.\n" 53 | "\n" 54 | " -y4mp[=sar default 0:0]\n" 55 | " output yuv4mpeg2 format video to stdout as progressive.\n" 56 | " -y4mt[=sar default 0:0]\n" 57 | " output yuv4mpeg2 format video to stdout as tff interlaced.\n" 58 | " -y4mb[=sar default 0:0]\n" 59 | " output yuv4mpeg2 format video to stdout as bff interlaced.\n" 60 | "\n" 61 | " -rawvideo[=vflip default unset]\n" 62 | " output rawvideo(without any header) to stdout.\n" 63 | "\n" 64 | #if 0 65 | " -x264bdp[=4:3 default unset(16:9)]\n" 66 | " suggest x264(r1939 or later) arguments for bluray disc encoding\n" 67 | " in case of progressive source.\n" 68 | " set optarg if DAR4:3 is required(ntsc/pal sd source).\n" 69 | " -x264bdt[=4:3 default unset(16:9)]\n" 70 | " suggest x264(r1939 or later) arguments for bluray disc encoding\n" 71 | " in case of tff interlaced source.\n" 72 | " set optional arg if DAR4:3 is required(ntsc/pal sd source).\n" 73 | "\n" 74 | " -x264raw[=input-depth(8 to 16) default 8]\n" 75 | " suggest x264 arguments in case of -rawvideo output.\n" 76 | " set optional arg when using interleaved output of dither hack.\n" 77 | " -x264rawtc[=input-depth(8 to 16) default 8]\n" 78 | " suggest x264 arguments in case of -rawvideo output with --tcfile-in.\n" 79 | " set optional arg when using interleaved output of dither hack.\n" 80 | "\n" 81 | #endif 82 | " -info - output information about aviscript clip.\n" 83 | "\n" 84 | " -filters - output external plugin filters/functions list to stdout.\n" 85 | "\n" 86 | " -benchmark - do benchmark avs script, and output results to stdout.\n" 87 | "\n" 88 | " -dumptxt - dump pixel values as tab separated text to stdout.\n" 89 | "\n" 90 | " -dumpprops - dump frame properties as JSON string to stdout.\n" 91 | "\n" 92 | " -trim[=first_frame,last_frame default 0,0]\n" 93 | " add Trim(first_frame,last_frame) to input script.\n" 94 | " in info, this option is ignored.\n" 95 | "\n" 96 | " -dll[=path to avisynth.dll default \"avisynth\"]\n" 97 | " specify which avisynth.dll is used.\n" 98 | "\n" 99 | "note1 : in yuv4mpeg2 output mode, RGB input that has 720pix height or more\n" 100 | " will be converted to YUV with Rec.709 coefficients instead of Rec.601.\n" 101 | "\n" 102 | "note2 : in fact, it is a spec violation to use WAVEFORMATEX(-wav option)\n" 103 | " except 8bit/16bit PCM.\n" 104 | " however, there are some applications that accept such invalid files\n" 105 | " instead of supporting WAVEFORMATEXTENSIBLE.\n" 106 | "\n" 107 | "note3 : speaker positions in files output by '-extwav' are set using\n" 108 | " the clip property from avs+.\n" 109 | " if it is not set, general speaker positions are used instead.\n" 110 | "\n" 111 | " Chan. MS channels Description\n" 112 | " ----- ------------------------- ----------------\n" 113 | " 1 FC Mono\n" 114 | " 2 FL FR Stereo\n" 115 | " 3 FL FR BC First Surround\n" 116 | " 4 FL FR BL BR Quadro\n" 117 | " 5 FL FR FC BL BR like Dpl II (without LFE)\n" 118 | " 6 FL FR FC LF BL BR Standard Surround\n" 119 | " 7 FL FR FC LF BL BR BC With back center\n" 120 | " 8 FL FR FC LF BL BR FLC FRC With front center left/right\n" 121 | "\n" 122 | #if 0 123 | "note4 : '-x264bd(p/t)' supports only for primary stream encoding.\n" 124 | "\n" 125 | "note5 : in '-x264raw(tc)' with dither hack, output format needs to be\n" 126 | " interleaved(not stacked).\n" 127 | #endif 128 | ); 129 | 130 | } 131 | 132 | 133 | static void parse_opts(int argc, char **argv, Params& p) 134 | { 135 | char short_opts[] = "a::Bb::c::C::de::ip::t::T:v::w::x::y::"; 136 | struct option long_opts[] = { 137 | { "rawaudio", optional_argument, nullptr, 'a' }, 138 | { "extwav", optional_argument, nullptr, 'e' }, 139 | { "audio", optional_argument, nullptr, 'e' }, /* for backward compatibility */ 140 | { "wav", optional_argument, nullptr, 'w' }, 141 | { "y4mp", optional_argument, nullptr, 'p' }, 142 | { "video", optional_argument, nullptr, 'p' }, /* for backward compatibility */ 143 | { "y4mt", optional_argument, nullptr, 't' }, 144 | { "y4mb", optional_argument, nullptr, 'b' }, 145 | { "rawvideo", optional_argument, nullptr, 'v' }, 146 | { "info", no_argument, nullptr, 'i' }, 147 | { "benchmark", no_argument, nullptr, 'B' }, 148 | #if 0 149 | { "x264bdp", optional_argument, nullptr, 'x' }, 150 | { "x264bdt", optional_argument, nullptr, 'y' }, 151 | { "x264raw", optional_argument, nullptr, 'c' }, 152 | { "x264rawtc", optional_argument, nullptr, 'C' }, 153 | #endif 154 | { "dumpyuv", no_argument, nullptr, 'd' }, /* for backward compatibility */ 155 | { "dumptxt", no_argument, nullptr, 'd' }, 156 | { "dumpprops", no_argument, nullptr, 'j' }, 157 | { "filters", no_argument, nullptr, 'f' }, 158 | { "trim", required_argument, nullptr, 'T' }, 159 | { "dll", required_argument, nullptr, 'D' }, 160 | { "y4mbits", required_argument, nullptr, 'Y'}, 161 | {nullptr, 0, nullptr, 0} 162 | }; 163 | 164 | int index = 0; 165 | int parse = 0; 166 | int ret; 167 | 168 | while ((parse = getopt_long_only(argc, argv, short_opts, long_opts, &index)) != -1) { 169 | switch(parse) { 170 | case 'a': 171 | case 'e': 172 | case 'w': 173 | p.action = A2PM_ACT_AUDIO; 174 | if(optarg) 175 | p.bit = optarg; 176 | p.format_type = parse == 'e' ? FMT_WAVEFORMATEXTENSIBLE : 177 | parse == 'a' ? FMT_RAWAUDIO : FMT_WAVEFORMATEX; 178 | break; 179 | case 't': 180 | case 'b': 181 | case 'p': 182 | p.action = A2PM_ACT_VIDEO; 183 | p.format_type = FMT_YUV4MPEG2; 184 | p.frame_type = parse; 185 | if (optarg) { 186 | ret = sscanf(optarg, "%d:%d", &p.sarnum, &p.sarden); 187 | validate(p.sarnum < 0 || p.sarden < 0, 188 | std::format("invalid argument \"{}\".\n\n", optarg)); 189 | } 190 | break; 191 | case 'v': 192 | p.action = A2PM_ACT_VIDEO; 193 | p.format_type = FMT_RAWVIDEO; 194 | if (optarg && !strncmp(optarg, "vflip", 5)) 195 | p.format_type = FMT_RAWVIDEO_VFLIP; 196 | break; 197 | case 'i': 198 | p.action = A2PM_ACT_INFO; 199 | break; 200 | case 'f': 201 | p.action = A2PM_ACT_FILTERS; 202 | break; 203 | case 'B': 204 | p.action = A2PM_ACT_BENCHMARK; 205 | break; 206 | #if 0 207 | case 'x': 208 | case 'y': 209 | p.action = A2PM_ACT_X264BD; 210 | p.frame_type = parse == 'x' ? 'p' : 't'; 211 | p.format_type = FMT_HDBD; 212 | if (optarg) { 213 | validate(strncmp(optarg, "4:3", 3) != 0, 214 | "invalid argument \"%s\".\n\n", optarg); 215 | p.format_type = FMT_SDBD; 216 | } 217 | break; 218 | case 'c': 219 | case 'C': 220 | p.action = A2PM_ACT_X264RAW; 221 | p.yuv_depth = 8; 222 | p.format_type = parse == 'c' ? FMT_WITHOUT_TCFILE : FMT_WITH_TCFILE; 223 | if (!optarg) 224 | break; 225 | sscanf(optarg, "%d", &p.yuv_depth); 226 | if (p.yuv_depth < 8 || p.yuv_depth > 16) { 227 | fprintf(stderr, "avs2pipemod[error]:invalid argument \"%s\".\n\n", optarg); 228 | fprintf(stderr, "The argument needs to be an integer of 8 to 16.\n\n"); 229 | throw std::runtime_error(nullptr); 230 | } 231 | break; 232 | #endif 233 | case 'd': 234 | p.action = A2PM_ACT_DUMP_PIXEL_VALUES_AS_TXT; 235 | break; 236 | case 'j': 237 | p.action = A2PM_ACT_DUMP_FRAME_PROPERTIES_AS_JSON; 238 | break; 239 | case 'T': 240 | ret = sscanf(optarg, "%d,%d", &p.trimstart, &p.trimend); 241 | break; 242 | case 'D': 243 | p.dll_path = optarg; 244 | break; 245 | case 'Y': 246 | ret = sscanf(optarg, "%d", &p.yuv_depth); 247 | validate(p.yuv_depth != 9 && p.yuv_depth != 10 && p.yuv_depth != 12 248 | && p.yuv_depth != 14 && p.yuv_depth != 16, 249 | "invalid bits specified."); 250 | break; 251 | default: 252 | break; 253 | } 254 | } 255 | } 256 | 257 | 258 | 259 | int main(int argc, char** argv) 260 | { 261 | if (argc < 3) { 262 | usage(); 263 | return -1; 264 | } 265 | 266 | #if defined(_WIN32) 267 | auto cp = GetConsoleOutputCP(); 268 | #else 269 | #define SetConsoleOutputCP(X) 270 | #endif; 271 | SetConsoleOutputCP(CP_UTF8); 272 | 273 | 274 | try { 275 | auto params = Params(); 276 | parse_opts(argc, argv, params); 277 | 278 | std::unique_ptr a2pm( 279 | Avs2PipeMod::create(argv[argc - 1], params)); 280 | 281 | switch(params.action) { 282 | case A2PM_ACT_INFO: 283 | a2pm->info(true); 284 | break; 285 | case A2PM_ACT_BENCHMARK: 286 | a2pm->benchmark(); 287 | break; 288 | case A2PM_ACT_AUDIO: 289 | a2pm->outAudio(); 290 | break; 291 | case A2PM_ACT_VIDEO: 292 | a2pm->outVideo(); 293 | break; 294 | #if 0 295 | case A2PM_ACT_X264BD: 296 | a2pm->x264bd(params); 297 | break; 298 | case A2PM_ACT_X264RAW: 299 | a2pm->x264raw(params); 300 | break; 301 | #endif 302 | case A2PM_ACT_DUMP_PIXEL_VALUES_AS_TXT: 303 | a2pm->dumpPixValues(); 304 | break; 305 | case A2PM_ACT_DUMP_FRAME_PROPERTIES_AS_JSON: 306 | a2pm->dumpFrameProps(); 307 | break; 308 | case A2PM_ACT_FILTERS: 309 | a2pm->dumpPluginFiltersList(); 310 | break; 311 | default: 312 | break; 313 | } 314 | } catch (std::runtime_error& e) { 315 | if (e.what()) { 316 | fprintf(stderr, "avs2pipemod[error]: %s", e.what()); 317 | } 318 | SetConsoleOutputCP(cp); 319 | exit(-1); 320 | } catch (AvisynthError& e) { 321 | fprintf(stderr, "avs2pipemod[error]: %s", e.msg); 322 | SetConsoleOutputCP(cp); 323 | exit(-1); 324 | } 325 | SetConsoleOutputCP(cp); 326 | 327 | return 0; 328 | } 329 | -------------------------------------------------------------------------------- /src/utils.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Oka Motofumi 3 | * 4 | * This file is part of avs2pipemod. 5 | * 6 | * avs2pipemod is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * avs2pipemod is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with avs2pipemod. If not, see . 18 | * 19 | */ 20 | 21 | 22 | #include 23 | #if defined(_WIN32) 24 | #define WIN32_LEAN_AND_MEAN 25 | #define VC_EXTRALEAN 26 | #define NOMINMAX 27 | #define NOGDI 28 | #include 29 | #include 30 | #include 31 | #else 32 | #include 33 | #include 34 | #endif 35 | #include "utils.h" 36 | 37 | 38 | void a2pm_log(int level, const char *message, ...) 39 | { 40 | va_list args; 41 | fprintf(stderr, 42 | level == LOG_INFO ? "avs2pipemod[info]: " : 43 | level == LOG_REPEAT ? "\ravs2pipemod[info]: " : 44 | level == LOG_WARNING ? "avs2pipemod[warning]: " : ""); 45 | 46 | va_start(args, message); 47 | vfprintf(stderr, message, args); 48 | va_end(args); 49 | } 50 | 51 | 52 | const char* get_string_info(int pix_type) 53 | { 54 | switch (pix_type) { 55 | // gray 56 | case VideoInfo::CS_Y8: return "Y8"; 57 | case VideoInfo::CS_Y10: return "Y10"; 58 | case VideoInfo::CS_Y12: return "Y12"; 59 | case VideoInfo::CS_Y14: return "Y14"; 60 | case VideoInfo::CS_Y16: return "Y16"; 61 | case VideoInfo::CS_Y32: return "Y32"; 62 | // packed RGB 63 | case VideoInfo::CS_BGR32: return "RGB32"; 64 | case VideoInfo::CS_BGR64: return "RGB64"; 65 | case VideoInfo::CS_BGR24: return "RGB24"; 66 | case VideoInfo::CS_BGR48: return "RGB48"; 67 | // packed YUV 68 | case VideoInfo::CS_YUY2: return "YUY2"; 69 | // planar RGB 70 | case VideoInfo::CS_RGBP: return "RGBP"; 71 | case VideoInfo::CS_RGBP10: return "RGBP10"; 72 | case VideoInfo::CS_RGBP12: return "RGBP12"; 73 | case VideoInfo::CS_RGBP14: return "RGBP14"; 74 | case VideoInfo::CS_RGBP16: return "RGBP16"; 75 | case VideoInfo::CS_RGBPS: return "RGBPS"; 76 | // planar RGB with alpha 77 | case VideoInfo::CS_RGBAP: return "RGBAP"; 78 | case VideoInfo::CS_RGBAP10: return "RGBAP10"; 79 | case VideoInfo::CS_RGBAP12: return "RGBAP12"; 80 | case VideoInfo::CS_RGBAP14: return "RGBAP14"; 81 | case VideoInfo::CS_RGBAP16: return "RGBAP16"; 82 | case VideoInfo::CS_RGBAPS: return "RGBAPS"; 83 | // planar yuv444 84 | case VideoInfo::CS_YV24: return "YV24"; 85 | case VideoInfo::CS_YUV444P10: return "YUV444P10"; 86 | case VideoInfo::CS_YUV444P12: return "YUV444P12"; 87 | case VideoInfo::CS_YUV444P14: return "YUV444P14"; 88 | case VideoInfo::CS_YUV444P16: return "YUV444P16"; 89 | case VideoInfo::CS_YUV444PS: return "YUV444PS"; 90 | // planar yuv444 with alpha 91 | case VideoInfo::CS_YUVA444: return "YUVA444"; 92 | case VideoInfo::CS_YUVA444P10: return "YUVA444P10"; 93 | case VideoInfo::CS_YUVA444P12: return "YUVA444P12"; 94 | case VideoInfo::CS_YUVA444P14: return "YUVA444P14"; 95 | case VideoInfo::CS_YUVA444P16: return "YUVA444P16"; 96 | case VideoInfo::CS_YUVA444PS: return "YUVA444PS"; 97 | // planar yuv422 98 | case VideoInfo::CS_YV16: return "YV16"; 99 | case VideoInfo::CS_YUV422P10: return "YUV422P10"; 100 | case VideoInfo::CS_YUV422P12: return "YUV422P12"; 101 | case VideoInfo::CS_YUV422P14: return "YUV422P14"; 102 | case VideoInfo::CS_YUV422P16: return "YUV422P16"; 103 | case VideoInfo::CS_YUV422PS: return "YUV422PS"; 104 | // planar yuv422 with alpha 105 | case VideoInfo::CS_YUVA422: return "YUVA422"; 106 | case VideoInfo::CS_YUVA422P10: return "YUVA422P10"; 107 | case VideoInfo::CS_YUVA422P12: return "YUVA422P12"; 108 | case VideoInfo::CS_YUVA422P14: return "YUVA422P14"; 109 | case VideoInfo::CS_YUVA422P16: return "YUVA422P16"; 110 | case VideoInfo::CS_YUVA422PS: return "YUVA422PS"; 111 | // planar yuv420 112 | case VideoInfo::CS_YV12: 113 | case VideoInfo::CS_I420: return "YV12"; 114 | case VideoInfo::CS_YUV420P10: return "YUV420P10"; 115 | case VideoInfo::CS_YUV420P12: return "YUV420P12"; 116 | case VideoInfo::CS_YUV420P14: return "YUV420P14"; 117 | case VideoInfo::CS_YUV420P16: return "YUV420P16"; 118 | case VideoInfo::CS_YUV420PS: return "YUV420PS"; 119 | // planar yuv420 with alpha 120 | case VideoInfo::CS_YUVA420: return "YUVA420"; 121 | case VideoInfo::CS_YUVA420P10: return "YUVA420P10"; 122 | case VideoInfo::CS_YUVA420P12: return "YUVA420P12"; 123 | case VideoInfo::CS_YUVA420P14: return "YUVA420P14"; 124 | case VideoInfo::CS_YUVA420P16: return "YUVA420P16"; 125 | case VideoInfo::CS_YUVA420PS: return "YUVA420PS"; 126 | // planar yuv411 127 | case VideoInfo::CS_YV411: return "YV411"; 128 | // others 129 | default: return "unknown"; 130 | } 131 | } 132 | 133 | 134 | const char* get_string_video_out(int pix_type) 135 | { 136 | switch (pix_type) { 137 | // gray 138 | case VideoInfo::CS_Y8: return "luma-only-8bit"; 139 | case VideoInfo::CS_Y10: return "luma-only-10bit"; 140 | case VideoInfo::CS_Y12: return "luma-only-12bit"; 141 | case VideoInfo::CS_Y14: return "luma-only-14bit"; 142 | case VideoInfo::CS_Y16: return "luma-only-16bit"; 143 | case VideoInfo::CS_Y32: return "luma-only-float"; 144 | // packed RGB 145 | case VideoInfo::CS_BGR32: return "BGRA-packed-8bit"; 146 | case VideoInfo::CS_BGR64: return "BGRA-packed-16bit"; 147 | case VideoInfo::CS_BGR24: return "BGR-packed-8bit"; 148 | case VideoInfo::CS_BGR48: return "BGR-packed-16bit"; 149 | // packed YUV 150 | case VideoInfo::CS_YUY2: return "YUYV-packed-8bit"; 151 | // planar RGB 152 | case VideoInfo::CS_RGBP: return "GBR-planar-8bit"; 153 | case VideoInfo::CS_RGBP10: return "GBR-planar-10bit"; 154 | case VideoInfo::CS_RGBP12: return "GBR-planar-12bit"; 155 | case VideoInfo::CS_RGBP14: return "GBR-planar-14bit"; 156 | case VideoInfo::CS_RGBP16: return "GBR-planar-16bit"; 157 | case VideoInfo::CS_RGBPS: return "GBR-planar-float"; 158 | // planar RGB with alpha 159 | case VideoInfo::CS_RGBAP: return "GBRA-planar-8bit"; 160 | case VideoInfo::CS_RGBAP10: return "GBRA-planar-10bit"; 161 | case VideoInfo::CS_RGBAP12: return "GBRA-planar-12bit"; 162 | case VideoInfo::CS_RGBAP14: return "GBRA-planar-14bit"; 163 | case VideoInfo::CS_RGBAP16: return "GBRA-planar-16bit"; 164 | case VideoInfo::CS_RGBAPS: return "GBRA-planar-float"; 165 | // planar yuv444 166 | case VideoInfo::CS_YV24: return "YUV-444-planar-8bit"; 167 | case VideoInfo::CS_YUV444P10: return "YUV-444-planar-10bit"; 168 | case VideoInfo::CS_YUV444P12: return "YUV-444-planar-12bit"; 169 | case VideoInfo::CS_YUV444P14: return "YUV-444-planar-14bit"; 170 | case VideoInfo::CS_YUV444P16: return "YUV-444-planar-16bit"; 171 | case VideoInfo::CS_YUV444PS: return "YUV-444-planar-float"; 172 | // planar yuv444 with alpha 173 | case VideoInfo::CS_YUVA444: return "YUVA-444-planar-8bit"; 174 | case VideoInfo::CS_YUVA444P10: return "YUVA-444-planar-10bit"; 175 | case VideoInfo::CS_YUVA444P12: return "YUVA-444-planar-12bit"; 176 | case VideoInfo::CS_YUVA444P14: return "YUVA-444-planar-14bit"; 177 | case VideoInfo::CS_YUVA444P16: return "YUVA-444-planar-16bit"; 178 | case VideoInfo::CS_YUVA444PS: return "YUVA-444-planar-float"; 179 | // planar yuv422 180 | case VideoInfo::CS_YV16: return "YUV-422-planar-8bit"; 181 | case VideoInfo::CS_YUV422P10: return "YUV-422-planar-10bit"; 182 | case VideoInfo::CS_YUV422P12: return "YUV-422-planar-12bit"; 183 | case VideoInfo::CS_YUV422P14: return "YUV-422-planar-14bit"; 184 | case VideoInfo::CS_YUV422P16: return "YUV-422-planar-16bit"; 185 | case VideoInfo::CS_YUV422PS: return "YUV-422-planar-float"; 186 | // planar yuv422 with alpha 187 | case VideoInfo::CS_YUVA422: return "YUVA-422-planar-8bit"; 188 | case VideoInfo::CS_YUVA422P10: return "YUVA-422-planar-10bit"; 189 | case VideoInfo::CS_YUVA422P12: return "YUVA-422-planar-12bit"; 190 | case VideoInfo::CS_YUVA422P14: return "YUVA-422-planar-14bit"; 191 | case VideoInfo::CS_YUVA422P16: return "YUVA-422-planar-16bit"; 192 | case VideoInfo::CS_YUVA422PS: return "YUVA-422-planar-float"; 193 | // planar yuv420 194 | case VideoInfo::CS_YV12: 195 | case VideoInfo::CS_I420: return "YUV-420-planar-8bit"; 196 | case VideoInfo::CS_YUV420P10: return "YUV-420-planar-10bit"; 197 | case VideoInfo::CS_YUV420P12: return "YUV-420-planar-12bit"; 198 | case VideoInfo::CS_YUV420P14: return "YUV-420-planar-14bit"; 199 | case VideoInfo::CS_YUV420P16: return "YUV-420-planar-16bit"; 200 | case VideoInfo::CS_YUV420PS: return "YUV-420-planar-float"; 201 | // planar yuv420 with alpha 202 | case VideoInfo::CS_YUVA420: return "YUVA-420-planar-8bit"; 203 | case VideoInfo::CS_YUVA420P10: return "YUVA-420-planar-10bit"; 204 | case VideoInfo::CS_YUVA420P12: return "YUVA-420-planar-12bit"; 205 | case VideoInfo::CS_YUVA420P14: return "YUVA-420-planar-14bit"; 206 | case VideoInfo::CS_YUVA420P16: return "YUVA-420-planar-16bit"; 207 | case VideoInfo::CS_YUVA420PS: return "YUVA-420-planar-float"; 208 | // planar yuv411 209 | case VideoInfo::CS_YV411: return "YUV-411-planar-8bit"; 210 | // others 211 | default: return "unknown"; 212 | } 213 | } 214 | 215 | 216 | const char* get_string_y4mheader(int pix_type, int chromaloc) 217 | { 218 | switch (pix_type) { 219 | case VideoInfo::CS_Y8: return "mono"; 220 | case VideoInfo::CS_Y16: return "mono16"; 221 | 222 | case VideoInfo::CS_YV411: return "411"; 223 | 224 | case VideoInfo::CS_YV12: 225 | case VideoInfo::CS_I420: 226 | switch (chromaloc) { 227 | case 0: return "420mpeg2"; 228 | case 1: return "420jpeg"; 229 | case 2: return "420paldv"; 230 | default: return "420mpeg2"; 231 | } 232 | case VideoInfo::CS_YUV420P10: return "420p10"; 233 | case VideoInfo::CS_YUV420P12: return "420p12"; 234 | case VideoInfo::CS_YUV420P14: return "420p14"; 235 | case VideoInfo::CS_YUV420P16: return "420p16"; 236 | 237 | case VideoInfo::CS_YV16: return "422"; 238 | case VideoInfo::CS_YUV422P10: return "422p10"; 239 | case VideoInfo::CS_YUV422P12: return "422p12"; 240 | case VideoInfo::CS_YUV422P14: return "422p14"; 241 | case VideoInfo::CS_YUV422P16: return "422p16"; 242 | 243 | case VideoInfo::CS_YV24: return "444"; 244 | case VideoInfo::CS_YUV444P10: return "444p10"; 245 | case VideoInfo::CS_YUV444P12: return "444p12"; 246 | case VideoInfo::CS_YUV444P14: return "444p14"; 247 | case VideoInfo::CS_YUV444P16: return "444p16"; 248 | 249 | case VideoInfo::CS_YUVA444: return "444alpha"; 250 | 251 | default: return nullptr; 252 | } 253 | } 254 | 255 | 256 | int get_sample_bits(int pixel_type) 257 | { 258 | int32_t bits = pixel_type & VideoInfo::CS_Sample_Bits_Mask; 259 | switch (bits) { 260 | case VideoInfo::CS_Sample_Bits_8: return 8; 261 | case VideoInfo::CS_Sample_Bits_10: return 10; 262 | case VideoInfo::CS_Sample_Bits_12: return 12; 263 | case VideoInfo::CS_Sample_Bits_14: return 14; 264 | case VideoInfo::CS_Sample_Bits_16: return 16; 265 | case VideoInfo::CS_Sample_Bits_32: return 32; 266 | } 267 | return 0; 268 | } 269 | 270 | int get_num_planes(int pixel_type) 271 | { 272 | if (pixel_type & VideoInfo::CS_INTERLEAVED) { 273 | return 1; 274 | } 275 | if (pixel_type & VideoInfo::CS_BGR) { 276 | return (pixel_type & VideoInfo::CS_RGBA_TYPE) ? 4 : 3; 277 | } 278 | return (pixel_type & VideoInfo::CS_YUVA) ? 4 : 3; 279 | } 280 | 281 | void convert_channelmask_to_string(uint32_t cm, std::string& cmstr) 282 | { 283 | cmstr = ""; 284 | struct { 285 | int mask; 286 | const char* pos; 287 | } array[] = { 288 | {MASK_SPEAKER_FRONT_LEFT, " FL"}, 289 | {MASK_SPEAKER_FRONT_RIGHT," FR"}, 290 | {MASK_SPEAKER_FRONT_CENTER, " FC"}, 291 | {MASK_SPEAKER_LOW_FREQUENCY, " LF"}, 292 | {MASK_SPEAKER_BACK_LEFT, " BL"}, 293 | {MASK_SPEAKER_BACK_RIGHT, " BR"}, 294 | {MASK_SPEAKER_FRONT_LEFT_OF_CENTER, " FLC"}, 295 | {MASK_SPEAKER_FRONT_RIGHT_OF_CENTER, " FRC"}, 296 | {MASK_SPEAKER_BACK_CENTER, " BC"}, 297 | {MASK_SPEAKER_SIDE_LEFT, " SL"}, 298 | {MASK_SPEAKER_SIDE_RIGHT, " SR"}, 299 | {MASK_SPEAKER_TOP_CENTER, " TC"}, 300 | {MASK_SPEAKER_TOP_FRONT_LEFT, " TFL"}, 301 | {MASK_SPEAKER_TOP_FRONT_CENTER, " TFC"}, 302 | {MASK_SPEAKER_TOP_FRONT_RIGHT, " TFR"}, 303 | {MASK_SPEAKER_TOP_BACK_LEFT, " TBL"}, 304 | {MASK_SPEAKER_TOP_BACK_CENTER, " TBC"}, 305 | {MASK_SPEAKER_TOP_BACK_RIGHT, " TBR"}, 306 | {0, nullptr}, 307 | }; 308 | for (int i = 0; array[i].mask != 0; ++i) { 309 | if (cm & array[i].mask) cmstr += array[i].pos; 310 | } 311 | if (cmstr.length() > 0) { 312 | cmstr = cmstr.substr(1); 313 | } 314 | } 315 | 316 | #if 0 317 | const char* get_string_filter(int pix_type) 318 | { 319 | switch (pix_type) { 320 | case VideoInfo::CS_BGR32: return "ConvertToYV24"; 321 | case VideoInfo::CS_BGR24: return "ConvertToYV24"; 322 | case VideoInfo::CS_YUY2: return "ConvertToYV16"; 323 | default: return nullptr; 324 | } 325 | } 326 | 327 | 328 | x264raw_t get_string_x264raw(int pix_type) 329 | { 330 | using std::make_tuple; 331 | 332 | const char* unkown = nullptr; 333 | 334 | switch (pix_type) { 335 | case VideoInfo::CS_BGR32: 336 | return make_tuple("raw", "bgra", "rgb"); 337 | case VideoInfo::CS_BGR24: 338 | return make_tuple("raw", "bgr", "rgb"); 339 | case VideoInfo::CS_YUY2: 340 | return make_tuple("lavf --input-fmt rawvideo", "yuyv422", "i422"); 341 | case VideoInfo::CS_YV24: 342 | case VideoInfo::CS_YUV444P16: 343 | return make_tuple("raw", "i444", "i444"); 344 | case VideoInfo::CS_YV16: 345 | case VideoInfo::CS_YUV422P16: 346 | return make_tuple("raw", "i422", "i422"); 347 | case VideoInfo::CS_YV12: 348 | case VideoInfo::CS_I420: 349 | case VideoInfo::CS_YUV420P16: 350 | return make_tuple("raw", "i420", "i420"); 351 | case VideoInfo::CS_Y8: 352 | return make_tuple("lavf --input-fmt rawvideo", "gray", "i420"); 353 | case VideoInfo::CS_Y16: 354 | return make_tuple("lavf --input-fmt rawvideo", "gray16", "i420"); 355 | default: 356 | return make_tuple(nullptr, nullptr, nullptr); 357 | } 358 | } 359 | #endif 360 | 361 | Buffer::Buffer(size_t size, size_t align) 362 | { 363 | buff = avs_malloc(size, align); 364 | if (!buff) { 365 | throw std::runtime_error("failed to allocate buffer."); 366 | } 367 | } 368 | 369 | Buffer::~Buffer() 370 | { 371 | avs_free(buff); 372 | buff = nullptr; 373 | } 374 | 375 | void* Buffer::data() 376 | { 377 | return buff; 378 | } 379 | 380 | -------------------------------------------------------------------------------- /src/avs2pipemod.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012-2016 Oka Motofumi 3 | * 4 | * This file is part of avs2pipemod. 5 | * 6 | * avs2pipemod is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * avs2pipemod is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with avs2pipemod. If not, see . 18 | * 19 | */ 20 | 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include "avs2pipemod.h" 33 | #include "utils.h" 34 | #include "wave.h" 35 | 36 | 37 | static const AVS_Linkage* AVS_linkage = nullptr; 38 | 39 | static inline int64_t get_current_time(void) 40 | { 41 | timeb tb; 42 | ftime(&tb); 43 | return (static_cast(tb.time) * 1000 + static_cast(tb.millitm)) * 1000; 44 | } 45 | 46 | 47 | Avs2PipeMod::Avs2PipeMod(HMODULE d, ise_t* e, PClip c, const char* in, Params& p) : 48 | dll(d), env(e), clip(c), input(in), versionString("unknown"), params(p) 49 | { 50 | vi = clip->GetVideoInfo(); 51 | 52 | auto v = env->Invoke("VersionNumber", AVSValue(nullptr, 0)); 53 | validate(!v.IsFloat(), "VersionNumber did not return a float value.\n"); 54 | version = static_cast(v.AsFloat()); 55 | 56 | v = env->Invoke("VersionString", AVSValue(nullptr, 0)); 57 | validate(!v.IsString(), "VersionString did not return string.\n"); 58 | versionString = v.AsString(); 59 | 60 | sampleBits = get_sample_bits(vi.pixel_type); 61 | numPlanes = get_num_planes(vi.pixel_type); 62 | params.channel_mask = 0; 63 | if (version > 3.72 && vi.IsChannelMaskKnown()) { 64 | params.channel_mask = vi.GetChannelMask(); 65 | } 66 | } 67 | 68 | 69 | Avs2PipeMod::~Avs2PipeMod() 70 | { 71 | clip.~PClip(); 72 | AVS_linkage = nullptr; 73 | env->DeleteScriptEnvironment(); 74 | FreeLibrary(dll); 75 | } 76 | 77 | 78 | void Avs2PipeMod:: 79 | invokeFilter(const char* filter, AVSValue args, const char** names) 80 | { 81 | a2pm_log(LOG_INFO, "invoking %s ...\n", filter); 82 | try { 83 | clip = env->Invoke(filter, args, names).AsClip(); 84 | vi = clip->GetVideoInfo(); 85 | } catch (AvisynthError& e) { 86 | throw std::runtime_error(e.msg); 87 | } 88 | } 89 | 90 | 91 | void Avs2PipeMod::trim() 92 | { 93 | if (params.trimstart == 0 && params.trimend == 0) { 94 | return; 95 | } 96 | 97 | AVSValue array[] = { clip, params.trimstart, params.trimend }; 98 | invokeFilter("Trim", AVSValue(array, 3)); 99 | } 100 | 101 | 102 | void Avs2PipeMod::info(bool act_info) 103 | { 104 | printf("\navisynth_version %.3f / %s\n", version, versionString); 105 | printf("script_name %s\n\n", input); 106 | 107 | if (vi.HasVideo()) { 108 | printf("v:width %d\n", vi.width); 109 | printf("v:height %d\n", vi.height); 110 | printf("v:image_type %s\n", 111 | vi.IsFieldBased() ? "fieldbased" : "framebased"); 112 | printf("v:field_order %s\n", 113 | vi.IsBFF() ? "assumed bottom field first" : 114 | vi.IsTFF() ? "assumed top field first" : "not specified"); 115 | printf("v:pixel_type %s\n", get_string_info(vi.pixel_type)); 116 | printf("v:bit_depth %d\n", sampleBits); 117 | printf("v:number of planes %d\n", numPlanes); 118 | printf("v:fps %u/%u\n", vi.fps_numerator, vi.fps_denominator); 119 | printf("v:frames %d\n", vi.num_frames); 120 | printf("v:duration[sec] %.3f\n\n", 121 | 1.0 * vi.num_frames * vi.fps_denominator / vi.fps_numerator); 122 | } 123 | 124 | if (vi.HasAudio() && act_info) { 125 | std::string cmstr; 126 | convert_channelmask_to_string(params.channel_mask, cmstr); 127 | if (cmstr == "") cmstr = "None"; 128 | 129 | printf("a:sample_rate %d\n", vi.audio_samples_per_second); 130 | printf("a:format %s\n", 131 | vi.sample_type == SAMPLE_FLOAT ? "float" : "integer"); 132 | printf("a:bit_depth %d\n", vi.BytesPerChannelSample() * 8); 133 | printf("a:channels %d\n", vi.AudioChannels()); 134 | printf("a:samples %" PRIi64 "\n", vi.num_audio_samples); 135 | printf("a:channel_mask %s\n", cmstr.c_str()); 136 | printf("a:duration[sec] %.3f\n\n", 137 | 1.0 * vi.num_audio_samples / vi.audio_samples_per_second); 138 | 139 | } 140 | } 141 | 142 | 143 | void Avs2PipeMod::benchmark() 144 | { 145 | constexpr int FRAMES_PER_OUT = 50; 146 | validate(!vi.HasVideo(), "clip has no video.\n"); 147 | trim(); 148 | info(false); 149 | 150 | int surplus = vi.num_frames % FRAMES_PER_OUT; 151 | int passed = 0; 152 | 153 | a2pm_log(LOG_INFO, "benchmarking %d frames video.\n", vi.num_frames); 154 | 155 | int64_t start = get_current_time(); 156 | int64_t elapsed; 157 | 158 | while (passed < surplus) { 159 | clip->GetFrame(passed++, env); 160 | } 161 | 162 | while (passed < vi.num_frames) { 163 | elapsed = get_current_time() - start; 164 | double de = elapsed * 0.000001; 165 | fprintf(stderr, "\r" 166 | "avs2pipemod[info]: [elapsed %.3f sec] %d/%d frames " 167 | "[%3d%%][%.3ffps]", 168 | de, passed, vi.num_frames, passed * 100 / vi.num_frames, 169 | passed / de); 170 | for (int n = 0; n < FRAMES_PER_OUT; ++n) { 171 | clip->GetFrame(passed++, env); 172 | } 173 | } 174 | 175 | elapsed = get_current_time() - start; 176 | 177 | fprintf(stderr, "\n"); 178 | printf("benchmark result: total elapsed time is %.3f sec [%.3ffps]\n", 179 | elapsed / 1000000.0, passed * 1000000.0 / elapsed); 180 | fflush(stdout); 181 | 182 | validate(passed != vi.num_frames, 183 | std::format("only passed {} of {} frames.\n", passed, vi.num_frames)); 184 | } 185 | 186 | 187 | static void write_audio_file_header(Params& pr, const VideoInfo& vi, 188 | float version) 189 | { 190 | WaveFormatType format = vi.sample_type == SAMPLE_FLOAT ? 191 | WAVE_FORMAT_IEEE_FLOAT : WAVE_FORMAT_PCM; 192 | wave_args_t args = { 193 | format, 194 | vi.nchannels, 195 | vi.audio_samples_per_second, 196 | vi.BytesPerChannelSample(), 197 | vi.num_audio_samples, 198 | pr.channel_mask, 199 | }; 200 | 201 | if (pr.format_type == FMT_WAVEFORMATEXTENSIBLE) { 202 | auto header = WaveRiffExtHeader(args); 203 | fwrite(&header, sizeof(header), 1, stdout); 204 | return; 205 | } 206 | if (pr.format_type == FMT_WAVEFORMATEX) { 207 | auto header = WaveRiffHeader(args); 208 | fwrite(&header, sizeof(header), 1, stdout); 209 | } 210 | } 211 | 212 | 213 | void Avs2PipeMod::outAudio() 214 | { 215 | validate(!vi.HasAudio(), "clip has no audio.\n"); 216 | trim(); 217 | 218 | validate(_setmode(_fileno(stdout), _O_BINARY) == -1, 219 | "cannot switch stdout to binary mode.\n"); 220 | 221 | if (params.bit) { 222 | auto filter = std::string("ConvertAudioTo") + params.bit; 223 | invokeFilter(filter.c_str(), clip); 224 | } 225 | 226 | size_t step = 0; 227 | size_t count = vi.audio_samples_per_second; 228 | size_t size = vi.BytesPerChannelSample() * vi.nchannels; 229 | uint64_t target = vi.num_audio_samples; 230 | auto buff = Buffer(size * count); 231 | void* data = buff.data(); 232 | 233 | a2pm_log(LOG_INFO, "writing %.3f seconds of %zu Hz, %d channel audio.\n", 234 | 1.0 * target / count, count, vi.nchannels); 235 | 236 | int64_t elapsed = get_current_time(); 237 | 238 | if (params.format_type != FMT_RAWAUDIO) { 239 | if (version > 3.72 && vi.IsChannelMaskKnown()) { 240 | params.channel_mask = vi.GetChannelMask(); 241 | } 242 | write_audio_file_header(params, vi, version); 243 | } 244 | 245 | clip->GetAudio(data, 0, target % count, env); 246 | uint64_t wrote = fwrite(data, size, target % count, stdout); 247 | 248 | while (wrote < target) { 249 | clip->GetAudio(data, wrote, count, env); 250 | step = fwrite(data, size, count, stdout); 251 | if (step != count) break; 252 | wrote += step; 253 | a2pm_log(LOG_REPEAT, "wrote %.3f seconds [%" PRIu64 "%%]", 254 | 1.0 * wrote / count, (100 * wrote) / target); 255 | } 256 | 257 | fflush(stdout); 258 | 259 | elapsed = get_current_time() - elapsed; 260 | 261 | a2pm_log(LOG_INFO, "total elapsed time is %.3f sec.\n", 262 | elapsed / 1000000.0); 263 | 264 | validate(wrote != target, 265 | std::format("only wrote {} of {} samples.\n", wrote, target)); 266 | } 267 | 268 | 269 | void Avs2PipeMod::prepareY4MOut() 270 | { 271 | if (vi.IsFieldBased()) { 272 | a2pm_log(LOG_WARNING, 273 | "clip is FieldBased.\n" 274 | "%19s yuv4mpeg2's spec doesn't support fieldbased clip.\n" 275 | "%19s choose what you want.\n" 276 | "%19s 1:add AssumeFrameBased() 2:add Weave() others:exit\n" 277 | "%19s input a number and press enter : ", 278 | "", "", "", ""); 279 | int k = fgetc(stdin); 280 | validate(k != '1' && k != '2', "good bye...\n"); 281 | invokeFilter(k == '1' ? "AssumeFrameBased" : "Weave", clip); 282 | } 283 | 284 | if (vi.IsYUY2()) { 285 | invokeFilter("ConvertToYV16", clip); 286 | } 287 | if (vi.IsRGB24() || vi.IsRGB32()) { 288 | const char* names[] = {nullptr, "interlaced", "matrix"}; 289 | AVSValue args[] = { 290 | clip, 291 | params.frame_type != 'p', 292 | vi.height < 720 ? "Rec601" : "Rec709" 293 | }; 294 | invokeFilter("ConvertToYV24", AVSValue(args, 3), names); 295 | } 296 | if (vi.IsRGB48() || vi.IsRGB64() || vi.IsPlanarRGB()) { 297 | throw std::runtime_error("unsupported format."); 298 | } 299 | if (sampleBits == 32 || (vi.IsY() && (sampleBits >= 10 && sampleBits < 16))) { 300 | invokeFilter("ConvertTo16bit", clip); 301 | } 302 | } 303 | 304 | 305 | static void set_frame_props(Params& p, PVideoFrame& vf, ise_t* env) 306 | { 307 | auto map = env->getFramePropsRO(vf); 308 | auto set_prop = [env, map](int& var, const char* key) { 309 | if (env->propGetType(map, key) == 'i') { 310 | var = env->propGetInt(map, key, 0, nullptr); 311 | } 312 | }; 313 | 314 | if (p.sarnum == 0) { 315 | set_prop(p.sarnum, "_SARNum"); 316 | set_prop(p.sarden, "_SARDen"); 317 | } 318 | set_prop(p.colorrange, "_ColorRange"); 319 | set_prop(p.colorprim, "_Primaries"); 320 | set_prop(p.transfer, "_Transfer"); 321 | set_prop(p.colormatrix, "_Matrix"); 322 | set_prop(p.chromaloc, "_ChromaLocation"); 323 | } 324 | 325 | 326 | template 327 | int Avs2PipeMod::writeFrames() 328 | { 329 | const int planes[] = { 330 | 0, 331 | vi.IsYUV() ? PLANAR_U : PLANAR_B, 332 | vi.IsYUV() ? PLANAR_V : PLANAR_R, 333 | PLANAR_A 334 | }; 335 | 336 | const size_t buffsize = vi.BitsPerPixel() * vi.width * vi.height / 8; 337 | auto b = Buffer(buffsize, 64); 338 | uint8_t* buff = reinterpret_cast(b.data()); 339 | 340 | auto frame = clip->GetFrame(0, env); 341 | int wrote = 0; 342 | 343 | if constexpr (Y4MOUT) { 344 | if (version >= 3.70) { 345 | set_frame_props(params, frame, env); 346 | } 347 | const char* range = ""; 348 | if (params.colorrange == 0) { 349 | range = " XCOLORRANGE=FULL"; 350 | } else if (params.colorrange == 1) { 351 | range = " XCOLORRANGE=LIMITED"; 352 | } 353 | const char* color = get_string_y4mheader(vi.pixel_type, params.chromaloc); 354 | auto header = std::format( 355 | "YUV4MPEG2 W{} H{} F{}:{} I{} A{}:{} C{}{}" 356 | " XCOLORPRIMARIES={} XTRANSFER={} XCOLORMATRIX={}", 357 | vi.width, vi.height, vi.fps_numerator, vi.fps_denominator, 358 | params.frame_type, params.sarnum, params.sarden, color, range, 359 | params.colorprim, params.transfer, params.colormatrix); 360 | puts(header.c_str()); 361 | } 362 | 363 | while (true) { 364 | if constexpr (Y4MOUT) { 365 | puts("FRAME"); 366 | } 367 | for (int p = 0; p < numPlanes; ++p) { 368 | int plane = planes[p]; 369 | const uint8_t* srcp = frame->GetReadPtr(plane); 370 | int rowsize = frame->GetRowSize(plane); 371 | int pitch = frame->GetPitch(plane); 372 | int height = frame->GetHeight(plane); 373 | size_t count = rowsize * height; 374 | size_t step = 0; 375 | 376 | if (rowsize == pitch) { 377 | step = fwrite(srcp, 1, count, stdout); 378 | } else { 379 | env->BitBlt(buff, rowsize, srcp, pitch, rowsize, height); 380 | step = fwrite(buff, 1, count, stdout); 381 | } 382 | if (step != count) { 383 | goto finish; 384 | } 385 | } 386 | if (++wrote >= vi.num_frames) break; 387 | frame = clip->GetFrame(wrote, env); 388 | } 389 | 390 | finish: 391 | fflush(stdout); 392 | return wrote; 393 | } 394 | 395 | 396 | void Avs2PipeMod::outVideo() 397 | { 398 | validate(!vi.HasVideo(), "clip has no video.\n"); 399 | trim(); 400 | 401 | validate(_setmode(_fileno(stdout), _O_BINARY) == -1, 402 | "cannot switch stdout to binary mode.\n"); 403 | 404 | if (params.format_type == FMT_RAWVIDEO_VFLIP) { 405 | invokeFilter("FlipVertical", clip); 406 | } 407 | 408 | bool y4mout = params.format_type == FMT_YUV4MPEG2; 409 | std::string msg; 410 | if (y4mout) { 411 | prepareY4MOut(); 412 | const char* type = params.frame_type == 'p' ? "progressive" : 413 | params.frame_type == 't' ? "tff" : "bff"; 414 | msg = std::format("writing {} frames of {}/{} fps, {}x{},\n", 415 | vi.num_frames, vi.fps_numerator, vi.fps_denominator, vi.width, 416 | vi.height); 417 | msg += std::format("{:18} sar {}:{}, {} {} video.\n", "", params.sarnum, 418 | params.sarden, get_string_video_out(vi.pixel_type), type); 419 | } else { 420 | msg = std::format("writing {} frames of {}x{} {} rawvideo.\n", 421 | vi.num_frames, vi.width, vi.height, 422 | get_string_video_out(vi.pixel_type)); 423 | } 424 | a2pm_log(LOG_INFO, msg.c_str()); 425 | 426 | int64_t elapsed = get_current_time(); 427 | 428 | int wrote = y4mout ? writeFrames() : writeFrames(); 429 | 430 | elapsed = get_current_time() - elapsed; 431 | 432 | a2pm_log(LOG_INFO, "finished, wrote %d frames [%d%%].\n", 433 | wrote, 100 * wrote / vi.num_frames); 434 | a2pm_log(LOG_INFO, "total elapsed time is %.3f sec.\n", 435 | elapsed / 1000000.0); 436 | 437 | validate(wrote != vi.num_frames, 438 | std::format("only wrote {} of {} frames.\n", wrote, vi.num_frames)); 439 | } 440 | 441 | 442 | template 443 | int Avs2PipeMod::writePixValuesAsText() 444 | { 445 | const int planes[] = { 446 | 0, 447 | vi.IsYUV() ? PLANAR_U : PLANAR_B, 448 | vi.IsYUV() ? PLANAR_V : PLANAR_R, 449 | PLANAR_A 450 | }; 451 | 452 | int wrote = 0; 453 | while (wrote < vi.num_frames) { 454 | auto frame = clip->GetFrame(wrote, env); 455 | fprintf(stdout, "frame %d\n", wrote); 456 | 457 | for (int p = 0; p < numPlanes; ++p) { 458 | int plane = planes[p]; 459 | const uint8_t* srcp = frame->GetReadPtr(plane); 460 | int width = frame->GetRowSize(plane) / sizeof(T); 461 | int height = frame->GetHeight(plane); 462 | int pitch = frame->GetPitch(plane); 463 | 464 | if (numPlanes > 1) { 465 | if (vi.IsYUV()) { 466 | puts(p == 0 ? "Y-plane" : p == 1 ? "U-plane" : p == 2 ? "V-plane" : "Alpha"); 467 | } else { 468 | puts(p == 0 ? "G-plane" : p == 1 ? "B-plane" : p == 2 ? "R-plane" : "Alpha"); 469 | } 470 | } 471 | 472 | for (int y = 0; y < height; ++y) { 473 | const T* s0 = reinterpret_cast(srcp); 474 | for (int x = 0; x < width; ++x) { 475 | if (sizeof(T) != 4) { 476 | fprintf(stdout, "%u\t", static_cast(s0[x])); 477 | } else { 478 | fprintf(stdout, "%.8f\t", static_cast(s0[x])); 479 | } 480 | } 481 | fputc('\n', stdout); 482 | srcp += pitch; 483 | } 484 | fputc('\n', stdout); 485 | } 486 | ++wrote; 487 | } 488 | fflush(stdout); 489 | return wrote; 490 | } 491 | 492 | 493 | void Avs2PipeMod::dumpPixValues() 494 | { 495 | validate(!vi.HasVideo(), "clip has no video.\n"); 496 | trim(); 497 | 498 | a2pm_log(LOG_INFO, 499 | "writing pixel values of %dx%dx%dframes to stdout as text.\n", 500 | vi.width, vi.height, vi.num_frames); 501 | 502 | info(false); 503 | puts("\n"); 504 | 505 | int wrote = 0; 506 | if (sampleBits == 8) { 507 | wrote = writePixValuesAsText(); 508 | } else if (sampleBits != 32) { 509 | wrote = writePixValuesAsText(); 510 | } else { 511 | wrote = writePixValuesAsText(); 512 | } 513 | 514 | a2pm_log(LOG_INFO, "finished, wrote %d frames [%d%%].\n", 515 | wrote, 100 * wrote / vi.num_frames); 516 | 517 | validate(wrote != vi.num_frames, std::format("only wrote {} of {} frames.\n", 518 | wrote, vi.num_frames)); 519 | } 520 | 521 | 522 | void Avs2PipeMod::dumpPluginFiltersList() 523 | { 524 | printf("\navisynth_version %.3f / %s\n", version, versionString); 525 | printf("script_name %s\n\n", input); 526 | 527 | try { 528 | auto filters = std::string(env->GetVar("$PluginFunctions$").AsString("")); 529 | size_t pos = filters.find(" "); 530 | while (pos != std::string::npos) { 531 | filters.replace(pos, 1, "\n"); 532 | pos = filters.find(" "); 533 | } 534 | printf("%s\n", filters.c_str()); 535 | } catch (...) { 536 | throw std::runtime_error("plugin funtions/filters not found."); 537 | } 538 | } 539 | 540 | static inline void procIntProp(std::stringstream& ss, const AVSMap* map, 541 | ise_t* env, const char* key) 542 | { 543 | auto elms = env->propNumElements(map, key); 544 | if (elms > 1) { 545 | ss << "["; 546 | for (auto i = 0; i < elms; ++i) { 547 | ss << env->propGetInt(map, key, i, nullptr); 548 | ss << (i < elms - 1 ? ", " : "]"); 549 | } 550 | } else { 551 | ss << env->propGetInt(map, key, 0, nullptr); 552 | } 553 | } 554 | 555 | static inline void procFloatProp(std::stringstream& ss, const AVSMap* map, 556 | ise_t* env, const char* key) 557 | { 558 | auto elms = env->propNumElements(map, key); 559 | if (elms > 1) { 560 | ss << "["; 561 | for (auto i = 0; i < elms; ++i) { 562 | ss << std::format("{:.07f}", 563 | env->propGetFloat(map, key, i, nullptr)) 564 | << (i < elms - 1 ? ", " : "]"); 565 | } 566 | } else { 567 | ss << std::format("{:.07f}", 568 | env->propGetFloat(map, key, 0, nullptr)); 569 | } 570 | } 571 | 572 | 573 | static inline void 574 | setData(std::stringstream& ss, const AVSMap* map, const char* key, int idx, 575 | ise_t* env) 576 | { 577 | auto data = env->propGetData(map, key, idx, nullptr); 578 | auto hint = env->propGetDataTypeHint(map, key, idx, nullptr); 579 | if (hint == AVSPropDataTypeHint::PROPDATATYPEHINT_BINARY) { 580 | auto size = env->propGetDataSize(map, key, idx, nullptr); 581 | auto length = std::min(size, 16); 582 | std::vector v(data, data + length); 583 | ss << "\""; 584 | for (auto j = 0; j < length; ++j) { 585 | ss << std::format("{:02x} ", v[j]); 586 | } 587 | ss << (length < size ? "...\"" : "\""); 588 | } else { 589 | ss << std::format("\"{}\"", data); 590 | } 591 | } 592 | 593 | 594 | static inline void procDataProp(std::stringstream& ss, const AVSMap* map, 595 | ise_t* env, const char* key) 596 | { 597 | auto elms = env->propNumElements(map, key); 598 | if (elms > 1) { 599 | ss << "["; 600 | for (auto i = 0; i < elms; ++i) { 601 | setData(ss, map, key, i, env); 602 | ss << (i < elms - 1 ? ", " : "]"); 603 | } 604 | } else { 605 | setData(ss, map, key, 0, env); 606 | } 607 | } 608 | 609 | 610 | void Avs2PipeMod::dumpFrameProps() 611 | { 612 | validate(version < 3.70, "frame properties does not exists.\n"); 613 | validate(!vi.HasVideo(), "clip has no video.\n"); 614 | trim(); 615 | 616 | auto setProp = [](const char* key, const std::stringstream& val, 617 | const char* comma, std::stringstream& line) { 618 | line << std::format("\"{}\": {}{}", key, val.str(), comma); 619 | }; 620 | 621 | int passed = 0; 622 | fputs("[\n", stdout); 623 | while (passed < vi.num_frames) { 624 | auto frame = clip->GetFrame(passed++, env); 625 | auto map = env->getFramePropsRO(frame); 626 | auto num = env->propNumKeys(map); 627 | 628 | std::stringstream line; 629 | line << "\t{"; 630 | for (auto i = 0; i < num; ++i) { 631 | auto key = env->propGetKey(map, i); 632 | auto t = env->propGetType(map, key); 633 | auto comma = i == num - 1 ? "" : ", "; 634 | std::stringstream ss; 635 | if (t == 'i') { 636 | procIntProp(ss, map, env, key); 637 | setProp(key, ss, comma, line); 638 | continue; 639 | } 640 | if (t == 'f') { 641 | procFloatProp(ss, map, env, key); 642 | setProp(key, ss, comma, line); 643 | continue; 644 | } 645 | if (t == 's') { 646 | procDataProp(ss, map, env, key); 647 | setProp(key, ss, comma, line); 648 | } 649 | } 650 | if (vi.num_frames == passed) { 651 | line << "}\n"; 652 | } else { 653 | line << "},\n"; 654 | } 655 | fputs(line.str().c_str(), stdout); 656 | 657 | fprintf(stderr, "\ravs2pipemod[info]: output %d/%d frame properties.", 658 | passed, vi.num_frames); 659 | } 660 | fputs("]\n", stdout); 661 | fputs("\n", stderr); 662 | fflush(stdout); 663 | 664 | validate(passed != vi.num_frames, 665 | std::format("only output {} of {} frame properties.\n", passed, 666 | vi.num_frames)); 667 | } 668 | 669 | 670 | Avs2PipeMod* Avs2PipeMod::create(const char* input, Params& p) 671 | { 672 | typedef ise_t* (__stdcall *cse_t)(int); 673 | constexpr int AVS_INTERFACE_VERSION = 11; 674 | 675 | HMODULE dll = nullptr; 676 | ise_t* env = nullptr; 677 | 678 | try { 679 | const char* d = p.dll_path ? p.dll_path : "avisynth"; 680 | dll = LoadLibraryExA(d , nullptr, LOAD_WITH_ALTERED_SEARCH_PATH); 681 | validate(!dll, "failed to load avisynth.dll\n"); 682 | 683 | auto pa = GetProcAddress(dll, "CreateScriptEnvironment"); 684 | validate(!pa, "failed to load avisynth.dll\n"); 685 | cse_t create_env = reinterpret_cast(pa); 686 | 687 | env = create_env(AVS_INTERFACE_VERSION); 688 | validate(!env, "failed to create avisynth script environment.\n"); 689 | 690 | AVS_linkage = env->GetAVSLinkage(); 691 | 692 | AVSValue res = env->Invoke("Import", AVSValue(input)); 693 | validate(!res.IsClip(), "Script didn't return a clip.\n"); 694 | 695 | return new Avs2PipeMod(dll, env, res.AsClip(), input, p); 696 | 697 | } catch (std::exception& e) { 698 | AVS_linkage = nullptr; 699 | if (env) { 700 | env->DeleteScriptEnvironment(); 701 | } 702 | if (dll) { 703 | FreeLibrary(dll); 704 | } 705 | throw e; 706 | } catch (AvisynthError e) { 707 | fprintf(stderr, "avs2pipemod[error]: %s\n", e.msg); 708 | AVS_linkage = nullptr; 709 | env->DeleteScriptEnvironment(); 710 | FreeLibrary(dll); 711 | exit(-1); 712 | } catch (...) { 713 | throw std::runtime_error("unkown exception.\n"); 714 | } 715 | 716 | return nullptr; 717 | } 718 | 719 | -------------------------------------------------------------------------------- /src/getopt.c: -------------------------------------------------------------------------------- 1 | /* Getopt for Microsoft C 2 | This code is a modification of the Free Software Foundation, Inc. 3 | Getopt library for parsing command line argument the purpose was 4 | to provide a Microsoft Visual C friendly derivative. This code 5 | provides functionality for both Unicode and Multibyte builds. 6 | 7 | Date: 02/03/2011 - Ludvik Jerabek - Initial Release 8 | Version: 1.1 9 | Comment: Supports getopt, getopt_long, and getopt_long_only 10 | and POSIXLY_CORRECT environment flag 11 | License: LGPL 12 | 13 | Revisions: 14 | 15 | 02/03/2011 - Ludvik Jerabek - Initial Release 16 | 02/20/2011 - Ludvik Jerabek - Fixed compiler warnings at Level 4 17 | 07/05/2011 - Ludvik Jerabek - Added no_argument, required_argument, optional_argument defs 18 | 08/03/2011 - Ludvik Jerabek - Fixed non-argument runtime bug which caused runtime exception 19 | 08/09/2011 - Ludvik Jerabek - Added code to export functions for DLL and LIB 20 | 02/15/2012 - Ludvik Jerabek - Fixed _GETOPT_THROW definition missing in implementation file 21 | 08/01/2012 - Ludvik Jerabek - Created separate functions for char and wchar_t characters so single dll can do both unicode and ansi 22 | 10/15/2012 - Ludvik Jerabek - Modified to match latest GNU features 23 | 06/19/2015 - Ludvik Jerabek - Fixed maximum option limitation caused by option_a (255) and option_w (65535) structure val variable 24 | 09/24/2022 - Ludvik Jerabek - Updated to match most recent getopt release 25 | 09/25/2022 - Ludvik Jerabek - Fixed memory allocation (malloc call) issue for wchar_t* 26 | 27 | **DISCLAIMER** 28 | THIS MATERIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 29 | EITHER EXPRESS OR IMPLIED, INCLUDING, BUT Not LIMITED TO, THE 30 | IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 31 | PURPOSE, OR NON-INFRINGEMENT. SOME JURISDICTIONS DO NOT ALLOW THE 32 | EXCLUSION OF IMPLIED WARRANTIES, SO THE ABOVE EXCLUSION MAY NOT 33 | APPLY TO YOU. IN NO EVENT WILL I BE LIABLE TO ANY PARTY FOR ANY 34 | DIRECT, INDIRECT, SPECIAL OR OTHER CONSEQUENTIAL DAMAGES FOR ANY 35 | USE OF THIS MATERIAL INCLUDING, WITHOUT LIMITATION, ANY LOST 36 | PROFITS, BUSINESS INTERRUPTION, LOSS OF PROGRAMS OR OTHER DATA ON 37 | YOUR INFORMATION HANDLING SYSTEM OR OTHERWISE, EVEN If WE ARE 38 | EXPRESSLY ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 39 | */ 40 | #define _CRT_SECURE_NO_WARNINGS 41 | 42 | #include 43 | #include 44 | #include 45 | #include 46 | #include "getopt.h" 47 | 48 | #ifdef __cplusplus 49 | #define _GETOPT_THROW throw() 50 | #else 51 | #define _GETOPT_THROW 52 | #endif 53 | 54 | int optind = 1; 55 | int opterr = 1; 56 | int optopt = '?'; 57 | enum ENUM_ORDERING { REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER }; 58 | 59 | // 60 | // 61 | // Ansi structures and functions follow 62 | // 63 | // 64 | 65 | static struct _getopt_data_a 66 | { 67 | int optind; 68 | int opterr; 69 | int optopt; 70 | char* optarg; 71 | int __initialized; 72 | char* __nextchar; 73 | enum ENUM_ORDERING __ordering; 74 | int __first_nonopt; 75 | int __last_nonopt; 76 | } getopt_data_a; 77 | char* optarg_a; 78 | 79 | static void exchange_a(char** argv, struct _getopt_data_a* d) 80 | { 81 | int bottom = d->__first_nonopt; 82 | int middle = d->__last_nonopt; 83 | int top = d->optind; 84 | char* tem; 85 | while (top > middle && middle > bottom) 86 | { 87 | if (top - middle > middle - bottom) 88 | { 89 | int len = middle - bottom; 90 | int i; 91 | for (i = 0; i < len; i++) 92 | { 93 | tem = argv[bottom + i]; 94 | argv[bottom + i] = argv[top - (middle - bottom) + i]; 95 | argv[top - (middle - bottom) + i] = tem; 96 | } 97 | top -= len; 98 | } 99 | else 100 | { 101 | int len = top - middle; 102 | int i; 103 | for (i = 0; i < len; i++) 104 | { 105 | tem = argv[bottom + i]; 106 | argv[bottom + i] = argv[middle + i]; 107 | argv[middle + i] = tem; 108 | } 109 | bottom += len; 110 | } 111 | } 112 | d->__first_nonopt += (d->optind - d->__last_nonopt); 113 | d->__last_nonopt = d->optind; 114 | } 115 | 116 | static int process_long_option_a(int argc, char** argv, const char* optstring, const struct option_a* longopts, int* longind, int long_only, struct _getopt_data_a* d, int print_errors, const char* prefix) 117 | { 118 | assert(longopts != NULL); 119 | char* nameend; 120 | size_t namelen; 121 | const struct option_a* p; 122 | const struct option_a* pfound = NULL; 123 | int n_options; 124 | int option_index = 0; 125 | for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++); 126 | namelen = nameend - d->__nextchar; 127 | for (p = longopts, n_options = 0; p->name; p++, n_options++) 128 | if (!strncmp(p->name, d->__nextchar, namelen) && namelen == strlen(p->name)) 129 | { 130 | pfound = p; 131 | option_index = n_options; 132 | break; 133 | } 134 | if (pfound == NULL) 135 | { 136 | unsigned char* ambig_set = NULL; 137 | int ambig_fallback = 0; 138 | int indfound = -1; 139 | for (p = longopts, option_index = 0; p->name; p++, option_index++) 140 | if (!strncmp(p->name, d->__nextchar, namelen)) 141 | { 142 | if (pfound == NULL) 143 | { 144 | pfound = p; 145 | indfound = option_index; 146 | } 147 | else if (long_only 148 | || pfound->has_arg != p->has_arg 149 | || pfound->flag != p->flag 150 | || pfound->val != p->val) 151 | { 152 | if (!ambig_fallback) 153 | { 154 | if (!print_errors) 155 | ambig_fallback = 1; 156 | 157 | else if (!ambig_set) 158 | { 159 | if ((ambig_set = malloc(n_options * sizeof(char))) == NULL) 160 | ambig_fallback = 1; 161 | 162 | if (ambig_set) 163 | { 164 | memset(ambig_set, 0, n_options * sizeof(char)); 165 | ambig_set[indfound] = 1; 166 | } 167 | } 168 | if (ambig_set) 169 | ambig_set[option_index] = 1; 170 | } 171 | } 172 | } 173 | if (ambig_set || ambig_fallback) 174 | { 175 | if (print_errors) 176 | { 177 | if (ambig_fallback) 178 | fprintf(stderr, "%s: option '%s%s' is ambiguous\n", argv[0], prefix, d->__nextchar); 179 | else 180 | { 181 | _lock_file(stderr); 182 | fprintf(stderr, "%s: option '%s%s' is ambiguous; possibilities:", argv[0], prefix, d->__nextchar); 183 | for (option_index = 0; option_index < n_options; option_index++) 184 | if (ambig_set[option_index]) 185 | fprintf(stderr, " '%s%s'", prefix, longopts[option_index].name); 186 | fprintf(stderr, "\n"); 187 | _unlock_file(stderr); 188 | } 189 | } 190 | free(ambig_set); 191 | d->__nextchar += strlen(d->__nextchar); 192 | d->optind++; 193 | d->optopt = 0; 194 | return '?'; 195 | } 196 | option_index = indfound; 197 | } 198 | if (pfound == NULL) 199 | { 200 | if (!long_only || argv[d->optind][1] == '-' 201 | || strchr(optstring, *d->__nextchar) == NULL) 202 | { 203 | if (print_errors) 204 | fprintf(stderr, "%s: unrecognized option '%s%s'\n", argv[0], prefix, d->__nextchar); 205 | d->__nextchar = NULL; 206 | d->optind++; 207 | d->optopt = 0; 208 | return '?'; 209 | } 210 | return -1; 211 | } 212 | d->optind++; 213 | d->__nextchar = NULL; 214 | if (*nameend) 215 | { 216 | if (pfound->has_arg) 217 | d->optarg = nameend + 1; 218 | else 219 | { 220 | if (print_errors) 221 | fprintf(stderr, "%s: option '%s%s' doesn't allow an argument\n", argv[0], prefix, pfound->name); 222 | d->optopt = pfound->val; 223 | return '?'; 224 | } 225 | } 226 | else if (pfound->has_arg == 1) 227 | { 228 | if (d->optind < argc) 229 | d->optarg = argv[d->optind++]; 230 | else 231 | { 232 | if (print_errors) 233 | fprintf(stderr, "%s: option '%s%s' requires an argument\n", argv[0], prefix, pfound->name); 234 | d->optopt = pfound->val; 235 | return optstring[0] == ':' ? ':' : '?'; 236 | } 237 | } 238 | if (longind != NULL) 239 | *longind = option_index; 240 | 241 | if (pfound->flag) 242 | { 243 | *(pfound->flag) = pfound->val; 244 | return 0; 245 | } 246 | return pfound->val; 247 | } 248 | 249 | static const char* _getopt_initialize_a(const char* optstring, struct _getopt_data_a* d, int posixly_correct) 250 | { 251 | if (d->optind == 0) 252 | d->optind = 1; 253 | 254 | d->__first_nonopt = d->__last_nonopt = d->optind; 255 | d->__nextchar = NULL; 256 | 257 | if (optstring[0] == '-') 258 | { 259 | d->__ordering = RETURN_IN_ORDER; 260 | ++optstring; 261 | } 262 | else if (optstring[0] == '+') 263 | { 264 | d->__ordering = REQUIRE_ORDER; 265 | ++optstring; 266 | } 267 | else if (posixly_correct | !!getenv("POSIXLY_CORRECT")) 268 | d->__ordering = REQUIRE_ORDER; 269 | else 270 | d->__ordering = PERMUTE; 271 | 272 | d->__initialized = 1; 273 | return optstring; 274 | } 275 | 276 | int _getopt_internal_r_a(int argc, char* const* argv, const char* optstring, const struct option_a* longopts, int* longind, int long_only, struct _getopt_data_a* d, int posixly_correct) 277 | { 278 | int print_errors = d->opterr; 279 | if (argc < 1) 280 | return -1; 281 | d->optarg = NULL; 282 | if (d->optind == 0 || !d->__initialized) 283 | optstring = _getopt_initialize_a(optstring, d, posixly_correct); 284 | else if (optstring[0] == '-' || optstring[0] == '+') 285 | optstring++; 286 | if (optstring[0] == ':') 287 | print_errors = 0; 288 | 289 | if (d->__nextchar == NULL || *d->__nextchar == '\0') 290 | { 291 | if (d->__last_nonopt > d->optind) 292 | d->__last_nonopt = d->optind; 293 | if (d->__first_nonopt > d->optind) 294 | d->__first_nonopt = d->optind; 295 | if (d->__ordering == PERMUTE) 296 | { 297 | if (d->__first_nonopt != d->__last_nonopt 298 | && d->__last_nonopt != d->optind) 299 | exchange_a((char**)argv, d); 300 | else if (d->__last_nonopt != d->optind) 301 | d->__first_nonopt = d->optind; 302 | while (d->optind < argc && (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')) 303 | d->optind++; 304 | d->__last_nonopt = d->optind; 305 | } 306 | if (d->optind != argc && !strcmp(argv[d->optind], "--")) 307 | { 308 | d->optind++; 309 | if (d->__first_nonopt != d->__last_nonopt 310 | && d->__last_nonopt != d->optind) 311 | exchange_a((char**)argv, d); 312 | else if (d->__first_nonopt == d->__last_nonopt) 313 | d->__first_nonopt = d->optind; 314 | d->__last_nonopt = argc; 315 | d->optind = argc; 316 | } 317 | if (d->optind == argc) 318 | { 319 | if (d->__first_nonopt != d->__last_nonopt) 320 | d->optind = d->__first_nonopt; 321 | return -1; 322 | } 323 | if (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0') 324 | { 325 | if (d->__ordering == REQUIRE_ORDER) 326 | return -1; 327 | d->optarg = argv[d->optind++]; 328 | return 1; 329 | } 330 | if (longopts) 331 | { 332 | if (argv[d->optind][1] == '-') 333 | { 334 | d->__nextchar = argv[d->optind] + 2; 335 | return process_long_option_a(argc, (char**)argv, optstring, longopts, longind, long_only, d, print_errors, "--"); 336 | } 337 | if (long_only && (argv[d->optind][2] 338 | || !strchr(optstring, argv[d->optind][1]))) 339 | { 340 | int code; 341 | d->__nextchar = argv[d->optind] + 1; 342 | code = process_long_option_a(argc, (char**)argv, optstring, longopts, 343 | longind, long_only, d, 344 | print_errors, "-"); 345 | if (code != -1) 346 | return code; 347 | } 348 | } 349 | d->__nextchar = argv[d->optind] + 1; 350 | } 351 | { 352 | char c = *d->__nextchar++; 353 | const char* temp = strchr(optstring, c); 354 | if (*d->__nextchar == '\0') 355 | ++d->optind; 356 | if (temp == NULL || c == ':' || c == ';') 357 | { 358 | if (print_errors) 359 | fprintf(stderr, "%s: invalid option -- '%c'\n", argv[0], c); 360 | d->optopt = c; 361 | return '?'; 362 | } 363 | if (temp[0] == 'W' && temp[1] == ';' && longopts != NULL) 364 | { 365 | if (*d->__nextchar != '\0') 366 | d->optarg = d->__nextchar; 367 | else if (d->optind == argc) 368 | { 369 | if (print_errors) 370 | fprintf(stderr, "%s: option requires an argument -- '%c'\n", argv[0], c); 371 | d->optopt = c; 372 | if (optstring[0] == ':') 373 | c = ':'; 374 | else 375 | c = '?'; 376 | return c; 377 | } 378 | else 379 | d->optarg = argv[d->optind]; 380 | d->__nextchar = d->optarg; 381 | d->optarg = NULL; 382 | return process_long_option_a(argc, (char**)argv, optstring, longopts, longind, 0, d, print_errors, "-W "); 383 | } 384 | if (temp[1] == ':') 385 | { 386 | if (temp[2] == ':') 387 | { 388 | if (*d->__nextchar != '\0') 389 | { 390 | d->optarg = d->__nextchar; 391 | d->optind++; 392 | } 393 | else 394 | d->optarg = NULL; 395 | d->__nextchar = NULL; 396 | } 397 | else 398 | { 399 | if (*d->__nextchar != '\0') 400 | { 401 | d->optarg = d->__nextchar; 402 | d->optind++; 403 | } 404 | else if (d->optind == argc) 405 | { 406 | if (print_errors) 407 | fprintf(stderr, "%s: option requires an argument -- '%c'\n", argv[0], c); 408 | d->optopt = c; 409 | if (optstring[0] == ':') 410 | c = ':'; 411 | else 412 | c = '?'; 413 | } 414 | else 415 | d->optarg = argv[d->optind++]; 416 | d->__nextchar = NULL; 417 | } 418 | } 419 | return c; 420 | } 421 | } 422 | 423 | int _getopt_internal_a(int argc, char* const* argv, const char* optstring, const struct option_a* longopts, int* longind, int long_only, int posixly_correct) 424 | { 425 | int result; 426 | getopt_data_a.optind = optind; 427 | getopt_data_a.opterr = opterr; 428 | result = _getopt_internal_r_a(argc, argv, optstring, longopts, longind, long_only, &getopt_data_a, posixly_correct); 429 | optind = getopt_data_a.optind; 430 | optarg_a = getopt_data_a.optarg; 431 | optopt = getopt_data_a.optopt; 432 | return result; 433 | } 434 | 435 | int getopt_a(int argc, char* const* argv, const char* optstring) _GETOPT_THROW 436 | { 437 | return _getopt_internal_a(argc, argv, optstring, (const struct option_a*)0, (int*)0, 0, 0); 438 | } 439 | 440 | int getopt_long_a(int argc, char* const* argv, const char* options, const struct option_a* long_options, int* opt_index) _GETOPT_THROW 441 | { 442 | return _getopt_internal_a(argc, argv, options, long_options, opt_index, 0, 0); 443 | } 444 | 445 | int getopt_long_only_a(int argc, char* const* argv, const char* options, const struct option_a* long_options, int* opt_index) _GETOPT_THROW 446 | { 447 | return _getopt_internal_a(argc, argv, options, long_options, opt_index, 1, 0); 448 | } 449 | 450 | int _getopt_long_r_a(int argc, char* const* argv, const char* options, const struct option_a* long_options, int* opt_index, struct _getopt_data_a* d) 451 | { 452 | return _getopt_internal_r_a(argc, argv, options, long_options, opt_index, 0, d, 0); 453 | } 454 | 455 | int _getopt_long_only_r_a(int argc, char* const* argv, const char* options, const struct option_a* long_options, int* opt_index, struct _getopt_data_a* d) 456 | { 457 | return _getopt_internal_r_a(argc, argv, options, long_options, opt_index, 1, d, 0); 458 | } 459 | 460 | // 461 | // 462 | // Unicode Structures and Functions 463 | // 464 | // 465 | 466 | static struct _getopt_data_w 467 | { 468 | int optind; 469 | int opterr; 470 | int optopt; 471 | wchar_t* optarg; 472 | int __initialized; 473 | wchar_t* __nextchar; 474 | enum ENUM_ORDERING __ordering; 475 | int __first_nonopt; 476 | int __last_nonopt; 477 | } getopt_data_w; 478 | wchar_t* optarg_w; 479 | 480 | static void exchange_w(wchar_t** argv, struct _getopt_data_w* d) 481 | { 482 | int bottom = d->__first_nonopt; 483 | int middle = d->__last_nonopt; 484 | int top = d->optind; 485 | wchar_t* tem; 486 | while (top > middle && middle > bottom) 487 | { 488 | if (top - middle > middle - bottom) 489 | { 490 | int len = middle - bottom; 491 | int i; 492 | for (i = 0; i < len; i++) 493 | { 494 | tem = argv[bottom + i]; 495 | argv[bottom + i] = argv[top - (middle - bottom) + i]; 496 | argv[top - (middle - bottom) + i] = tem; 497 | } 498 | top -= len; 499 | } 500 | else 501 | { 502 | int len = top - middle; 503 | int i; 504 | for (i = 0; i < len; i++) 505 | { 506 | tem = argv[bottom + i]; 507 | argv[bottom + i] = argv[middle + i]; 508 | argv[middle + i] = tem; 509 | } 510 | bottom += len; 511 | } 512 | } 513 | d->__first_nonopt += (d->optind - d->__last_nonopt); 514 | d->__last_nonopt = d->optind; 515 | } 516 | 517 | static int process_long_option_w(int argc, wchar_t** argv, const wchar_t* optstring, const struct option_w* longopts, int* longind, int long_only, struct _getopt_data_w* d, int print_errors, const wchar_t* prefix) 518 | { 519 | assert(longopts != NULL); 520 | wchar_t* nameend; 521 | size_t namelen; 522 | const struct option_w* p; 523 | const struct option_w* pfound = NULL; 524 | int n_options; 525 | int option_index = 0; 526 | for (nameend = d->__nextchar; *nameend && *nameend != L'='; nameend++); 527 | namelen = nameend - d->__nextchar; 528 | for (p = longopts, n_options = 0; p->name; p++, n_options++) 529 | if (!wcsncmp(p->name, d->__nextchar, namelen) && namelen == wcslen(p->name)) 530 | { 531 | pfound = p; 532 | option_index = n_options; 533 | break; 534 | } 535 | if (pfound == NULL) 536 | { 537 | wchar_t* ambig_set = NULL; 538 | int ambig_fallback = 0; 539 | int indfound = -1; 540 | for (p = longopts, option_index = 0; p->name; p++, option_index++) 541 | if (!wcsncmp(p->name, d->__nextchar, namelen)) 542 | { 543 | if (pfound == NULL) 544 | { 545 | pfound = p; 546 | indfound = option_index; 547 | } 548 | else if (long_only 549 | || pfound->has_arg != p->has_arg 550 | || pfound->flag != p->flag 551 | || pfound->val != p->val) 552 | { 553 | if (!ambig_fallback) 554 | { 555 | if (!print_errors) 556 | ambig_fallback = 1; 557 | 558 | else if (!ambig_set) 559 | { 560 | if ((ambig_set = malloc(n_options * sizeof(wchar_t))) == NULL) 561 | ambig_fallback = 1; 562 | 563 | if (ambig_set) 564 | { 565 | memset(ambig_set, 0, n_options * sizeof(wchar_t)); 566 | ambig_set[indfound] = 1; 567 | } 568 | } 569 | if (ambig_set) 570 | ambig_set[option_index] = 1; 571 | } 572 | } 573 | } 574 | if (ambig_set || ambig_fallback) 575 | { 576 | if (print_errors) 577 | { 578 | if (ambig_fallback) 579 | fwprintf(stderr, L"%s: option '%s%s' is ambiguous\n", argv[0], prefix, d->__nextchar); 580 | else 581 | { 582 | _lock_file(stderr); 583 | fwprintf(stderr, L"%s: option '%s%s' is ambiguous; possibilities:", argv[0], prefix, d->__nextchar); 584 | for (option_index = 0; option_index < n_options; option_index++) 585 | if (ambig_set[option_index]) 586 | fwprintf(stderr, L" '%s%s'", prefix, longopts[option_index].name); 587 | fwprintf(stderr, L"\n"); 588 | _unlock_file(stderr); 589 | } 590 | } 591 | free(ambig_set); 592 | d->__nextchar += wcslen(d->__nextchar); 593 | d->optind++; 594 | d->optopt = 0; 595 | return L'?'; 596 | } 597 | option_index = indfound; 598 | } 599 | if (pfound == NULL) 600 | { 601 | if (!long_only || argv[d->optind][1] == L'-' || wcschr(optstring, *d->__nextchar) == NULL) 602 | { 603 | if (print_errors) 604 | fwprintf(stderr, L"%s: unrecognized option '%s%s'\n", argv[0], prefix, d->__nextchar); 605 | d->__nextchar = NULL; 606 | d->optind++; 607 | d->optopt = 0; 608 | return L'?'; 609 | } 610 | return -1; 611 | } 612 | d->optind++; 613 | d->__nextchar = NULL; 614 | if (*nameend) 615 | { 616 | if (pfound->has_arg) 617 | d->optarg = nameend + 1; 618 | else 619 | { 620 | if (print_errors) 621 | fwprintf(stderr, L"%s: option '%s%s' doesn't allow an argument\n", argv[0], prefix, pfound->name); 622 | d->optopt = pfound->val; 623 | return L'?'; 624 | } 625 | } 626 | else if (pfound->has_arg == 1) 627 | { 628 | if (d->optind < argc) 629 | d->optarg = argv[d->optind++]; 630 | else 631 | { 632 | if (print_errors) 633 | fwprintf(stderr, L"%s: option '%s%s' requires an argument\n", argv[0], prefix, pfound->name); 634 | d->optopt = pfound->val; 635 | return optstring[0] == L':' ? L':' : L'?'; 636 | } 637 | } 638 | if (longind != NULL) 639 | *longind = option_index; 640 | if (pfound->flag) 641 | { 642 | *(pfound->flag) = pfound->val; 643 | return 0; 644 | } 645 | return pfound->val; 646 | } 647 | 648 | static const wchar_t* _getopt_initialize_w(const wchar_t* optstring, struct _getopt_data_w* d, int posixly_correct) 649 | { 650 | if (d->optind == 0) 651 | d->optind = 1; 652 | 653 | d->__first_nonopt = d->__last_nonopt = d->optind; 654 | d->__nextchar = NULL; 655 | 656 | if (optstring[0] == L'-') 657 | { 658 | d->__ordering = RETURN_IN_ORDER; 659 | ++optstring; 660 | } 661 | else if (optstring[0] == L'+') 662 | { 663 | d->__ordering = REQUIRE_ORDER; 664 | ++optstring; 665 | } 666 | else if (posixly_correct | !!_wgetenv(L"POSIXLY_CORRECT")) 667 | d->__ordering = REQUIRE_ORDER; 668 | else 669 | d->__ordering = PERMUTE; 670 | 671 | d->__initialized = 1; 672 | return optstring; 673 | } 674 | 675 | int _getopt_internal_r_w(int argc, wchar_t* const* argv, const wchar_t* optstring, const struct option_w* longopts, int* longind, int long_only, struct _getopt_data_w* d, int posixly_correct) 676 | { 677 | int print_errors = d->opterr; 678 | if (argc < 1) 679 | return -1; 680 | d->optarg = NULL; 681 | if (d->optind == 0 || !d->__initialized) 682 | optstring = _getopt_initialize_w(optstring, d, posixly_correct); 683 | else if (optstring[0] == L'-' || optstring[0] == L'+') 684 | optstring++; 685 | if (optstring[0] == L':') 686 | print_errors = 0; 687 | #define NONOPTION_P (argv[d->optind][0] != L'-' || argv[d->optind][1] == L'\0') 688 | 689 | if (d->__nextchar == NULL || *d->__nextchar == L'\0') 690 | { 691 | if (d->__last_nonopt > d->optind) 692 | d->__last_nonopt = d->optind; 693 | if (d->__first_nonopt > d->optind) 694 | d->__first_nonopt = d->optind; 695 | if (d->__ordering == PERMUTE) 696 | { 697 | if (d->__first_nonopt != d->__last_nonopt 698 | && d->__last_nonopt != d->optind) 699 | exchange_w((wchar_t**)argv, d); 700 | else if (d->__last_nonopt != d->optind) 701 | d->__first_nonopt = d->optind; 702 | while (d->optind < argc && NONOPTION_P) 703 | d->optind++; 704 | d->__last_nonopt = d->optind; 705 | } 706 | if (d->optind != argc && !wcscmp(argv[d->optind], L"--")) 707 | { 708 | d->optind++; 709 | if (d->__first_nonopt != d->__last_nonopt 710 | && d->__last_nonopt != d->optind) 711 | exchange_w((wchar_t**)argv, d); 712 | else if (d->__first_nonopt == d->__last_nonopt) 713 | d->__first_nonopt = d->optind; 714 | d->__last_nonopt = argc; 715 | d->optind = argc; 716 | } 717 | if (d->optind == argc) 718 | { 719 | if (d->__first_nonopt != d->__last_nonopt) 720 | d->optind = d->__first_nonopt; 721 | return -1; 722 | } 723 | if (NONOPTION_P) 724 | { 725 | if (d->__ordering == REQUIRE_ORDER) 726 | return -1; 727 | d->optarg = argv[d->optind++]; 728 | return 1; 729 | } 730 | if (longopts) 731 | { 732 | if (argv[d->optind][1] == L'-') 733 | { 734 | d->__nextchar = argv[d->optind] + 2; 735 | return process_long_option_w(argc, (wchar_t**)argv, optstring, longopts, longind, long_only, d, print_errors, L"--"); 736 | } 737 | if (long_only && (argv[d->optind][2] || !wcschr(optstring, argv[d->optind][1]))) 738 | { 739 | int code; 740 | d->__nextchar = argv[d->optind] + 1; 741 | code = process_long_option_w(argc, (wchar_t**)argv, optstring, longopts, longind, long_only, d, print_errors, L"-"); 742 | if (code != -1) 743 | return code; 744 | } 745 | } 746 | d->__nextchar = argv[d->optind] + 1; 747 | } 748 | { 749 | wchar_t c = *d->__nextchar++; 750 | const wchar_t* temp = wcschr(optstring, c); 751 | if (*d->__nextchar == L'\0') 752 | ++d->optind; 753 | if (temp == NULL || c == L':' || c == L';') 754 | { 755 | if (print_errors) 756 | fwprintf(stderr, L"%s: invalid option -- '%c'\n", argv[0], c); 757 | d->optopt = c; 758 | return L'?'; 759 | } 760 | if (temp[0] == L'W' && temp[1] == L';' && longopts != NULL) 761 | { 762 | if (*d->__nextchar != L'\0') 763 | d->optarg = d->__nextchar; 764 | else if (d->optind == argc) 765 | { 766 | if (print_errors) 767 | fwprintf(stderr, L"%s: option requires an argument -- '%c'\n", argv[0], c); 768 | d->optopt = c; 769 | if (optstring[0] == L':') 770 | c = L':'; 771 | else 772 | c = L'?'; 773 | return c; 774 | } 775 | else 776 | d->optarg = argv[d->optind]; 777 | d->__nextchar = d->optarg; 778 | d->optarg = NULL; 779 | return process_long_option_w(argc, (wchar_t**)argv, optstring, longopts, longind, 780 | 0, d, print_errors, L"-W "); 781 | } 782 | if (temp[1] == L':') 783 | { 784 | if (temp[2] == L':') 785 | { 786 | if (*d->__nextchar != L'\0') 787 | { 788 | d->optarg = d->__nextchar; 789 | d->optind++; 790 | } 791 | else 792 | d->optarg = NULL; 793 | d->__nextchar = NULL; 794 | } 795 | else 796 | { 797 | if (*d->__nextchar != L'\0') 798 | { 799 | d->optarg = d->__nextchar; 800 | d->optind++; 801 | } 802 | else if (d->optind == argc) 803 | { 804 | if (print_errors) 805 | fwprintf(stderr, L"%s: option requires an argument -- '%c'\n", argv[0], c); 806 | d->optopt = c; 807 | if (optstring[0] == L':') 808 | c = L':'; 809 | else 810 | c = L'?'; 811 | } 812 | else 813 | d->optarg = argv[d->optind++]; 814 | d->__nextchar = NULL; 815 | } 816 | } 817 | return c; 818 | } 819 | } 820 | 821 | int _getopt_internal_w(int argc, wchar_t* const* argv, const wchar_t* optstring, const struct option_w* longopts, int* longind, int long_only, int posixly_correct) 822 | { 823 | int result; 824 | getopt_data_w.optind = optind; 825 | getopt_data_w.opterr = opterr; 826 | result = _getopt_internal_r_w(argc, argv, optstring, longopts, longind, long_only, &getopt_data_w, posixly_correct); 827 | optind = getopt_data_w.optind; 828 | optarg_w = getopt_data_w.optarg; 829 | optopt = getopt_data_w.optopt; 830 | return result; 831 | } 832 | 833 | int getopt_w(int argc, wchar_t* const* argv, const wchar_t* optstring) _GETOPT_THROW 834 | { 835 | return _getopt_internal_w(argc, argv, optstring, (const struct option_w*)0, (int*)0, 0, 0); 836 | } 837 | 838 | int getopt_long_w(int argc, wchar_t* const* argv, const wchar_t* options, const struct option_w* long_options, int* opt_index) _GETOPT_THROW 839 | { 840 | return _getopt_internal_w(argc, argv, options, long_options, opt_index, 0, 0); 841 | } 842 | 843 | int getopt_long_only_w(int argc, wchar_t* const* argv, const wchar_t* options, const struct option_w* long_options, int* opt_index) _GETOPT_THROW 844 | { 845 | return _getopt_internal_w(argc, argv, options, long_options, opt_index, 1, 0); 846 | } 847 | 848 | int _getopt_long_r_w(int argc, wchar_t* const* argv, const wchar_t* options, const struct option_w* long_options, int* opt_index, struct _getopt_data_w* d) 849 | { 850 | return _getopt_internal_r_w(argc, argv, options, long_options, opt_index, 0, d, 0); 851 | } 852 | 853 | int _getopt_long_only_r_w(int argc, wchar_t* const* argv, const wchar_t* options, const struct option_w* long_options, int* opt_index, struct _getopt_data_w* d) 854 | { 855 | return _getopt_internal_r_w(argc, argv, options, long_options, opt_index, 1, d, 0); 856 | } -------------------------------------------------------------------------------- /gpl-3.0.txt: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------