OSDN Git Service

ソースの文字コードと改行コードを修正
[coroid/inqubus.git] / vhook / framehook.c
1 /*フレームフックの相手をするため専用*/
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <SDL/SDL.h>
7 #include "common/framehook_ext.h"
8 #include "framehook.h"
9 #include "main.h"
10 #include "mydef.h"
11 #include "nicodef.h"
12 #include "util.h"
13
14 #ifdef _WIN32
15  #define DLLEXPORT __declspec(dllexport)
16 #else
17  #define DLLEXPORT
18 #endif
19
20 typedef struct ContextInfo{
21         FILE* log;
22         DATA data;
23 } ContextInfo;
24
25 /*
26  * 必要な関数ひとつめ。最初に呼ばれるよ!
27  * 
28  */
29 int init_setting(FILE*log,const toolbox *tbox,SETTING* setting,int argc, char *argv[]);
30
31 DLLEXPORT int ExtConfigure(void **ctxp,const toolbox *tbox, int argc, char *argv[]){
32         int i;
33         //ログ
34         FILE* log = fopen("[log]vhext.txt", "w");
35         if(log == NULL){
36                 puts("[framehook/init]failed to open logfile.\n");
37                 fflush(log);
38                 return -1;
39         }else{
40                 fputs("[framehook/init]initializing..\n",log);
41                 fflush(log);
42         }
43         //必要な設定があるかの確認
44         fprintf(log,"[framehook/init]called with argc = %d\n",argc);
45         fflush(log);
46         for(i=0;i<argc;i++){
47                 fprintf(log,"[framehook/init]arg[%2d] = %s\n",i,argv[i]);
48                 fflush(log);
49         }
50         //セッティング取得。
51         SETTING setting;
52         if(init_setting(log,tbox,&setting,argc,argv)){
53                 fputs("[framehook/init]initialized settings.\n",log);
54                 fflush(log);
55         }else{
56                 fputs("[framehook/init]failed to initialize settings.\n",log);
57                 fflush(log);
58                 return -2;
59         }
60         //ライブラリなどの初期化
61         if(init(log)){
62                 fputs("[framehook/init]initialized libs.\n",log);
63                 fflush(log);
64         }else{
65                 fputs("[framehook/init]failed to initialize libs.\n",log);
66                 fflush(log);
67                 return -3;
68         }
69         /*コンテキストの設定*/
70         *ctxp = malloc(sizeof(ContextInfo));
71         if(*ctxp == NULL){
72                 fputs("[framehook/init]initialized to malloc for context.\n",log);
73                 fflush(log);
74         }
75         ContextInfo* ci = (ContextInfo*)*ctxp;
76         ci->log = log;
77         fflush(log);
78         if(initData(&ci->data,log,&setting)){
79                 fputs("[framehook/init]initialized context.\n",log);
80                 fputs("[framehook/init]initialized.\n",log);
81                 fflush(log);
82                 return 0;
83         }else{
84                 fputs("[framehook/init]failed to initialize context.\n",log);
85                 fflush(log);
86                 return -4;
87         }
88 }
89 /*
90  * 内部でのみ呼ばれる。
91  */
92
93  /*
94         argv[0]:プログラム
95         argv[1]:vhook
96         argv[2]:フォント
97         argv[3]:フォントインデックス
98         argv[4]:一画面
99         argv[5]:影の種類
100         以降オプション
101         --enable-show-video:描画中に動画を見せる。
102         --enable-fontsize-fix:フォントサイズを自動で調整する。
103 */
104  
105 int init_setting(FILE*log,const toolbox *tbox,SETTING* setting,int argc, char *argv[]){
106         /*videoの長さ*/
107         setting->video_length = (tbox->video_length * VPOS_FACTOR);
108         /*以降オプション*/
109         
110         //コメントを見せるか否か?
111         setting->enable_user_comment = FALSE;
112         setting->enable_owner_comment = FALSE;
113         setting->data_user_path = NULL;
114         setting->data_owner_path = NULL;
115         //一般的な設定
116         setting->font_path = NULL;
117         setting->font_index = 0;
118         setting->user_slot_max = 30;
119         setting->owner_slot_max = 30;
120         setting->shadow_kind = 1;//デフォルトはニコニコ動画風
121         setting->show_video = FALSE;
122         setting->fontsize_fix=FALSE;
123         setting->opaque_comment=FALSE;
124         int i;
125         char* arg;
126         for(i=0;i<argc;i++){
127                 arg = argv[i];
128                 if(!setting->data_user_path && strncmp(FRAMEHOOK_OPT_DATA_USER,arg,FRAMEHOOK_OPT_DATA_USER_LEN) == 0){
129                         char* data_user = arg+FRAMEHOOK_OPT_DATA_USER_LEN;
130                         setting->data_user_path = data_user;
131                         setting->enable_user_comment = TRUE;
132                         fprintf(log,"[framehook/init]User Comment data path:%s\n",setting->data_user_path);
133                         fflush(log);
134                 }else if(!setting->data_owner_path && strncmp(FRAMEHOOK_OPT_DATA_OWNER,arg,FRAMEHOOK_OPT_DATA_OWNER_LEN) == 0){
135                         char* data_owner = arg+FRAMEHOOK_OPT_DATA_OWNER_LEN;
136                         setting->data_owner_path = data_owner;
137                         setting->enable_owner_comment = TRUE;
138                         fprintf(log,"[framehook/init]Owner Comment data path:%s\n",setting->data_owner_path);
139                         fflush(log);
140                 }else if(!setting->font_path && strncmp(FRAMEHOOK_OPT_FONT,arg,FRAMEHOOK_OPT_FONT_LEN) == 0){
141                         char* font = arg+FRAMEHOOK_OPT_FONT_LEN;
142                         setting->font_path = font;
143                         fprintf(log,"[framehook/init]Font path:%s\n",setting->font_path);
144                         fflush(log);
145                 }else if(strncmp(FRAMEHOOK_OPT_FONTINDEX,arg,FRAMEHOOK_OPT_FONTINDEX_LEN) == 0){
146                         setting->font_index = MAX(0,atoi(arg+FRAMEHOOK_OPT_FONTINDEX_LEN));
147                         fprintf(log,"[framehook/init]font index:%d\n",setting->font_index);
148                         fflush(log);
149                 }else if(strncmp(FRAMEHOOK_OPT_SHADOW,arg,FRAMEHOOK_OPT_SHADOW_LEN) == 0){
150                         setting->shadow_kind = MAX(0,atoi(arg+FRAMEHOOK_OPT_SHADOW_LEN));
151                         fprintf(log,"[framehook/init]shadow kind:%d\n",setting->shadow_kind);
152                         fflush(log);
153                 }else if(strncmp(FRAMEHOOK_OPT_SHOW_USER,arg,FRAMEHOOK_OPT_SHOW_USER_LEN) == 0){
154                         setting->user_slot_max = MAX(0,atoi(arg+FRAMEHOOK_OPT_SHOW_USER_LEN));
155                         fprintf(log,"[framehook/init]User Comments on screen:%d\n",setting->user_slot_max);
156                         fflush(log);
157                 }else if(strncmp(FRAMEHOOK_OPT_SHOW_OWNER,arg,FRAMEHOOK_OPT_SHOW_OWNER_LEN) == 0){
158                         setting->owner_slot_max = MAX(0,atoi(arg+FRAMEHOOK_OPT_SHOW_OWNER_LEN));
159                         fprintf(log,"[framehook/init]Owner Comments on screen:%d\n",setting->owner_slot_max);
160                         fflush(log);
161                 }else if(!setting->show_video && strcmp(arg,"--enable-show-video") == 0){
162                         fputs("[framehook/init]show video while converting.\n",log);
163                         fflush(log);
164                         setting->show_video=TRUE;
165                 }else if(!setting->fontsize_fix && strcmp(arg,"--enable-fix-font-size") == 0){
166                         fputs("[framehook/init]fix font size automatically.\n",log);
167                         fflush(log);
168                         setting->fontsize_fix=TRUE;
169                 }else if(!setting->opaque_comment && strcmp(arg,"--enable-opaque-comment") == 0){
170                         fputs("[framehook/init]enable opaque comment.\n",log);
171                         fflush(log);
172                         setting->opaque_comment=TRUE;
173                 }
174         }
175         //引数を正しく入力したか否かのチェック
176         //ここでチェックしているの以外は、デフォルト設定で逃げる。
177         if(!setting->font_path){
178                 fputs("[framehook/init]please set FONT PATH.\n",log);
179                 fflush(log);
180                 return FALSE;
181         }
182         return TRUE;
183 }
184
185 /*
186  * 必要な関数二つめ。フレームごとに呼ばれるよ!
187  * 
188  */
189 DLLEXPORT void ExtProcess(void *ctx,const toolbox *tbox,vhext_frame *pict){
190     ContextInfo *ci = (ContextInfo *) ctx;
191     FILE* log = ci->log;
192
193         /* Note:
194          * Saccubus 1.22以降の拡張vhookフィルタでは、RGB24フォーマットでのみ
195          * 画像が提供されます。
196          */
197
198         //SDLのサーフェイスに変換
199     SDL_Surface* surf = SDL_CreateRGBSurfaceFrom(pict->data,
200                                                                                         pict->w,pict->h,24,pict->linesize,
201                                                                                                 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
202                                                                                                     0xff000000,
203                                                                                                     0x00ff0000,
204                                                                                                     0x0000ff00,
205                                                                                                 #else
206                                                                                                     0x000000ff,
207                                                                                                     0x0000ff00,
208                                                                                                     0x00ff0000,
209                                                                                                 #endif
210                                                                                                     0x00000000
211                                                                                                 );
212         //フィルタ
213         int now_vpos = (pict->pts * VPOS_FACTOR);
214         if(!main_process(&ci->data,surf,now_vpos)){
215                 fputs("[framehook/process]failed to process.\n",log);
216                 fflush(log);
217                 exit(1);
218         }
219         //サーフェイス開放
220         SDL_FreeSurface(surf);
221         fflush(log);
222 }
223
224 /*
225  * 必要な関数最後。終わったら呼ばれるよ!
226  * 
227  */
228
229 DLLEXPORT void ExtRelease(void *ctx,const toolbox *tbox){
230     ContextInfo *ci;
231     ci = (ContextInfo *) ctx;
232     FILE* log = ci->log;
233     fputs("[framehook/close]closing...\n",log);
234     if (ctx) {
235         closeData(&ci->data);
236             fputs("[framehook/close]closed.\n",log);
237         fclose(log);
238             //コンテキスト全体
239         free(ctx);
240     }
241     //ライブラリの終了
242     close();
243 }
244