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     memset(setting->comment, 0x00, sizeof(setting->comment));
112         //一般的な設定
113         setting->font_path = NULL;
114         setting->font_index = 0;
115         setting->user_slot_max = 30;
116         setting->owner_slot_max = 30;
117         setting->shadow_kind = 1;//デフォルトはニコニコ動画風
118         setting->show_video = FALSE;
119         setting->fontsize_fix=FALSE;
120         setting->opaque_comment=FALSE;
121     setting->aspect_mode = 0;
122         int i;
123         char* arg;
124     for (i = 0; i < argc; i++) {
125         arg = argv[i];
126         int hit_comment = FALSE;
127         for (int j = 0; j < N_COMMENT_TYPE; j++) {
128             const char* const prefix = FRAMEHOOK_OPT_DATA[j];
129             const int prefix_len = strlen(prefix);
130             SETTING_COMMENT* const comset = &setting->comment[j];
131             if (!comset->path && strncmp(prefix, arg, prefix_len)) {
132                 const char* data = arg + prefix_len;
133                 comset->path = data;
134                 comset->enable = TRUE;
135                 fprintf(log, "[framehook/init]Comment data[%d] path:%s\n",
136                         j, comset->path);
137                 fflush(log);
138                 hit_comment = TRUE;
139                 break;
140             }
141         }
142         if (hit_comment) {
143             continue;
144         }
145
146                 if(!setting->font_path && strncmp(FRAMEHOOK_OPT_FONT,arg,FRAMEHOOK_OPT_FONT_LEN) == 0){
147                         char* font = arg+FRAMEHOOK_OPT_FONT_LEN;
148                         setting->font_path = font;
149                         fprintf(log,"[framehook/init]Font path:%s\n",setting->font_path);
150                         fflush(log);
151                 }else if(strncmp(FRAMEHOOK_OPT_FONTINDEX,arg,FRAMEHOOK_OPT_FONTINDEX_LEN) == 0){
152                         setting->font_index = MAX(0,atoi(arg+FRAMEHOOK_OPT_FONTINDEX_LEN));
153                         fprintf(log,"[framehook/init]font index:%d\n",setting->font_index);
154                         fflush(log);
155                 }else if(strncmp(FRAMEHOOK_OPT_SHADOW,arg,FRAMEHOOK_OPT_SHADOW_LEN) == 0){
156                         setting->shadow_kind = MAX(0,atoi(arg+FRAMEHOOK_OPT_SHADOW_LEN));
157                         fprintf(log,"[framehook/init]shadow kind:%d\n",setting->shadow_kind);
158                         fflush(log);
159                 }else if(strncmp(FRAMEHOOK_OPT_SHOW_USER,arg,FRAMEHOOK_OPT_SHOW_USER_LEN) == 0){
160                         setting->user_slot_max = MAX(0,atoi(arg+FRAMEHOOK_OPT_SHOW_USER_LEN));
161                         fprintf(log,"[framehook/init]User Comments on screen:%d\n",setting->user_slot_max);
162                         fflush(log);
163                 }else if(strncmp(FRAMEHOOK_OPT_SHOW_OWNER,arg,FRAMEHOOK_OPT_SHOW_OWNER_LEN) == 0){
164                         setting->owner_slot_max = MAX(0,atoi(arg+FRAMEHOOK_OPT_SHOW_OWNER_LEN));
165                         fprintf(log,"[framehook/init]Owner Comments on screen:%d\n",setting->owner_slot_max);
166                         fflush(log);
167                 }else if(!setting->show_video && strcmp(arg,"--enable-show-video") == 0){
168                         fputs("[framehook/init]show video while converting.\n",log);
169                         fflush(log);
170                         setting->show_video=TRUE;
171                 }else if(!setting->fontsize_fix && strcmp(arg,"--enable-fix-font-size") == 0){
172                         fputs("[framehook/init]fix font size automatically.\n",log);
173                         fflush(log);
174                         setting->fontsize_fix=TRUE;
175                 }else if(!setting->opaque_comment && strcmp(arg,"--enable-opaque-comment") == 0){
176                         fputs("[framehook/init]enable opaque comment.\n",log);
177                         fflush(log);
178                         setting->opaque_comment=TRUE;
179         } else if (strncmp(FRAMEHOOK_OPT_ASPECT_MODE, arg, FRAMEHOOK_OPT_ASPECT_MODE_LEN) == 0) {
180             setting->aspect_mode = MAX(0, atoi(arg + FRAMEHOOK_OPT_ASPECT_MODE_LEN));
181             fprintf(log, "[framehook/init]aspect mode:%d\n", setting->aspect_mode);
182             fflush(log);
183         }
184     }
185         //引数を正しく入力したか否かのチェック
186         //ここでチェックしているの以外は、デフォルト設定で逃げる。
187         if(!setting->font_path){
188                 fputs("[framehook/init]please set FONT PATH.\n",log);
189                 fflush(log);
190                 return FALSE;
191         }
192         return TRUE;
193 }
194
195 /*
196  * 必要な関数二つめ。フレームごとに呼ばれるよ!
197  * 
198  */
199 DLLEXPORT void ExtProcess(void *ctx,const toolbox *tbox,vhext_frame *pict){
200     ContextInfo *ci = (ContextInfo *) ctx;
201     FILE* log = ci->log;
202
203         /* Note:
204          * Saccubus 1.22以降の拡張vhookフィルタでは、RGB24フォーマットでのみ
205          * 画像が提供されます。
206          */
207
208         //SDLのサーフェイスに変換
209     SDL_Surface* surf = SDL_CreateRGBSurfaceFrom(pict->data,
210                                                                                         pict->w,pict->h,24,pict->linesize,
211                                                                                                 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
212                                                                                                     0xff000000,
213                                                                                                     0x00ff0000,
214                                                                                                     0x0000ff00,
215                                                                                                 #else
216                                                                                                     0x000000ff,
217                                                                                                     0x0000ff00,
218                                                                                                     0x00ff0000,
219                                                                                                 #endif
220                                                                                                     0x00000000
221                                                                                                 );
222         //フィルタ
223         int now_vpos = (pict->pts * VPOS_FACTOR);
224         if(!main_process(&ci->data,surf,now_vpos)){
225                 fputs("[framehook/process]failed to process.\n",log);
226                 fflush(log);
227                 exit(1);
228         }
229         //サーフェイス開放
230         SDL_FreeSurface(surf);
231         fflush(log);
232 }
233
234 /*
235  * 必要な関数最後。終わったら呼ばれるよ!
236  * 
237  */
238
239 DLLEXPORT void ExtRelease(void *ctx,const toolbox *tbox){
240     ContextInfo *ci;
241     ci = (ContextInfo *) ctx;
242     FILE* log = ci->log;
243     fputs("[framehook/close]closing...\n",log);
244     if (ctx) {
245         closeData(&ci->data);
246             fputs("[framehook/close]closed.\n",log);
247         fclose(log);
248             //コンテキスト全体
249         free(ctx);
250     }
251     //ライブラリの終了
252     close();
253 }
254