4 * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
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.
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.
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
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")
56 class TitleOutlinePackage;
57 class TitleOutlineUnit;
58 class TitleOutlineEngine;
59 class TitleTranslatePackage;
60 class TitleTranslateUnit;
79 #include "bcfontentry.h"
81 #include "indexable.inc"
82 #include "loadbalance.h"
84 #include "overlayframe.h"
85 #include "pluginvclient.h"
86 #include "renderengine.inc"
87 #include "titlerwindow.h"
90 #include FT_FREETYPE_H
91 #include <sys/types.h>
98 #define BOTTOM_TO_TOP 0x1
99 #define TOP_TO_BOTTOM 0x2
100 #define RIGHT_TO_LEFT 0x3
101 #define LEFT_TO_RIGHT 0x4
103 // Horizontal justification
104 #define JUSTIFY_LEFT 0x0
105 #define JUSTIFY_CENTER 0x1
106 #define JUSTIFY_RIGHT 0x2
108 // Vertical justification
109 #define JUSTIFY_TOP 0x0
110 #define JUSTIFY_MID 0x1
111 #define JUSTIFY_BOTTOM 0x2
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
124 #define FONT_ALIAS 0x08
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,
140 int64_t current_frame);
143 char font[BCTEXTLEN];
144 // Encoding to convert from
145 char encoding[BCTEXTLEN];
155 int motion_strategy; // Motion of title across frame
157 int loop; // Loop motion path
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
166 // Position in frame relative to top left
167 float title_x, title_y;
168 int title_w, title_h;
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;
178 char background_path[BCTEXTLEN];
185 // loop background playback
198 int width, height, style;
201 int left, top, right, bottom;
202 VFrame *data, *data_stroke;
204 class TitleGlyphs : public ArrayList<TitleGlyph *> {
206 void clear() { remove_all_objects(); }
207 int count() { return size(); }
210 ~TitleGlyphs() { clear(); }
218 TitleImage(const char *path, VFrame *vfrm);
221 class TitleImages : public ArrayList<TitleImage *> {
223 void clear() { remove_all_objects(); }
224 int count() { return size(); }
227 ~TitleImages() { clear(); }
231 // Position of each image box in a row
243 TitleChar *init(int typ, void *vp);
245 class TitleChars : public ArrayList<TitleChar *> {
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);
256 TitleChars() { reset(); }
257 ~TitleChars() { clear(); }
262 float x0, y0, x1, y1, x2, y2;
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;
271 class TitleRows : public ArrayList<TitleRow *> {
274 void reset() { next = 0; }
275 void clear() { remove_all_objects(); reset(); }
276 int count() { return next; }
278 TitleRow *ret = next < size() ? get(next++) : 0;
279 if( !ret ) { append(ret = new TitleRow()); next = size(); }
282 TitleRows() { reset(); }
283 ~TitleRows() { clear(); }
286 // Draw a single character into the glyph cache
288 class GlyphPackage : public LoadPackage
296 class GlyphUnit : public LoadClient
299 GlyphUnit(TitleMain *plugin, GlyphEngine *server);
301 void process_package(LoadPackage *package);
304 FT_Library freetype_library; // Freetype library
305 FT_Face freetype_face;
308 class GlyphEngine : public LoadServer
311 GlyphEngine(TitleMain *plugin, int cpus);
312 void init_packages();
313 LoadClient* new_client();
314 LoadPackage* new_package();
319 // Copy a single character to the text mask
320 class TitlePackage : public LoadPackage
333 class TitleUnit : public LoadClient
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);
345 class TitleEngine : public LoadServer
348 TitleEngine(TitleMain *plugin, int cpus);
349 void init_packages();
350 LoadClient* new_client();
351 LoadPackage* new_package();
358 class TitleOutlinePackage : public LoadPackage
361 TitleOutlinePackage();
366 class TitleOutlineUnit : public LoadClient
369 TitleOutlineUnit(TitleMain *plugin, TitleOutlineEngine *server);
370 void process_package(LoadPackage *package);
372 TitleOutlineEngine *engine;
375 class TitleOutlineEngine : public LoadServer
378 TitleOutlineEngine(TitleMain *plugin, int cpus);
379 void init_packages();
381 LoadClient* new_client();
382 LoadPackage* new_package();
387 template<class typ> class TitleStack : public ArrayList<typ>
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(); }
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);
403 template<class typ> int TitleStack<typ>::set(const char *txt)
405 typ v = !*txt ? 1 : strtol(txt,(char **)&txt,0);
406 if( *txt || v < 0 || v > 1 ) return 1;
410 template<class typ> int TitleStack<typ>::unset(const char *txt)
415 class TitleCurNudge : public TitleStack<int> {
417 TitleCurNudge(TitleParser *parser, TitleMain *plugin);
418 int set(const char *txt);
421 class TitleCurColor : public TitleStack<int> {
423 TitleCurColor(TitleParser *parser, TitleMain *plugin);
424 int set(const char *txt);
427 class TitleCurAlpha : public TitleStack<int> {
429 TitleCurAlpha(TitleParser *parser, TitleMain *plugin);
430 int set(const char *txt);
433 class TitleCurSize : public TitleStack<float> {
435 TitleCurSize(TitleParser *parser, TitleMain *plugin);
436 int set(const char *txt);
437 int unset(const char *txt);
440 class TitleCurBold : public TitleStack<int> {
442 TitleCurBold(TitleParser *parser, TitleMain *plugin);
443 int set(const char *txt);
444 int unset(const char *txt);
447 class TitleCurItalic : public TitleStack<int> {
449 TitleCurItalic(TitleParser *parser, TitleMain *plugin);
450 int set(const char *txt);
451 int unset(const char *txt);
454 class TitleCurFont : public TitleStack<BC_FontEntry*>
457 BC_FontEntry *get(const char *txt, int style);
458 BC_FontEntry *set(const char *txt, int style);
460 virtual int set(const char *txt=0);
461 virtual int unset(const char *txt);
462 TitleCurFont(TitleParser *parser, TitleMain *plugin);
465 class TitleCurCaps : public TitleStack<int> {
467 TitleCurCaps(TitleParser *parser, TitleMain *plugin);
468 int set(const char *txt);
471 class TitleCurUnder : public TitleStack<int> {
473 TitleCurUnder(TitleParser *parser, TitleMain *plugin);
476 class TitleCurBlink : public TitleStack<float> {
478 TitleCurBlink(TitleParser *parser, TitleMain *plugin);
479 int set(const char *txt);
482 class TitleCurFixed : public TitleStack<int> {
484 TitleCurFixed(TitleParser *parser, TitleMain *plugin);
485 int set(const char *txt);
488 class TitleCurAlias : public TitleStack<int> {
490 TitleCurAlias(TitleParser *parser, TitleMain *plugin);
491 int set(const char *txt);
494 class TitleCurSuper : public TitleStack<int> {
496 TitleCurSuper(TitleParser *parser, TitleMain *plugin);
497 int set(const char *txt);
503 const wchr_t *bfr, *out, *lmt;
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);
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;
532 TitleParser(TitleMain *main);
536 // Overlay text mask with fractional translation
537 // We don't use OverlayFrame to enable alpha blending on non alpha
539 class TitleTranslatePackage : public LoadPackage
542 TitleTranslatePackage();
546 class TitleTranslateUnit : public LoadClient
549 TitleTranslateUnit(TitleMain *plugin, TitleTranslate *server);
551 void process_package(LoadPackage *package);
555 class TitleTranslate : public LoadServer
558 TitleTranslate(TitleMain *plugin, int cpus);
563 float in_w, in_h, out_w, out_h;
564 float ix1, iy1, ix2, iy2;
565 float ox1, oy1, ox2, oy2;
567 void copy(VFrame *input);
568 LoadClient* new_client();
569 LoadPackage* new_package();
570 void init_packages();
574 class TitleMain : public PluginVClient
577 TitleMain(PluginServer *server);
580 // required for all realtime plugins
581 PLUGIN_CLASS_MEMBERS(TitleConfig)
582 void render_gui(void *data);
584 int process_realtime(VFrame *input_ptr, VFrame *output_ptr);
588 void save_data(KeyFrame *keyframe);
589 void read_data(KeyFrame *keyframe);
590 void insert_text(const wchr_t *wtxt, int pos);
592 void build_previews(TitleWindow *gui);
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);
604 int draw_text(int need_redraw);
605 int draw_underline(VFrame *mask, int alpha);
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);
623 VFrame *add_image(const char *path);
624 VFrame *get_image(const char *path);
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);
633 GlyphEngine *glyph_engine;
634 TitleEngine *title_engine;
635 VFrame *outline_mask;
636 TitleOutlineEngine *outline_engine;
637 TitleTranslate *translate;
639 TitleChars title_chars;
640 TitleRows title_rows;
641 TitleGlyphs title_glyphs;
642 TitleImages title_images;
644 FT_Library freetype_library;
645 FT_Face freetype_face;
646 char text_font[BCTEXTLEN];
648 int window_w, window_h;
649 int fuzz, fuzz1, fuzz2;
650 float title_x, title_y;
651 int title_w, title_h;
653 float text_x, text_y, text_w, text_h;
654 float text_x1, text_y1, text_x2, text_y2;
656 int mask_x, mask_y; int mask_w, mask_h;
657 int mask_x1, mask_y1, mask_x2, mask_y2;
660 int visible_row1, visible_char1;
661 int visible_row2, visible_char2;
664 VFrame *input, *output;
665 int output_model, text_model, mask_model;
667 Indexable *background;
670 RenderEngine *render_engine;
671 CICache *video_cache;
672 OverlayFrame *overlay_frame;
674 int64_t last_position;
675 int need_reconfigure;