no longer need ffmpeg patch0 which was for Termux
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / titler / titler.h
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program 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 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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 this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #ifndef TITLE_H
23 #define TITLE_H
24 #define USE_STROKER
25
26 #define KW_NUDGE  N_("nudge")
27 #define KW_COLOR  N_("color")
28 #define KW_ALPHA  N_("alpha")
29 #define KW_FONT   N_("font")
30 #define KW_SIZE   N_("size")
31 #define KW_BOLD   N_("bold")
32 #define KW_ITALIC N_("italic")
33 #define KW_CAPS   N_("caps")
34 #define KW_UL     N_("ul")
35 #define KW_BLINK  N_("blink")
36 #define KW_FIXED  N_("fixed")
37 #define KW_ALIAS  N_("smooth")
38 #define KW_SUP    N_("sup")
39 #define KW_PNG    N_("png")
40
41 class TitleConfig;
42 class TitleGlyph;
43 class TitleGlyphs;
44 class TitleImage;
45 class TitleImages;
46 class TitleChar;
47 class TitleChars;
48 class TitleRow;
49 class TitleRows;
50 class GlyphPackage;
51 class GlyphUnit;
52 class GlyphEngine;
53 class TitlePackage;
54 class TitleUnit;
55 class TitleEngine;
56 class TitleOutlinePackage;
57 class TitleOutlineUnit;
58 class TitleOutlineEngine;
59 class TitleTranslatePackage;
60 class TitleTranslateUnit;
61 class TitleTranslate;
62 class TitleCurFont;
63 class TitleCurSize;
64 class TitleCurColor;
65 class TitleCurAlpha;
66 class TitleCurBold;
67 class TitleCurItalic;
68 class TitleCurCaps;
69 class TitleCurUnder;
70 class TitleCurBlink;
71 class TitleCurFixed;
72 class TitleCurAlias;
73 class TitleCurSuper;
74 class TitleCurNudge;
75 class TitleParser;
76 class TitleMain;
77
78 #include "bchash.h"
79 #include "bcfontentry.h"
80 #include "file.inc"
81 #include "indexable.inc"
82 #include "loadbalance.h"
83 #include "mutex.h"
84 #include "overlayframe.h"
85 #include "pluginvclient.h"
86 #include "renderengine.inc"
87 #include "titlerwindow.h"
88
89 #include <ft2build.h>
90 #include FT_FREETYPE_H
91 #include <sys/types.h>
92 #include <wchar.h>
93 #include <wctype.h>
94
95 // Motion strategy
96 #define TOTAL_PATHS 5
97 #define NO_MOTION     0x0
98 #define BOTTOM_TO_TOP 0x1
99 #define TOP_TO_BOTTOM 0x2
100 #define RIGHT_TO_LEFT 0x3
101 #define LEFT_TO_RIGHT 0x4
102
103 // Horizontal justification
104 #define JUSTIFY_LEFT   0x0
105 #define JUSTIFY_CENTER 0x1
106 #define JUSTIFY_RIGHT  0x2
107
108 // Vertical justification
109 #define JUSTIFY_TOP     0x0
110 #define JUSTIFY_MID     0x1
111 #define JUSTIFY_BOTTOM  0x2
112
113 // char types
114 #define CHAR_GLYPH  0
115 #define CHAR_IMAGE  1
116
117 // flags
118 #define FLAG_UNDER  0x0001
119 #define FLAG_FIXED  0x0002
120 #define FLAG_SUPER  0x0004
121 #define FLAG_SUBER  0x0008
122 #define FLAG_BLINK  0x0010
123
124 #define FONT_ALIAS 0x08
125
126 class TitleConfig
127 {
128 public:
129         TitleConfig();
130         ~TitleConfig();
131
132         void to_wtext(const char *from_enc, const char *text, int tlen);
133 // Only used to clear glyphs
134         int equivalent(TitleConfig &that);
135         void copy_from(TitleConfig &that);
136         void interpolate(TitleConfig &prev,
137                 TitleConfig &next,
138                 int64_t prev_frame,
139                 int64_t next_frame,
140                 int64_t current_frame);
141
142 // Font information
143         char font[BCTEXTLEN];
144 // Encoding to convert from
145         char encoding[BCTEXTLEN];
146         int style;
147         float size;
148         int color;
149         int alpha;
150         float outline_size;
151         int outline_color;
152         int outline_alpha;
153         int color_stroke;
154         float stroke_width;
155         int motion_strategy;     // Motion of title across frame
156         int line_pitch;
157         int loop;                // Loop motion path
158         int hjustification;
159         int vjustification;
160 // Number of seconds the fade in and fade out of the title take
161         double fade_in, fade_out;
162         float pixels_per_second; // Speed of motion
163 // Text to display
164         wchr_t *wtext;
165         long wsize, wlen;
166 // Position in frame relative to top left
167         float title_x, title_y;
168         int title_w, title_h;
169 // Size of window
170         int window_w, window_h;
171 // Calculated during every frame for motion strategy
172         int64_t prev_keyframe_position;
173         int64_t next_keyframe_position;
174 // Stamp timecode
175         int timecode;
176         int dropshadow;
177         int background;
178         char background_path[BCTEXTLEN];
179
180         void convert_text();
181         int demand(long sz);
182
183 // Time Code Format
184         int timecode_format;
185 // loop background playback
186         int loop_playback;
187 };
188
189 class TitleGlyph
190 {
191 public:
192         TitleGlyph();
193         ~TitleGlyph();
194
195         FT_ULong char_code;
196         int freetype_index;
197         BC_FontEntry *font;
198         int width, height, style;
199         int size, pitch;
200         int advance_x;
201         int left, top, right, bottom;
202         VFrame *data, *data_stroke;
203 };
204 class TitleGlyphs : public ArrayList<TitleGlyph *> {
205 public:
206         void clear() { remove_all_objects(); }
207         int count() { return size(); }
208
209         TitleGlyphs() {}
210         ~TitleGlyphs() { clear(); }
211 };
212
213 class TitleImage {
214 public:
215         char *path;
216         VFrame *vframe;
217
218         TitleImage(const char *path, VFrame *vfrm);
219         ~TitleImage();
220 };
221 class TitleImages : public ArrayList<TitleImage *> {
222 public:
223         void clear() { remove_all_objects(); }
224         int count() { return size(); }
225
226         TitleImages() {}
227         ~TitleImages() { clear(); }
228 };
229
230
231 // Position of each image box in a row
232 class TitleChar {
233 public:
234         wchr_t wch;
235         int typ, flags;
236         void *vp;
237         int x, y;
238         int row, dx;
239         int color, alpha;
240         float fade;
241         float blink, size;
242
243         TitleChar *init(int typ, void *vp);
244 };
245 class TitleChars : public ArrayList<TitleChar *> {
246         int next;
247 public:
248         void reset() { next = 0; }
249         void clear() { remove_all_objects(); reset(); }
250         int count() { return next; }
251         TitleChar *add(int typ, void *vp) {
252                 TitleChar *ret = next < size() ? get(next++) : 0;
253                 if( !ret ) { append(ret = new TitleChar());  next = size(); }
254                 return ret->init(typ, vp);
255         }
256         TitleChars() { reset(); }
257         ~TitleChars() { clear(); }
258 };
259
260 class TitleRow {
261 public:
262         float x0, y0, x1, y1, x2, y2;
263         TitleRow *init();
264         void bound(float lt, float tp, float rt, float bt) {
265                 if( x1 > lt ) x1 = lt;
266                 if( y1 < tp ) y1 = tp;
267                 if( x2 < rt ) x2 = rt;
268                 if( y2 > bt ) y2 = bt;
269         }
270 };
271 class TitleRows : public ArrayList<TitleRow *> {
272         int next;
273 public:
274         void reset() { next = 0; }
275         void clear() { remove_all_objects(); reset(); }
276         int count() { return next; }
277         TitleRow *add() {
278                 TitleRow *ret = next < size() ? get(next++) : 0;
279                 if( !ret ) { append(ret = new TitleRow());  next = size(); }
280                 return ret->init();
281         }
282         TitleRows() { reset(); }
283         ~TitleRows() { clear(); }
284 };
285
286 // Draw a single character into the glyph cache
287 //
288 class GlyphPackage : public LoadPackage
289 {
290 public:
291         GlyphPackage();
292         TitleGlyph *glyph;
293 };
294
295
296 class GlyphUnit : public LoadClient
297 {
298 public:
299         GlyphUnit(TitleMain *plugin, GlyphEngine *server);
300         ~GlyphUnit();
301         void process_package(LoadPackage *package);
302
303         TitleMain *plugin;
304         FT_Library freetype_library;            // Freetype library
305         FT_Face freetype_face;
306 };
307
308 class GlyphEngine : public LoadServer
309 {
310 public:
311         GlyphEngine(TitleMain *plugin, int cpus);
312         void init_packages();
313         LoadClient* new_client();
314         LoadPackage* new_package();
315         TitleMain *plugin;
316 };
317
318
319 // Copy a single character to the text mask
320 class TitlePackage : public LoadPackage
321 {
322 public:
323         TitlePackage();
324         int x, y;
325         TitleChar *chr;
326 };
327
328 // overlay modes
329 #define DRAW_ALPHA 1
330 #define DRAW_COLOR 2
331 #define DRAW_IMAGE 3
332
333 class TitleUnit : public LoadClient
334 {
335 public:
336         TitleUnit(TitleMain *plugin, TitleEngine *server);
337         void process_package(LoadPackage *package);
338         void draw_frame(int mode, VFrame *dst, VFrame *src, int x, int y);
339
340         TitleMain *plugin;
341         TitleEngine *engine;
342         TitleChar *chr;
343 };
344
345 class TitleEngine : public LoadServer
346 {
347 public:
348         TitleEngine(TitleMain *plugin, int cpus);
349         void init_packages();
350         LoadClient* new_client();
351         LoadPackage* new_package();
352         TitleMain *plugin;
353         int do_dropshadow;
354 };
355
356
357 // Create outline
358 class TitleOutlinePackage : public LoadPackage
359 {
360 public:
361         TitleOutlinePackage();
362         int y1, y2;
363 };
364
365
366 class TitleOutlineUnit : public LoadClient
367 {
368 public:
369         TitleOutlineUnit(TitleMain *plugin, TitleOutlineEngine *server);
370         void process_package(LoadPackage *package);
371         TitleMain *plugin;
372         TitleOutlineEngine *engine;
373 };
374
375 class TitleOutlineEngine : public LoadServer
376 {
377 public:
378         TitleOutlineEngine(TitleMain *plugin, int cpus);
379         void init_packages();
380         void do_outline();
381         LoadClient* new_client();
382         LoadPackage* new_package();
383         TitleMain *plugin;
384         int pass;
385 };
386
387 template<class typ> class TitleStack : public ArrayList<typ>
388 {
389         typ &last() { return ArrayList<typ>::last(); }
390         int size() { return ArrayList<typ>::size(); }
391         typ &append(typ &v) { return ArrayList<typ>::append(v); }
392         void remove() { return ArrayList<typ>::remove(); }
393 public:
394         TitleParser *parser;
395         TitleStack(TitleParser *p, typ v) : parser(p) { append(v); }
396         operator typ&() { return last(); }
397         typ &push(typ &v) { return append(v); }
398         int pop() { return size()>1 ? (remove(),0) : 1; }
399         int set(const char *txt);
400         int unset(const char *txt);
401 };
402
403 template<class typ> int TitleStack<typ>::set(const char *txt)
404 {
405         typ v = !*txt ? 1 : strtol(txt,(char **)&txt,0);
406         if( *txt || v < 0 || v > 1 ) return 1;
407         push(v);
408         return 0;
409 }
410 template<class typ> int TitleStack<typ>::unset(const char *txt)
411 {
412         return pop();
413 }
414
415 class TitleCurNudge : public TitleStack<int> {
416 public:
417         TitleCurNudge(TitleParser *parser, TitleMain *plugin);
418         int set(const char *txt);
419 };
420
421 class TitleCurColor : public TitleStack<int> {
422 public:
423         TitleCurColor(TitleParser *parser, TitleMain *plugin);
424         int set(const char *txt);
425 };
426
427 class TitleCurAlpha : public TitleStack<int> {
428 public:
429         TitleCurAlpha(TitleParser *parser, TitleMain *plugin);
430         int set(const char *txt);
431 };
432
433 class TitleCurSize : public TitleStack<float> {
434 public:
435         TitleCurSize(TitleParser *parser, TitleMain *plugin);
436         int set(const char *txt);
437         int unset(const char *txt);
438 };
439
440 class TitleCurBold : public TitleStack<int> {
441 public:
442         TitleCurBold(TitleParser *parser, TitleMain *plugin);
443         int set(const char *txt);
444         int unset(const char *txt);
445 };
446
447 class TitleCurItalic : public TitleStack<int> {
448 public:
449         TitleCurItalic(TitleParser *parser, TitleMain *plugin);
450         int set(const char *txt);
451         int unset(const char *txt);
452 };
453
454 class TitleCurFont : public TitleStack<BC_FontEntry*>
455 {
456 public:
457         BC_FontEntry *get(const char *txt, int style);
458         BC_FontEntry *set(const char *txt, int style);
459         int style();
460         virtual int set(const char *txt=0);
461         virtual int unset(const char *txt);
462         TitleCurFont(TitleParser *parser, TitleMain *plugin);
463 };
464
465 class TitleCurCaps : public TitleStack<int> {
466 public:
467         TitleCurCaps(TitleParser *parser, TitleMain *plugin);
468         int set(const char *txt);
469 };
470
471 class TitleCurUnder : public TitleStack<int> {
472 public:
473         TitleCurUnder(TitleParser *parser, TitleMain *plugin);
474 };
475
476 class TitleCurBlink : public TitleStack<float> {
477 public:
478         TitleCurBlink(TitleParser *parser, TitleMain *plugin);
479         int set(const char *txt);
480 };
481
482 class TitleCurFixed : public TitleStack<int> {
483 public:
484         TitleCurFixed(TitleParser *parser, TitleMain *plugin);
485         int set(const char *txt);
486 };
487
488 class TitleCurAlias : public TitleStack<int> {
489 public:
490         TitleCurAlias(TitleParser *parser, TitleMain *plugin);
491         int set(const char *txt);
492 };
493
494 class TitleCurSuper : public TitleStack<int> {
495 public:
496         TitleCurSuper(TitleParser *parser, TitleMain *plugin);
497         int set(const char *txt);
498 };
499
500
501 class TitleParser
502 {
503         const wchr_t *bfr, *out, *lmt;
504 public:
505         TitleMain *plugin;
506
507         long tell() { return out - bfr; }
508         void seek(long pos) { out = bfr + pos; }
509         bool eof() { return out >= lmt; }
510         int wcur() { return eof() ? -1 : *out; }
511         int wnext() { return eof() ? -1 : *out++; }
512         int wget(wchr_t &wch);
513         int tget(wchr_t &wch);
514         wchr_t wid[BCSTRLEN], wtxt[BCTEXTLEN];
515         char id[BCSTRLEN], text[BCTEXTLEN];
516         int set_attributes(int ret);
517
518         TitleCurNudge  cur_nudge;
519         TitleCurColor  cur_color;
520         TitleCurAlpha  cur_alpha;
521         TitleCurSize   cur_size;
522         TitleCurBold   cur_bold;
523         TitleCurItalic cur_italic;
524         TitleCurFont   cur_font;
525         TitleCurCaps   cur_caps;
526         TitleCurUnder  cur_under;
527         TitleCurBlink  cur_blink;
528         TitleCurFixed  cur_fixed;
529         TitleCurAlias  cur_alias;
530         TitleCurSuper  cur_super;
531
532         TitleParser(TitleMain *main);
533 };
534
535
536 // Overlay text mask with fractional translation
537 // We don't use OverlayFrame to enable alpha blending on non alpha
538 // output.
539 class TitleTranslatePackage : public LoadPackage
540 {
541 public:
542         TitleTranslatePackage();
543         int y1, y2;
544 };
545
546 class TitleTranslateUnit : public LoadClient
547 {
548 public:
549         TitleTranslateUnit(TitleMain *plugin, TitleTranslate *server);
550
551         void process_package(LoadPackage *package);
552         TitleMain *plugin;
553 };
554
555 class TitleTranslate : public LoadServer
556 {
557 public:
558         TitleTranslate(TitleMain *plugin, int cpus);
559         ~TitleTranslate();
560
561         TitleMain *plugin;
562         VFrame *input;
563         float in_w, in_h, out_w, out_h;
564         float ix1, iy1, ix2, iy2;
565         float ox1, oy1, ox2, oy2;
566
567         void copy(VFrame *input);
568         LoadClient* new_client();
569         LoadPackage* new_package();
570         void init_packages();
571 };
572
573
574 class TitleMain : public PluginVClient
575 {
576 public:
577         TitleMain(PluginServer *server);
578         ~TitleMain();
579
580 // required for all realtime plugins
581         PLUGIN_CLASS_MEMBERS(TitleConfig)
582         void render_gui(void *data);
583         int is_dragging();
584         int process_realtime(VFrame *input_ptr, VFrame *output_ptr);
585         int is_realtime();
586         int is_synthesis();
587         void update_gui();
588         void save_data(KeyFrame *keyframe);
589         void read_data(KeyFrame *keyframe);
590         void insert_text(const wchr_t *wtxt, int pos);
591
592         void build_previews(TitleWindow *gui);
593         void reset_render();
594         int init_freetype();
595         int set_font(BC_FontEntry*&font, const char *txt);
596         int set_font(BC_FontEntry*&font, const char *txt, int style);
597         int set_size(int &size, const char *txt);
598         int set_color(int &color, const char *txt);
599         int set_bold(int &bold, const char *txt);
600         int set_italic(int &italic, const char *txt);
601         int set_caps(int &caps, const char *txt);
602         int set_under(int &under, const char *txt);
603         void load_glyphs();
604         int draw_text(int need_redraw);
605         int draw_underline(VFrame *mask, int alpha);
606         void draw_overlay();
607         void draw_boundry();
608         int get_text();
609         int get_visible_text();
610         int check_char_code_path(FT_Library &freetype_library,
611                 char *path_old, FT_ULong &char_code, char *path_new);
612         int load_freetype_face(FT_Library &freetype_library,
613                 FT_Face &freetype_face, const char *path);
614         int load_font(BC_FontEntry *font);
615         Indexable *open_background(const char *filename);
616         int read_background(VFrame *frame, int64_t position, int color_model);
617         void draw_background();
618         static BC_FontEntry* get_font(const char *font_name, int style);
619         BC_FontEntry* config_font();
620         TitleGlyph *get_glyph(FT_ULong char_code, BC_FontEntry *font, int size, int style);
621         int get_width(TitleGlyph *cur, TitleGlyph *nxt);
622
623         VFrame *add_image(const char *path);
624         VFrame *get_image(const char *path);
625
626 // backward compatibility
627         void convert_encoding();
628         static const char* motion_to_text(int motion);
629         static int text_to_motion(const char *text);
630
631         VFrame *text_mask;
632         VFrame *stroke_mask;
633         GlyphEngine *glyph_engine;
634         TitleEngine *title_engine;
635         VFrame *outline_mask;
636         TitleOutlineEngine *outline_engine;
637         TitleTranslate *translate;
638
639         TitleChars title_chars;
640         TitleRows title_rows;
641         TitleGlyphs title_glyphs;
642         TitleImages title_images;
643
644         FT_Library freetype_library;
645         FT_Face freetype_face;
646         char text_font[BCTEXTLEN];
647
648         int window_w, window_h;
649         int fuzz, fuzz1, fuzz2;
650         float title_x, title_y;
651         int title_w, title_h;
652
653         float text_x, text_y, text_w, text_h;
654         float text_x1, text_y1, text_x2, text_y2;
655
656         int mask_x, mask_y; int mask_w, mask_h;
657         int mask_x1, mask_y1, mask_x2, mask_y2;
658
659         int text_rows;
660         int visible_row1, visible_char1;
661         int visible_row2, visible_char2;
662         float fade;
663
664         VFrame *input, *output;
665         int output_model, text_model, mask_model;
666
667         Indexable *background;
668         File *bg_file;
669         VFrame *bg_frame;
670         RenderEngine *render_engine;
671         CICache *video_cache;
672         OverlayFrame *overlay_frame;
673
674         int64_t last_position;
675         int need_reconfigure;
676         int cpus;
677         int drag;
678 };
679
680
681 #endif