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
22 // Originally developed by Heroine Virtual Ltd.
23 // Support for multiple encodings, outline (stroke) by
24 // Andraz Tori <Andraz.tori1@guest.arnes.si>
25 // Additional support for UTF-8 by
26 // Paolo Rampino aka Akirad <info at tuttoainternet.it>
30 #include "bccmodels.h"
31 #include "bcsignals.h"
36 #include "edlsession.h"
39 #include "filesystem.h"
40 #include "indexable.h"
47 #include "mwindow.inc"
48 #include "overlayframe.h"
49 #include "renderengine.h"
51 #include "titlerwindow.h"
52 #include "transportque.h"
65 #include <fontconfig/fontconfig.h>
67 #define FIXED_FONT "Bitstream Vera Sans Mono (Bits)"
68 #define SMALL (1.0 / 64.0)
70 #define MAX_FLT 3.40282347e+38
71 #define MIN_FLT -3.40282347e+38
73 REGISTER_PLUGIN(TitleMain)
75 #ifdef X_HAVE_UTF8_STRING
76 #define DEFAULT_ENCODING "UTF-8"
78 #define DEFAULT_ENCODING "ISO8859-1"
80 #define DEFAULT_TIMECODEFORMAT TIME_HMS
82 TitleConfig::TitleConfig()
89 motion_strategy = NO_MOTION;
92 hjustification = JUSTIFY_CENTER;
93 vjustification = JUSTIFY_MID;
96 title_x = title_y = 0.0;
97 title_w = title_h = 0;
99 strcpy(font, "fixed");
100 strcpy(encoding, DEFAULT_ENCODING);
101 timecode_format = DEFAULT_TIMECODEFORMAT;
102 pixels_per_second = 1.0;
105 wtext[0] = 0; wlen = 0;
106 color_stroke = 0xff0000;
107 outline_color = WHITE;
109 strcpy(background_path, "");
114 next_keyframe_position = 0;
115 prev_keyframe_position = 0;
120 TitleConfig::~TitleConfig()
124 int TitleConfig::equivalent(TitleConfig &that)
126 return !strcasecmp(font, that.font) &&
127 style == that.style &&
129 color == that.color &&
130 color_stroke == that.color_stroke &&
131 stroke_width == that.stroke_width &&
132 outline_color == that.outline_color &&
133 alpha == that.alpha &&
134 outline_alpha == that.outline_alpha &&
135 EQUIV(pixels_per_second, that.pixels_per_second) &&
136 motion_strategy == that.motion_strategy &&
138 line_pitch == that.line_pitch &&
139 hjustification == that.hjustification &&
140 vjustification == that.vjustification &&
141 fade_in == that.fade_in && fade_out == that.fade_out &&
142 title_x == that.title_x && title_y == that.title_y &&
143 title_w == that.title_w && title_h == that.title_h &&
144 dropshadow == that.dropshadow &&
145 timecode == that.timecode &&
146 timecode_format == that.timecode_format &&
147 outline_size == that.outline_size &&
148 !strcasecmp(encoding, that.encoding) &&
150 !memcmp(wtext, that.wtext, wlen * sizeof(wchar_t));
153 void TitleConfig::copy_from(TitleConfig &that)
155 strcpy(font, that.font);
159 color_stroke = that.color_stroke;
160 stroke_width = that.stroke_width;
161 outline_color = that.outline_color;
163 outline_alpha = that.outline_alpha;
164 pixels_per_second = that.pixels_per_second;
165 motion_strategy = that.motion_strategy;
167 line_pitch = that.line_pitch;
168 hjustification = that.hjustification;
169 vjustification = that.vjustification;
170 fade_in = that.fade_in;
171 fade_out = that.fade_out;
172 title_x = that.title_x;
173 title_y = that.title_y;
174 title_w = that.title_w;
175 title_h = that.title_h;
176 dropshadow = that.dropshadow;
177 timecode = that.timecode;
178 timecode_format = that.timecode_format;
179 outline_size = that.outline_size;
180 strcpy(encoding, that.encoding);
181 memcpy(wtext, that.wtext, that.wlen * sizeof(wchar_t));
183 window_w = that.window_w;
184 window_h = that.window_h;
187 void TitleConfig::interpolate(TitleConfig &prev, TitleConfig &next,
188 int64_t prev_frame, int64_t next_frame, int64_t current_frame)
190 strcpy(font, prev.font);
191 strcpy(encoding, prev.encoding);
195 color_stroke = prev.color_stroke;
196 stroke_width = prev.stroke_width;
197 outline_color = prev.outline_color;
199 outline_alpha = prev.outline_alpha;
200 motion_strategy = prev.motion_strategy;
202 line_pitch = prev.line_pitch;
203 hjustification = prev.hjustification;
204 vjustification = prev.vjustification;
205 fade_in = prev.fade_in;
206 fade_out = prev.fade_out;
207 outline_size = prev.outline_size;
208 pixels_per_second = prev.pixels_per_second;
209 memcpy(wtext, prev.wtext, prev.wlen * sizeof(wchar_t));
213 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
214 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
215 this->title_x = prev.title_x * prev_scale + next.title_x * next_scale;
216 this->title_y = prev.title_y * prev_scale + next.title_y * next_scale;
217 // this->title_x = prev.title_x;
218 // this->title_y = prev.title_y;
219 timecode = prev.timecode;
220 timecode_format = prev.timecode_format;
221 this->dropshadow = prev.dropshadow * prev_scale + next.dropshadow * next_scale;
222 // this->dropshadow = prev.dropshadow;
225 void TitleConfig::to_wtext(const char *from_enc, const char *text, int tlen)
227 wlen = BC_Resources::encode(from_enc, BC_Resources::wide_encoding,
228 (char*)text,tlen, (char *)wtext,sizeof(wtext)) / sizeof(wchar_t);
229 while( wlen > 0 && !wtext[wlen-1] ) --wlen;
233 TitleGlyph::TitleGlyph()
252 TitleGlyph::~TitleGlyph()
254 //printf("TitleGlyph::~TitleGlyph 1\n");
255 if( data ) delete data;
256 if( data_stroke ) delete data_stroke;
260 GlyphPackage::GlyphPackage() : LoadPackage()
265 GlyphUnit::GlyphUnit(TitleMain *plugin, GlyphEngine *server)
268 this->plugin = plugin;
269 freetype_library = 0;
273 GlyphUnit::~GlyphUnit()
275 if( freetype_library )
276 FT_Done_FreeType(freetype_library);
279 void GlyphUnit::process_package(LoadPackage *package)
281 GlyphPackage *pkg = (GlyphPackage*)package;
282 TitleGlyph *glyph = pkg->glyph;
284 char new_path[BCTEXTLEN];
285 if( plugin->load_freetype_face(freetype_library, freetype_face, glyph->font->path) ) {
286 printf(_("GlyphUnit::process_package FT_New_Face failed, path=%s\n"),
292 int gindex = FT_Get_Char_Index(freetype_face, glyph->char_code);
294 //printf("GlyphUnit::process_package 1 %c\n", glyph->char_code);
297 BC_Resources *resources = BC_WindowBase::get_resources();
298 // Search replacement font
299 if( resources->find_font_by_char(glyph->char_code, new_path, freetype_face) ) {
300 plugin->load_freetype_face(freetype_library,
301 freetype_face, new_path);
302 gindex = FT_Get_Char_Index(freetype_face, glyph->char_code);
305 FT_Set_Pixel_Sizes(freetype_face, glyph->size, 0);
309 if( glyph->char_code != 10 )
310 printf(_("GlyphUnit::process_package FT_Load_Char failed - char: %li.\n"),
312 // Prevent a crash here
313 glyph->width = 8; glyph->height = 8;
314 glyph->pitch = 8; glyph->advance_x = 8;
315 glyph->left = 9; glyph->top = 9;
316 glyph->right = 0; glyph->bottom = 0;
317 glyph->freetype_index = 0;
318 glyph->data = new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch);
319 glyph->data->clear_frame();
320 glyph->data_stroke = 0;
322 // create outline glyph
323 if( plugin->config.stroke_width >= SMALL &&
324 (plugin->config.style & BC_FONT_OUTLINE) ) {
325 glyph->data_stroke = new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch);
326 glyph->data_stroke->clear_frame();
329 // char found and no outline desired
330 else if( plugin->config.stroke_width < SMALL ||
331 !(plugin->config.style & BC_FONT_OUTLINE) ) {
332 FT_Glyph glyph_image;
335 FT_Load_Glyph(freetype_face, gindex, FT_LOAD_DEFAULT);
336 FT_Get_Glyph(freetype_face->glyph, &glyph_image);
337 FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph_image)->outline, &bbox);
338 // printf("Stroke: Xmin: %ld, Xmax: %ld, Ymin: %ld, yMax: %ld\n",
339 // bbox.xMin,bbox.xMax, bbox.yMin, bbox.yMax);
341 glyph->width = bm.width = ((bbox.xMax - bbox.xMin + 63) >> 6);
342 glyph->height = bm.rows = ((bbox.yMax - bbox.yMin + 63) >> 6);
343 glyph->pitch = bm.pitch = bm.width;
344 bm.pixel_mode = FT_PIXEL_MODE_GRAY;
346 glyph->left = (bbox.xMin + 31) >> 6;
347 glyph->top = (bbox.yMax + 31) >> 6;
348 glyph->right = (bbox.xMax + 31) >> 6;
349 glyph->bottom = (bbox.yMin + 31) >> 6;
350 glyph->freetype_index = gindex;
351 glyph->advance_x = ((freetype_face->glyph->advance.x + 31) >> 6);
352 //printf("GlyphUnit::process_package 1 width=%d height=%d pitch=%d left=%d top=%d advance_x=%d freetype_index=%d\n",
353 //glyph->width, glyph->height, glyph->pitch, glyph->left, glyph->top, glyph->advance_x, glyph->freetype_index);
355 glyph->data = new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch);
356 glyph->data->clear_frame();
357 bm.buffer = glyph->data->get_data();
358 FT_Outline_Translate(&((FT_OutlineGlyph) glyph_image)->outline,
359 - bbox.xMin, - bbox.yMin);
360 FT_Outline_Get_Bitmap( freetype_library,
361 &((FT_OutlineGlyph) glyph_image)->outline,
363 FT_Done_Glyph(glyph_image);
366 // Outline desired and glyph found
367 FT_Glyph glyph_image;
372 FT_UInt npoints, ncontours;
374 FT_Load_Glyph(freetype_face, gindex, FT_LOAD_DEFAULT);
375 FT_Get_Glyph(freetype_face->glyph, &glyph_image);
377 // check if the outline is ok (non-empty);
378 FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph_image)->outline, &bbox);
379 if( bbox.xMin == 0 && bbox.xMax == 0 &&
380 bbox.yMin == 0 && bbox.yMax == 0 ) {
381 FT_Done_Glyph(glyph_image);
382 glyph->width = 0; glyph->height = 0;
383 glyph->left = 0; glyph->top = 0;
384 glyph->right = 0; glyph->bottom = 0;
387 new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch);
389 new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch);
390 glyph->advance_x =((int)(freetype_face->glyph->advance.x +
391 plugin->config.stroke_width * 64)) >> 6;
394 #if FREETYPE_MAJOR > 2 || (FREETYPE_MAJOR == 2 && FREETYPE_MINOR >= 2)
395 FT_Stroker_New(freetype_library, &stroker);
397 FT_Stroker_New(((FT_LibraryRec *)freetype_library)->memory, &stroker);
399 FT_Stroker_Set(stroker, (int)(plugin->config.stroke_width * 64),
400 FT_STROKER_LINECAP_ROUND, FT_STROKER_LINEJOIN_ROUND, 0);
401 FT_Stroker_ParseOutline(stroker, &((FT_OutlineGlyph) glyph_image)->outline,1);
402 FT_Stroker_GetCounts(stroker,&npoints, &ncontours);
403 if( npoints == 0 && ncontours == 0 ) {
404 // this never happens, but FreeType has a bug regarding Linotype's Palatino font
405 FT_Stroker_Done(stroker);
406 FT_Done_Glyph(glyph_image);
407 glyph->width = 0; glyph->height = 0;
408 glyph->left = 0; glyph->top = 0;
409 glyph->right = 0; glyph->bottom = 0;
412 new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch);
414 new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch);
415 glyph->advance_x =((int)(freetype_face->glyph->advance.x +
416 plugin->config.stroke_width * 64)) >> 6;
420 FT_Outline_New(freetype_library, npoints, ncontours, &outline);
422 outline.n_contours=0;
423 FT_Stroker_Export (stroker, &outline);
424 FT_Outline_Get_BBox(&outline, &bbox);
425 FT_Outline_Translate(&outline, - bbox.xMin, - bbox.yMin);
426 FT_Outline_Translate(&((FT_OutlineGlyph) glyph_image)->outline,
428 - bbox.yMin + (int)(plugin->config.stroke_width*32));
429 // printf("Stroke: Xmin: %ld, Xmax: %ld, Ymin: %ld, yMax: %ld\n"
430 // "Fill Xmin: %ld, Xmax: %ld, Ymin: %ld, yMax: %ld\n",
431 // bbox.xMin,bbox.xMax, bbox.yMin, bbox.yMax,
432 // bbox_fill.xMin,bbox_fill.xMax, bbox_fill.yMin, bbox_fill.yMax);
434 glyph->width = bm.width = ((bbox.xMax - bbox.xMin) >> 6)+1;
435 glyph->height = bm.rows = ((bbox.yMax - bbox.yMin) >> 6) +1;
436 glyph->pitch = bm.pitch = bm.width;
437 bm.pixel_mode = FT_PIXEL_MODE_GRAY;
439 glyph->left = (bbox.xMin + 31) >> 6;
440 glyph->top = (bbox.yMax + 31) >> 6;
441 glyph->right = (bbox.xMax + 31) >> 6;
442 glyph->bottom = (bbox.yMin + 31) >> 6;
443 glyph->freetype_index = gindex;
444 int real_advance = ((int)ceil((float)freetype_face->glyph->advance.x +
445 plugin->config.stroke_width * 64) >> 6);
446 glyph->advance_x = glyph->width + glyph->left;
447 if( real_advance > glyph->advance_x )
448 glyph->advance_x = real_advance;
449 //printf("GlyphUnit::process_package 1 width=%d height=%d "
450 // "pitch=%d left=%d top=%d advance_x=%d freetype_index=%d\n",
451 // glyph->width, glyph->height, glyph->pitch, glyph->left,
452 // glyph->top, glyph->advance_x, glyph->freetype_index);
455 //printf("GlyphUnit::process_package 1\n");
456 glyph->data = new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch);
457 glyph->data->clear_frame();
458 glyph->data_stroke = new VFrame(glyph->width, glyph->height, BC_A8, glyph->pitch);
459 glyph->data_stroke->clear_frame();
460 // for debugging memset( glyph->data_stroke->get_data(), 60, glyph->pitch * glyph->height);
461 bm.buffer=glyph->data->get_data();
462 FT_Outline_Get_Bitmap( freetype_library,
463 &((FT_OutlineGlyph) glyph_image)->outline,
465 bm.buffer=glyph->data_stroke->get_data();
466 FT_Outline_Get_Bitmap( freetype_library,
469 FT_Outline_Done(freetype_library,&outline);
470 FT_Stroker_Done(stroker);
471 FT_Done_Glyph(glyph_image);
473 //printf("GlyphUnit::process_package 2\n");
479 GlyphEngine::GlyphEngine(TitleMain *plugin, int cpus)
480 : LoadServer(cpus, cpus)
482 this->plugin = plugin;
485 void GlyphEngine::init_packages()
487 int current_package = 0;
488 for( int i=0; i<plugin->title_glyphs.count(); ++i ) {
489 if( !plugin->title_glyphs[i]->data ) {
490 GlyphPackage *pkg = (GlyphPackage*)get_package(current_package++);
491 pkg->glyph = plugin->title_glyphs[i];
496 LoadClient* GlyphEngine::new_client()
498 return new GlyphUnit(plugin, this);
501 LoadPackage* GlyphEngine::new_package()
503 return new GlyphPackage;
507 // Copy a single character to the text mask
508 TitlePackage::TitlePackage()
516 TitleUnit::TitleUnit(TitleMain *plugin, TitleEngine *server)
519 this->plugin = plugin;
520 this->engine = server;
524 static void get_mask_colors(int rgb, int color_model, int &rr, int &gg, int &bb)
526 int r = 0xff & (rgb>>16), g = 0xff & (rgb>>8), b = 0xff & (rgb>>0);
527 if( BC_CModels::is_yuv(color_model) ) bc_rgb2yuv(r,g,b, r,g,b);
528 rr = r; gg = g; bb = b;
531 void TitleUnit::draw_frame(int mode, VFrame *dst, VFrame *src, int x, int y)
533 int inp_w = src->get_w(), inp_h = src->get_h();
534 int out_w = dst->get_w(), out_h = dst->get_h();
535 unsigned char **inp_rows = src->get_rows();
536 unsigned char **out_rows = dst->get_rows();
538 int x_inp = 0, y_inp = 0;
540 if( x_out < 0 ) { x_inp = -x_out; x_out = 0; }
541 if( x_out+inp_w > out_w ) inp_w = out_w-x_out;
542 if( x_inp >= inp_w || x_out >= out_w ) return;
544 if( y_out < 0 ) { y_inp = -y_out; y_out = 0; }
545 if( y_out+inp_h > out_h ) inp_h = out_h-y_out;
546 if( y_inp >= inp_h || y_out >= out_h ) return;
547 int color = chr->color, max = 0xff;
548 int alpha = chr->alpha * chr->fade;
549 int ofs = BC_CModels::is_yuv(dst->get_color_model()) ? 0x80 : 0x00;
553 while( y_inp < inp_h && y_out < out_h ) {
554 uint8_t *inp = inp_rows[y_inp], *out = out_rows[y_out];
555 for( int xin=x_inp,xout=x_out*4+3; xin<inp_w; ++xin,xout+=4 ) {
556 out[xout] = inp[xin];
562 int r = 0, g = 0, b = 0;
563 get_mask_colors(color, plugin->text_model, r, g, b);
564 while( y_inp < inp_h && y_out < out_h ) {
565 uint8_t *inp = inp_rows[y_inp], *out = out_rows[y_out];
566 for( int xin=x_inp,xout=x_out*4+0; xin<inp_w; ++xin,xout+=4 ) {
567 int in_a = inp[xin], out_a = out[xout+3];
568 if( in_a + out_a == 0 ) continue;
569 int opacity = (in_a * alpha)/max, transp = out_a * (max - opacity)/max;
570 out[xout+0] = (opacity * r + transp * out[xout+0]) / max;
571 out[xout+1] = (opacity * (g-ofs) + transp * (out[xout+1]-ofs)) / max + ofs;
572 out[xout+2] = (opacity * (b-ofs) + transp * (out[xout+2]-ofs)) / max + ofs;
573 out[xout+3] = opacity + transp - (opacity * transp) / max;
579 while( y_inp < inp_h && y_out < out_h ) {
580 uint8_t *inp = inp_rows[y_inp], *out = out_rows[y_out];
581 for( int xin=x_inp,xout=x_out*4+0; xin<inp_w; ++xin,xout+=4 ) {
582 int xinp = xin*4, in_a = inp[xinp+3], out_a = out[xout+3];
583 if( in_a + out_a == 0 ) continue;
584 int opacity = (in_a * alpha)/max, transp = out_a * (max - opacity)/max;
585 out[xout+0] = (opacity * inp[xinp+0] + transp * out[xout+0]) / max;
586 out[xout+1] = (opacity * (inp[xinp+1]-ofs) + transp * (out[xout+1]-ofs)) / max + ofs;
587 out[xout+2] = (opacity * (inp[xinp+2]-ofs) + transp * (out[xout+2]-ofs)) / max + ofs;
588 out[xout+3] = opacity + transp - (opacity * transp) / max;
597 void TitleUnit::process_package(LoadPackage *package)
599 TitlePackage *pkg = (TitlePackage*)package;
600 int x = pkg->x, y = pkg->y;
604 VFrame *vframe = (VFrame *)chr->vp;
605 if( !vframe ) return;
606 draw_frame(DRAW_IMAGE, plugin->text_mask, vframe, x, y);
609 if( chr->wch == 0 || chr->wch == '\n') return;
610 TitleGlyph *glyph = (TitleGlyph *)chr->vp;
612 if( engine->do_dropshadow ) {
613 x += plugin->config.dropshadow;
614 y += plugin->config.dropshadow;
616 else if( plugin->config.dropshadow < 0 ) {
617 x -= plugin->config.dropshadow;
618 y -= plugin->config.dropshadow;
620 int mode = engine->do_dropshadow ? DRAW_ALPHA : DRAW_COLOR;
621 draw_frame(mode, plugin->text_mask, glyph->data, x, y);
622 if( plugin->config.stroke_width >= SMALL && (plugin->config.style & BC_FONT_OUTLINE) )
623 draw_frame(mode, plugin->stroke_mask, glyph->data_stroke, x, y);
628 TitleEngine::TitleEngine(TitleMain *plugin, int cpus)
629 : LoadServer(cpus, cpus)
631 this->plugin = plugin;
634 void TitleEngine::init_packages()
636 int current_package = 0;
637 for( int i=plugin->visible_char1; i<plugin->visible_char2; ++i ) {
638 TitlePackage *pkg = (TitlePackage*)get_package(current_package++);
639 TitleChar *chr = plugin->title_chars[i];
640 TitleRow *row = plugin->title_rows[chr->row];
642 pkg->x = row->x0 + chr->x - plugin->mask_x1;
643 pkg->y = row->y0 - chr->y - plugin->mask_y1;
644 //printf("draw '%c' at %d,%d\n",(int)pkg->chr->wch, pkg->x, pkg->y);
648 LoadClient* TitleEngine::new_client()
650 return new TitleUnit(plugin, this);
653 LoadPackage* TitleEngine::new_package()
655 return new TitlePackage;
658 void TitleTranslateUnit::translation_array_f(transfer_table_f* &table,
659 float in_x1, float in_x2, int in_total,
660 float out_x1, float out_x2, int out_total,
661 int &x1_out, int &x2_out)
664 //float offset = out_x1 - in_x1;
666 x1_out = (int)out_x1;
667 x2_out = MIN((int)ceil(out_x2), out_total);
668 if( x1_out >= x2_out ) return;
670 out_w = x2_out - x1_out;
671 table = new transfer_table_f[out_w];
672 bzero(table, sizeof(transfer_table_f) * out_w);
675 for( int out_x=x1_out; out_x<x2_out; ++out_x ) {
676 transfer_table_f *entry = &table[out_x - x1_out];
678 entry->in_x1 = (int)in_x;
679 entry->in_x2 = (int)in_x + 1;
681 // Get fraction of output pixel to fill
682 entry->output_fraction = 1;
684 if( out_x1 > out_x ) {
685 entry->output_fraction -= out_x1 - out_x;
688 if( out_x2 < out_x + 1 ) {
689 entry->output_fraction = (out_x2 - out_x);
692 // Advance in_x until out_x_fraction is filled
693 float out_x_fraction = entry->output_fraction;
694 float in_x_fraction = floor(in_x + 1) - in_x;
696 if( out_x_fraction <= in_x_fraction ) {
697 entry->in_fraction1 = out_x_fraction;
698 entry->in_fraction2 = 0.0;
699 in_x += out_x_fraction;
702 entry->in_fraction1 = in_x_fraction;
703 in_x += out_x_fraction;
704 entry->in_fraction2 = in_x - floor(in_x);
707 // Clip in_x and zero out fraction. This doesn't work for YUV.
708 if( entry->in_x2 >= in_total ) {
709 entry->in_x2 = in_total - 1;
710 entry->in_fraction2 = 0.0;
713 if( entry->in_x1 >= in_total ) {
714 entry->in_x1 = in_total - 1;
715 entry->in_fraction1 = 0.0;
722 // Copy a single character to the text mask
723 TitleOutlinePackage::TitleOutlinePackage()
729 TitleOutlineUnit::TitleOutlineUnit(TitleMain *plugin, TitleOutlineEngine *server)
732 this->plugin = plugin;
733 this->engine = server;
736 void TitleOutlineUnit::process_package(LoadPackage *package)
738 TitleOutlinePackage *pkg = (TitleOutlinePackage*)package;
739 int r = 0, g = 0, b = 0, outline_a = plugin->config.outline_alpha;
740 get_mask_colors(plugin->config.outline_color, plugin->mask_model, r, g, b);
742 unsigned char **outline_rows = plugin->outline_mask->get_rows();
743 unsigned char **text_rows = plugin->text_mask->get_rows();
744 int mask_w1 = plugin->text_mask->get_w()-1;
745 int mask_h1 = plugin->text_mask->get_h()-1;
746 int ofs = plugin->config.outline_size;
748 if( engine->pass == 0 ) {
749 // get max alpha under outline size macropixel
750 for( int y=pkg->y1, my=pkg->y2; y<my; ++y ) {
751 unsigned char *out_row = outline_rows[y];
752 int y1 = y - ofs, y2 = y + ofs;
753 CLAMP(y1, 0, mask_h1); CLAMP(y2, 0, mask_h1);
754 for( int x=0, mx=plugin->text_mask->get_w(); x<mx; ++x ) {
755 int x1 = x - ofs, x2 = x + ofs;
756 CLAMP(x1, 0, mask_w1); CLAMP(x2, 0, mask_w1);
758 for( int yy=y1; yy<=y2; ++yy ) {
759 unsigned char *text_row = text_rows[yy];
760 for( int xx = x1; xx <= x2; ++xx ) {
761 unsigned char *pixel = text_row + xx*4;
762 if( pixel[3] > max_a ) max_a = pixel[3];
765 unsigned char *out = out_row + x*4;
766 out[0] = r; out[1] = g; out[2] = b;
767 out[3] = (max_a * outline_a) / 0xff;
773 // Overlay text mask on top of outline mask
774 int ofs = BC_CModels::is_yuv(plugin->output->get_color_model()) ? 0x80 : 0;
775 for( int y=pkg->y1, my=pkg->y2; y<my; ++y ) {
776 unsigned char *outline_row = outline_rows[y];
777 unsigned char *text_row = text_rows[y];
778 for( int x=0, mx=plugin->text_mask->get_w(); x<mx; ++x ) {
779 unsigned char *out = text_row + x * 4;
780 unsigned char *inp = outline_row + x * 4;
781 int out_a = out[3], in_a = inp[3];
782 int transparency = in_a * (0xff - out_a) / 0xff;
783 out[0] = (out[0] * out_a + inp[0] * transparency) / 0xff;
784 out[1] = ((out[1]-ofs) * out_a + (inp[1]-ofs) * transparency) / 0xff + ofs;
785 out[2] = ((out[2]-ofs) * out_a + (inp[2]-ofs) * transparency) / 0xff + ofs;
786 out[3] = in_a + out_a - in_a*out_a / 0xff;
792 TitleOutlineEngine::TitleOutlineEngine(TitleMain *plugin, int cpus)
793 : LoadServer(cpus, cpus)
795 this->plugin = plugin;
798 void TitleOutlineEngine::init_packages()
800 int mask_h = plugin->text_mask->get_h();
801 if( !mask_h ) return;
802 int py1 = 0, py2 = 0;
803 int pkgs = get_total_packages();
804 for( int i=0; i<pkgs; py1=py2 ) {
805 TitleOutlinePackage *pkg = (TitleOutlinePackage*)get_package(i);
806 py2 = (++i * mask_h)/ pkgs;
807 pkg->y1 = py1; pkg->y2 = py2;
811 void TitleOutlineEngine::do_outline()
813 pass = 0; process_packages();
814 pass = 1; process_packages();
817 LoadClient* TitleOutlineEngine::new_client()
819 return new TitleOutlineUnit(plugin, this);
822 LoadPackage* TitleOutlineEngine::new_package()
824 return new TitleOutlinePackage;
829 TitleTranslatePackage::TitleTranslatePackage()
836 TitleTranslateUnit::TitleTranslateUnit(TitleMain *plugin, TitleTranslate *server)
839 this->plugin = plugin;
842 void TitleTranslate::run_packages()
844 // Generate scaling tables
845 delete [] x_table; x_table = 0;
846 delete [] y_table; y_table = 0;
848 float in_w = xlat_mask->get_w();
849 float in_h = xlat_mask->get_h();
850 float ix1 = 0, ix2 = ix1 + in_w;
851 float iy1 = 0, iy2 = iy1 + in_h;
853 float out_w = plugin->output->get_w();
854 float out_h = plugin->output->get_h();
855 float x1 = plugin->title_x, x2 = x1 + plugin->title_w;
856 float y1 = plugin->title_y, y2 = y1 + plugin->title_h;
857 bclamp(x1, 0, out_w); bclamp(y1, 0, out_h);
858 bclamp(x2, 0, out_w); bclamp(y2, 0, out_h);
860 float ox1 = plugin->title_x + plugin->text_x - plugin->text_x1 + plugin->mask_x1, ox2 = ox1 + in_w;
861 float oy1 = plugin->title_y + plugin->text_y - plugin->text_y1 + plugin->mask_y1, oy2 = oy1 + in_h;
862 if( ox1 < x1 ) { ix1 -= (ox1-x1); ox1 = x1; }
863 if( oy1 < y1 ) { iy1 -= (oy1-y1); oy1 = y1; }
864 if( ox2 > x2 ) { ix2 -= (ox2-x2); ox2 = x2; }
865 if( oy2 > y2 ) { iy2 -= (oy2-x2); oy2 = y2; }
867 printf("TitleTranslate text txy=%7.2f,%-7.2f\n"
868 " mxy1=%7d,%-7d mxy2=%7d,%-7d\n"
869 " xy1=%7.2f,%-7.2f xy2=%7.2f,%-7.2f\n"
870 " ixy1=%7.2f,%-7.2f ixy2=%7.2f,%-7.2f\n"
871 " oxy1=%7.2f,%-7.2f oxy2=%7.2f,%-7.2f\n",
872 plugin->text_x, plugin->text_y,
873 plugin->mask_x1, plugin->mask_y1, plugin->mask_x2, plugin->mask_y2,
874 x1,y1, x2,y2, ix1,iy1, ix2,iy2, ox1,oy1, ox2,oy2);
876 out_x1 = out_x2 = out_y1 = out_y2 = 0;
877 TitleTranslateUnit::translation_array_f(x_table,
878 ix1, ix2, in_w, ox1, ox2, out_w, out_x1, out_x2);
879 TitleTranslateUnit::translation_array_f(y_table,
880 iy1, iy2, in_h, oy1, oy2, out_h, out_y1, out_y2);
887 #define TRANSLATE(type, max, components, ofs) { \
888 unsigned char **in_rows = server->xlat_mask->get_rows(); \
889 type **out_rows = (type**)plugin->output->get_rows(); \
891 for( int y=pkg->y1; y<pkg->y2; ++y ) { \
892 int in_y1 = server->y_table[y].in_x1; \
893 int in_y2 = server->y_table[y].in_x2; \
894 float y_f1 = server->y_table[y].in_fraction1; \
895 float y_f2 = server->y_table[y].in_fraction2; \
896 unsigned char *in_row1 = in_rows[in_y1]; \
897 unsigned char *in_row2 = in_rows[in_y2]; \
898 type *out_row = out_rows[y + server->out_y1]; \
899 for( int i=0,x=server->out_x1; x<server->out_x2; ++i,++x ) { \
900 int in_x1 = 4*server->x_table[i].in_x1; \
901 int in_x2 = 4*server->x_table[i].in_x2; \
902 float x_f1 = server->x_table[i].in_fraction1; \
903 float x_f2 = server->x_table[i].in_fraction2; \
904 float f11 = x_f1 * y_f1 / (256.f-max); \
905 float f12 = x_f2 * y_f1 / (256.f-max); \
906 float f21 = x_f1 * y_f2 / (256.f-max); \
907 float f22 = x_f2 * y_f2 / (256.f-max); \
908 type input_r = (type)( \
909 in_row1[in_x1 + 0] * f11 + in_row1[in_x2 + 0] * f12 + \
910 in_row2[in_x1 + 0] * f21 + in_row2[in_x2 + 0] * f22 ); \
911 type input_g = (type)( \
912 in_row1[in_x1 + 1] * f11 + in_row1[in_x2 + 1] * f12 + \
913 in_row2[in_x1 + 1] * f21 + in_row2[in_x2 + 1] * f22 ); \
914 type input_b = (type)( \
915 in_row1[in_x1 + 2] * f11 + in_row1[in_x2 + 2] * f12 + \
916 in_row2[in_x1 + 2] * f21 + in_row2[in_x2 + 2] * f22 ); \
917 type input_a = (type)( \
918 in_row1[in_x1 + 3] * f11 + in_row1[in_x2 + 3] * f12 + \
919 in_row2[in_x1 + 3] * f21 + in_row2[in_x2 + 3] * f22 ); \
920 input_a = input_a * plugin->fade; \
921 if( components == 4 ) { \
922 type transparency = out_row[x * components + 3] * (max - input_a) / max; \
923 out_row[x * components + 0] = (input_r * input_a + \
924 out_row[x * components + 0] * transparency) / max; \
925 out_row[x * components + 1] = ((input_g-ofs) * input_a + \
926 (out_row[x * components + 1]-ofs) * transparency) / max + ofs; \
927 out_row[x * components + 2] = ((input_b-ofs) * input_a + \
928 (out_row[x * components + 2]-ofs) * transparency) / max + ofs; \
929 out_row[x * components + 3] = MAX(input_a, out_row[x * components + 3]); \
932 type transparency = max - input_a; \
933 out_row[x * components + 0] = (input_r * input_a + \
934 out_row[x * components + 0] * transparency) / max; \
935 out_row[x * components + 1] = ((input_g-ofs) * input_a + \
936 (out_row[x * components + 1]-ofs) * transparency) / max + ofs; \
937 out_row[x * components + 2] = ((input_b-ofs) * input_a + \
938 (out_row[x * components + 2]-ofs) * transparency) / max + ofs; \
945 void TitleTranslateUnit::process_package(LoadPackage *package)
947 TitleTranslatePackage *pkg = (TitleTranslatePackage*)package;
948 TitleTranslate *server = (TitleTranslate*)this->server;
950 switch( plugin->output->get_color_model() ) {
951 case BC_RGB888: TRANSLATE(unsigned char, 0xff, 3, 0); break;
952 case BC_RGB_FLOAT: TRANSLATE(float, 1.0, 3, 0); break;
953 case BC_YUV888: TRANSLATE(unsigned char, 0xff, 3, 0x80); break;
954 case BC_RGBA_FLOAT: TRANSLATE(float, 1.0, 4, 0); break;
955 case BC_RGBA8888: TRANSLATE(unsigned char, 0xff, 4, 0); break;
956 case BC_YUVA8888: TRANSLATE(unsigned char, 0xff, 4, 0x80); break;
958 //printf("TitleTranslateUnit::process_package 5\n");
962 TitleTranslate::TitleTranslate(TitleMain *plugin, int cpus)
963 : LoadServer(cpus, cpus)
965 this->plugin = plugin;
972 TitleTranslate::~TitleTranslate()
978 void TitleTranslate::init_packages()
980 int out_h = out_y2 - out_y1;
981 int py1 = 0, py2 = 0;
982 int pkgs = get_total_packages();
983 for( int i=0; i<pkgs; py1=py2 ) {
984 TitleTranslatePackage *pkg = (TitleTranslatePackage*)get_package(i);
985 py2 = (++i*out_h) / pkgs;
986 pkg->y1 = py1; pkg->y2 = py2;
988 //printf("TitleTranslate::init_packages 2\n");
991 LoadClient* TitleTranslate::new_client()
993 return new TitleTranslateUnit(plugin, this);
996 LoadPackage* TitleTranslate::new_package()
998 return new TitleTranslatePackage;
1001 TitleCurNudge::TitleCurNudge(TitleParser *parser, TitleMain *plugin)
1002 : TitleStack<int>(parser, 0)
1005 TitleCurColor::TitleCurColor(TitleParser *parser, TitleMain *plugin)
1006 : TitleStack<int>(parser, plugin->config.color)
1009 TitleCurAlpha::TitleCurAlpha(TitleParser *parser, TitleMain *plugin)
1010 : TitleStack<int>(parser, plugin->config.alpha)
1013 TitleCurSize::TitleCurSize(TitleParser *parser, TitleMain *plugin)
1014 : TitleStack<float>(parser, plugin->config.size)
1017 TitleCurBold::TitleCurBold(TitleParser *parser, TitleMain *plugin)
1018 : TitleStack<int>(parser, (plugin->config.style & BC_FONT_BOLD) ? 1 : 0)
1021 TitleCurItalic::TitleCurItalic(TitleParser *parser, TitleMain *plugin)
1022 : TitleStack<int>(parser, (plugin->config.style & BC_FONT_ITALIC) ? 1 : 0)
1025 TitleCurFont::TitleCurFont(TitleParser *parser, TitleMain *plugin)
1026 : TitleStack<BC_FontEntry*>(parser, plugin->config_font())
1029 TitleCurCaps::TitleCurCaps(TitleParser *parser, TitleMain *plugin)
1030 : TitleStack<int>(parser, 0)
1033 TitleCurUnder::TitleCurUnder(TitleParser *parser, TitleMain *plugin)
1034 : TitleStack<int>(parser, 0)
1037 TitleCurBlink::TitleCurBlink(TitleParser *parser, TitleMain *plugin)
1038 : TitleStack<float>(parser, 0)
1041 TitleCurFixed::TitleCurFixed(TitleParser *parser, TitleMain *plugin)
1042 : TitleStack<int>(parser, 0)
1045 TitleCurSuper::TitleCurSuper(TitleParser *parser, TitleMain *plugin)
1046 : TitleStack<int>(parser, 0)
1050 TitleParser::TitleParser(TitleMain *plugin)
1052 cur_nudge(this, plugin),
1053 cur_color(this, plugin),
1054 cur_alpha(this, plugin),
1055 cur_size(this, plugin),
1056 cur_bold(this, plugin),
1057 cur_italic(this, plugin),
1058 cur_font(this, plugin),
1059 cur_caps(this, plugin),
1060 cur_under(this, plugin),
1061 cur_blink(this, plugin),
1062 cur_fixed(this, plugin),
1063 cur_super(this, plugin)
1065 bfr = out = plugin->config.wtext;
1066 lmt = bfr + plugin->config.wlen;
1069 TitleMain::TitleMain(PluginServer *server)
1070 : PluginVClient(server)
1079 freetype_library = 0;
1082 window_w = window_h = 0;
1083 title_x = title_y = 0;
1084 title_w = title_h = 0;
1085 text_w = text_h = 0;
1086 text_x = text_y = 0; mask_w = mask_h = 0;
1087 mask_x1 = mask_y1 = mask_x2 = mask_y2 = 0;
1089 visible_row1 = visible_char1 = 0;
1090 visible_row2 = visible_char2 = 0;
1092 input = 0; output = 0;
1093 output_model = BC_RGBA8888;
1094 mask_model = BC_RGBA8888;
1095 background = 0; bg_file = 0; bg_frame = 0;
1096 render_engine = 0; video_cache = 0;
1098 cpus = PluginClient::smp + 1;
1099 if( cpus > 8 ) cpus = 8;
1101 need_reconfigure = 1;
1104 TitleMain::~TitleMain()
1107 background->Garbage::remove_user();
1110 delete render_engine;
1112 delete overlay_frame;
1116 delete outline_mask;
1118 delete glyph_engine;
1119 delete title_engine;
1121 FT_Done_Face(freetype_face);
1122 if( freetype_library )
1123 FT_Done_FreeType(freetype_library);
1125 delete outline_engine;
1128 const char* TitleMain::plugin_title() { return _("Title"); }
1129 int TitleMain::is_realtime() { return 1; }
1130 int TitleMain::is_synthesis() { return 1; }
1132 NEW_WINDOW_MACRO(TitleMain, TitleWindow);
1135 void TitleMain::build_previews(TitleWindow *gui)
1137 ArrayList<BC_FontEntry*>&fonts = *gui->get_resources()->fontlist;
1139 for( int font_number=0; font_number<fonts.size(); ++font_number ) {
1140 BC_FontEntry *font = fonts.get(font_number);
1141 // already have examples
1142 if( font->image ) return;
1145 // create example bitmaps
1146 FT_Library freetype_library = 0; // Freetype library
1147 FT_Face freetype_face = 0;
1148 const char *test_string = "Aa";
1149 char new_path[BCTEXTLEN];
1150 int text_height = gui->get_text_height(LARGEFONT);
1151 int max_height = 3*text_height/2, max_width = 2 * max_height;
1152 int text_color = BC_WindowBase::get_resources()->default_text_color;
1153 int r = (text_color >> 16) & 0xff;
1154 int g = (text_color >> 8) & 0xff;
1155 int b = text_color & 0xff;
1156 // dimensions for each line
1157 int height[fonts.size()];
1158 int ascent[fonts.size()];
1160 // pass 1 gets the extents for all the fonts
1161 // pass 2 draws the image
1164 for( int pass=0; pass<2; ++pass ) {
1165 //printf("TitleMain::build_previews %d %d %d\n",
1166 //__LINE__, text_height, total_h);
1167 for( int font_number=0; font_number<fonts.size(); ++font_number ) {
1168 BC_FontEntry *font = fonts[font_number];
1169 // test if font of same name has been processed
1171 for( int i=0; i<font_number; ++i ) {
1172 if( !strcasecmp(fonts[i]->displayname, font->displayname) ) {
1174 font->image = fonts[i]->image;
1181 if( skip ) continue;
1183 font->image = new VFrame;
1184 font->image->set_use_shm(0);
1185 font->image->reallocate(0, -1, 0, 0, 0,
1186 total_w, total_h, BC_RGBA8888, -1);
1187 font->image->clear_frame();
1190 int current_w = 1, current_h = 1;
1191 int current_x = 0, current_ascent = 0;
1192 int len = strlen(test_string);
1194 for( int j=0; j<len; ++j ) {
1195 FT_ULong c = test_string[j];
1196 // memory leaks here are fatal
1197 // check_char_code_path(freetype_library,
1200 // (char *)new_path);
1201 strcpy(new_path, font->path);
1202 if( load_freetype_face(freetype_library, freetype_face, new_path)) continue;
1203 FT_Set_Pixel_Sizes(freetype_face, text_height, 0);
1204 if( FT_Load_Char(freetype_face, c, FT_LOAD_RENDER) ) continue;
1205 int glyph_w = freetype_face->glyph->bitmap.width;
1206 int glyph_h = freetype_face->glyph->bitmap.rows;
1207 if( glyph_h > max_height ) glyph_h = max_height;
1208 int glyph_a = freetype_face->glyph->advance.x >> 6;
1209 int glyph_t = freetype_face->glyph->bitmap_top;
1211 current_w = current_x + glyph_w;
1212 if( current_w > max_width ) current_w = max_width;
1213 if( total_w < current_w ) total_w = current_w;
1214 if( current_ascent < glyph_t ) current_ascent = glyph_t;
1215 if( current_h < glyph_h ) current_h = glyph_h;
1216 if( total_h < glyph_h ) total_h = glyph_h;
1219 // copy 1 row at a time, center vertically
1220 int out_y = (total_h-height[font_number])/2 + ascent[font_number]-glyph_t;
1221 if( out_y < 0 ) out_y = 0;
1222 for( int in_y = 0; in_y < glyph_h && out_y < total_h; ++in_y, ++out_y ) {
1223 unsigned char *in_row = freetype_face->glyph->bitmap.buffer +
1224 freetype_face->glyph->bitmap.pitch * in_y;
1225 int out_x = current_x;
1226 unsigned char *out_row = font->image->get_rows()[out_y] +
1229 for( int in_x = 0; in_x < glyph_w && out_x < total_w; ++in_x, ++out_x ) {
1230 *out_row = (*in_row * r +
1231 (0xff - *in_row) * *out_row) / 0xff; ++out_row;
1232 *out_row = (*in_row * g +
1233 (0xff - *in_row) * *out_row) / 0xff; ++out_row;
1234 *out_row = (*in_row * b +
1235 (0xff - *in_row) * *out_row) / 0xff; ++out_row;
1236 *out_row = MAX(*in_row, *out_row); ++out_row;
1241 current_x += glyph_a;
1243 height[font_number] = current_h;
1244 ascent[font_number] = current_ascent;
1248 if( freetype_library ) FT_Done_FreeType(freetype_library);
1252 //This checks if char_code is on the selected font, else it changes font to the first compatible //Akirad
1253 int TitleMain::check_char_code_path(FT_Library &freetype_library,
1254 char *path_old, FT_ULong &char_code, char *path_new)
1256 FT_Face temp_freetype_face;
1260 FcChar8 *file, *format;
1265 config = FcConfigGetCurrent();
1266 FcConfigSetRescanInterval(config, 0);
1268 pat = FcPatternCreate();
1269 os = FcObjectSetBuild ( FC_FILE, FC_FONTFORMAT, (char *) 0);
1270 fs = FcFontList(config, pat, os);
1272 char tmpstring[BCTEXTLEN];
1273 int limit_to_truetype = 0; //if you want to limit search to truetype put 1
1274 if( !freetype_library ) FT_Init_FreeType(&freetype_library);
1275 if( !FT_New_Face(freetype_library, path_old, 0, &temp_freetype_face) ) {
1276 FT_Set_Pixel_Sizes(temp_freetype_face, 128, 0);
1277 int gindex = FT_Get_Char_Index(temp_freetype_face, char_code);
1278 if( gindex != 0 && char_code == 10 ) {
1279 strcpy(path_new, path_old);
1285 for( int i=0; fs && i<fs->nfont; ++i ) {
1286 font = fs->fonts[i];
1287 FcPatternGetString(font, FC_FONTFORMAT, 0, &format);
1288 if( strcmp((char *)format, "TrueType") && !limit_to_truetype ) continue;
1289 if( FcPatternGetString(font, FC_FILE, 0, &file) != FcResultMatch ) continue;
1290 sprintf(tmpstring, "%s", file);
1291 if( !FT_New_Face(freetype_library, tmpstring, 0, &temp_freetype_face) ) continue;
1292 FT_Set_Pixel_Sizes(temp_freetype_face, 128, 0);
1293 int gindex = FT_Get_Char_Index(temp_freetype_face, char_code);
1294 if( gindex != 0 && char_code == 10 ) {
1295 sprintf(path_new, "%s", tmpstring);
1303 if( fs ) FcFontSetDestroy(fs);
1304 if( temp_freetype_face ) FT_Done_Face(temp_freetype_face);
1305 temp_freetype_face = 0;
1308 strcpy(path_new, path_old);
1316 int TitleMain::load_freetype_face(FT_Library &freetype_library,
1317 FT_Face &freetype_face, const char *path)
1319 //printf("TitleMain::load_freetype_face 1\n");
1320 if( !freetype_library )
1321 FT_Init_FreeType(&freetype_library);
1323 FT_Done_Face(freetype_face);
1325 //printf("TitleMain::load_freetype_face 2\n");
1327 // Use freetype's internal function for loading font
1328 if( FT_New_Face(freetype_library, path, 0, &freetype_face) ) {
1329 fprintf(stderr, _("TitleMain::load_freetype_face %s failed.\n"), path);
1331 freetype_library = 0;
1337 int TitleMain::load_font(BC_FontEntry *font)
1339 if( load_freetype_face(freetype_library,freetype_face, font->path) ) return 1;
1340 strcpy(text_font, font->displayname);
1345 Indexable *TitleMain::open_background(const char *filename)
1347 delete render_engine; render_engine = 0;
1348 delete video_cache; video_cache = 0;
1349 delete bg_file; bg_file = new File;
1351 Asset *asset = new Asset(filename);
1352 int result = bg_file->open_file(server->preferences, asset, 1, 0);
1353 if( result == FILE_OK ) return (Indexable *)asset;
1355 asset->Garbage::remove_user();
1356 delete bg_file; bg_file = 0;
1357 if( result != FILE_IS_XML ) return 0;
1360 if( xml_file.read_from_file(filename) ) return 0;
1361 EDL *nested_edl = new EDL;
1362 nested_edl->create_objects();
1363 nested_edl->set_path(filename);
1364 nested_edl->load_xml(&xml_file, LOAD_ALL);
1365 TransportCommand command;
1366 //command.command = audio_tracks ? NORMAL_FWD : CURRENT_FRAME;
1367 command.command = CURRENT_FRAME;
1368 command.get_edl()->copy_all(nested_edl);
1369 command.change_type = CHANGE_ALL;
1370 command.realtime = 0;
1371 render_engine = new RenderEngine(0, server->preferences, 0, 0);
1372 render_engine->set_vcache(video_cache = new CICache(server->preferences));
1373 render_engine->arm_command(&command);
1374 return (Indexable *)nested_edl;
1377 int TitleMain::read_background(VFrame *frame, int64_t position, int color_model)
1380 VFrame *iframe = frame;
1381 int bw = background->get_w(), bh = background->get_h();
1382 if( background->is_asset ) {
1383 Asset *asset = (Asset *)background;
1384 if( bw != asset->width || bh != asset->height )
1385 iframe = new_temp(asset->width, asset->height, color_model);
1386 int64_t source_position = (int64_t)(position *
1387 asset->frame_rate / project_frame_rate);
1388 if( config.loop_playback ) {
1389 int64_t loop_size = asset->get_video_frames();
1390 source_position -= (int64_t)(source_position / loop_size) * loop_size;
1393 bg_file->set_video_position(source_position, 0);
1394 result = bg_file->read_frame(iframe);
1398 EDL *nested_edl = (EDL *)background;
1399 if( color_model != nested_edl->session->color_model ||
1400 bw != nested_edl->session->output_w ||
1401 bh != nested_edl->session->output_h )
1403 nested_edl->session->output_w,
1404 nested_edl->session->output_h,
1405 nested_edl->session->color_model);
1406 int64_t source_position = (int64_t)(position *
1407 nested_edl->session->frame_rate / project_frame_rate);
1408 if( config.loop_playback ) {
1409 int64_t loop_size = nested_edl->get_video_frames();
1410 source_position -= (int64_t)(source_position / loop_size) * loop_size;
1412 if( render_engine->vrender )
1413 result = render_engine->vrender->process_buffer(iframe, source_position, 0);
1415 if( !result && iframe != frame )
1416 frame->transfer_from(iframe);
1420 void TitleMain::draw_background()
1423 if( strcmp(background->path, config.background_path) ) {
1425 background->Garbage::remove_user();
1434 if( !background && config.background_path[0] && !access(config.background_path,R_OK) )
1435 background = open_background(config.background_path);
1437 int bw = background->get_w(), bh = background->get_h();
1438 if( bg_frame && (bg_frame->get_w() != bw || bg_frame->get_h() != bh ||
1439 bg_frame->get_color_model() != output_model) ) {
1440 delete bg_frame; bg_frame = 0;
1443 bg_frame = new VFrame(0, -1, bw, bh, output_model, -1);
1444 int64_t position = get_source_position() - get_source_start();
1445 if( !read_background(bg_frame, position, output_model) ) {
1446 if( !overlay_frame )
1447 overlay_frame = new OverlayFrame(cpus);
1448 float in_x1 = 0, in_x2 = bg_frame->get_w();
1449 float in_y1 = 0, in_y2 = bg_frame->get_h();
1450 float out_x1 = config.title_x, out_x2 = out_x1 + title_w;
1451 float out_y1 = config.title_y, out_y2 = out_y1 + title_h;
1452 overlay_frame->overlay(output, bg_frame,
1453 in_x1,in_y1, in_x2,in_y2,
1454 out_x1,out_y1, out_x2,out_y2,
1455 1, TRANSFER_NORMAL, CUBIC_LINEAR);
1460 BC_FontEntry* TitleMain::get_font(const char *font_name, int style)
1462 if( !strcmp("fixed", font_name) )
1463 font_name = FIXED_FONT;
1465 ((style & BC_FONT_ITALIC) != 0 ? FL_SLANT_ITALIC | FL_SLANT_OBLIQUE : FL_SLANT_ROMAN) |
1466 ((style & BC_FONT_BOLD) != 0 ? FL_WEIGHT_BOLD | FL_WEIGHT_DEMIBOLD |
1467 FL_WEIGHT_EXTRABOLD| FL_WEIGHT_BLACK | FL_WEIGHT_EXTRABLACK :
1468 FL_WEIGHT_BOOK | FL_WEIGHT_NORMAL | FL_WEIGHT_MEDIUM |
1469 FL_WEIGHT_LIGHT | FL_WEIGHT_EXTRALIGHT | FL_WEIGHT_THIN);
1471 int mask = FL_WEIGHT_MASK | FL_SLANT_MASK;
1473 BC_Resources *resources = BC_WindowBase::get_resources();
1474 return resources->find_fontentry(font_name, flavor, mask, style);
1476 BC_FontEntry* TitleMain::config_font()
1478 BC_FontEntry *font = get_font(config.font, config.style);
1479 if( font && load_font(font) )
1480 font = get_font(FIXED_FONT,0);
1485 static inline bool is_ltr(wchar_t wch) { return (wch>='a' && wch<='z') || (wch>='A' && wch<='Z'); }
1486 static inline bool is_nbr(wchar_t wch) { return (wch>='0' && wch<='9'); }
1487 static inline bool is_ws(wchar_t wch) { return wch==' ' || wch=='\t'; }
1488 static inline bool is_idch(wchar_t wch) { return is_ltr(wch) || is_nbr(wch) || wch=='_'; }
1490 // return eof=-1, chr=0, opener=1, closer=2
1491 int TitleParser::wget(wchar_t &wch)
1493 char *ip = id, *tp = text; *ip = 0; *tp = 0;
1494 int ilen = sizeof(id), tlen = sizeof(text), ich;
1495 while( (ich=wnext()) >= 0 ) {
1497 if( (ich=wnext()) == '\n' ) continue;
1501 if( ich == '<' ) break;
1502 if( ich != '#' ) { wch = ich; return 0; }
1503 while( (ich=wnext()) >= 0 && ich != '\n' );
1504 if( ich < 0 ) break;
1506 if( ich < 0 ) return -1;
1507 int ret = 1; long pos = tell();
1508 if( (ich=wnext()) == '/' ) { ret = 2; ich = wnext(); }
1511 while( is_idch(ich=wnext()) )
1512 if( --ilen > 0 ) *ip++ = ich;
1515 while( is_ws(ich) ) ich = wnext();
1516 while( ich >= 0 && ich != '>' ) {
1517 if( ich == '\n' || ich == '<' ) { ich = -1; break; }
1518 if( ich == '\\' && (ich=wnext()) < 0 ) break;
1519 if( --tlen > 0 ) *tp++ = ich;
1523 if( ich < 0 ) { ich = '<'; seek(pos); ret = 0; }
1528 TitleGlyph *TitleMain::get_glyph(FT_ULong char_code, BC_FontEntry *font, int size, int style)
1530 for( int i=0, n=title_glyphs.count(); i<n; ++i ) {
1531 TitleGlyph *glyph = title_glyphs[i];
1532 if( glyph->char_code == char_code && glyph->font == font &&
1533 glyph->size == size && glyph->style == style )
1539 int TitleMain::get_width(TitleGlyph *cur, TitleGlyph *nxt)
1541 if( !cur || cur->char_code == '\n' ) return 0;
1542 int result = cur->advance_x;
1543 if( !nxt ) return result;
1545 if( !FT_Get_Kerning(freetype_face,
1546 cur->freetype_index, nxt->freetype_index,
1547 ft_kerning_default, &kerning) )
1548 result += (kerning.x >> 6);
1553 VFrame *TitleMain::get_image(const char *path)
1555 for( int i=0; i<title_images.count(); ++i ) {
1556 if( !strcmp(path, title_images[i]->path) )
1557 return title_images[i]->vframe;
1562 VFrame *TitleMain::add_image(const char *path)
1564 VFrame *vframe = get_image(path);
1565 if( !vframe && (vframe=VFramePng::vframe_png(path)) != 0 ) {
1566 if( vframe->get_color_model() != text_model ) {
1567 VFrame *frame = new VFrame(vframe->get_w(), vframe->get_h(), text_model);
1568 frame->transfer_from(vframe); delete vframe;
1571 title_images.append(new TitleImage(path, vframe));
1576 int TitleCurColor::set(const char *txt)
1578 #define BCOLOR(NM) { #NM, NM }
1579 static const struct { const char *name; int color; } colors[] = {
1580 BCOLOR(MNBLUE), BCOLOR(ORANGE), BCOLOR(BLOND),
1581 BCOLOR(MNGREY), BCOLOR(FGGREY), BCOLOR(FTGREY), BCOLOR(DKGREY),
1582 BCOLOR(LTGREY), BCOLOR(MEGREY), BCOLOR(DMGREY), BCOLOR(MDGREY),
1583 BCOLOR(LTPURPLE),BCOLOR(MEPURPLE),BCOLOR(MDPURPLE), BCOLOR(DKPURPLE),
1584 BCOLOR(LTCYAN), BCOLOR(MECYAN), BCOLOR(MDCYAN), BCOLOR(DKCYAN),
1585 BCOLOR(YELLOW), BCOLOR(LTYELLOW),BCOLOR(MEYELLOW), BCOLOR(MDYELLOW),
1586 BCOLOR(LTGREEN), BCOLOR(DKGREEN), BCOLOR(DKYELLOW), BCOLOR(LTPINK),
1587 BCOLOR(PINK), BCOLOR(LTBLUE), BCOLOR(DKBLUE),
1588 BCOLOR(RED), BCOLOR(GREEN), BCOLOR(BLUE),
1589 BCOLOR(BLACK), BCOLOR(WHITE),
1593 if( txt[0] == '#' ) {
1594 if( sscanf(&txt[1], "%x", &color) != 1 ) return 1;
1597 int i = sizeof(colors)/sizeof(colors[0]);
1598 while( --i >= 0 && strcasecmp(txt, colors[i].name) );
1599 if( i < 0 ) return 1;
1600 color = colors[i].color;
1604 color = parser->plugin->config.color;
1609 int TitleCurAlpha::set(const char *txt)
1612 parser->plugin->config.alpha :
1613 strtof(txt,(char**)&txt) * 255;
1615 if( *txt || alpha < 0 || alpha > 255 ) return 1;
1620 int TitleCurSize::set(const char *txt)
1624 size = parser->plugin->config.size;
1626 else if( *txt == '+' || *txt == '-' ) {
1628 for( int ch; (ch=*txt)!=0; ++txt ) {
1629 if( ch == '+' ) { size *= 5./4.; continue; }
1630 if( ch == '-' ) { size *= 4./5.; continue; }
1635 size = strtof(txt,(char**)&txt);
1637 if( *txt || size <= 0 || size > 2048 ) return 1;
1638 int style = parser->cur_font.style();
1639 if( !parser->cur_font.set(0,style) ) return 1;
1643 int TitleCurSize::unset(const char *txt)
1645 if( pop() ) return 1;
1646 int style = parser->cur_font.style();
1647 parser->cur_font.set(0,style);
1652 int TitleCurBold::set(const char *txt)
1654 int bold = !*txt ? 1 :
1655 strtol(txt,(char**)&txt,0);
1656 if( *txt || bold < 0 || bold > 1 ) return 1;
1657 int style = parser->cur_font.style();
1658 if( bold ) style |= BC_FONT_BOLD;
1659 else style &= ~BC_FONT_BOLD;
1660 if( !parser->cur_font.set(0,style) ) return 1;
1664 int TitleCurBold::unset(const char *txt)
1666 if( pop() ) return 1;
1667 int style = parser->cur_font.style();
1668 parser->cur_font.set(0,style);
1672 int TitleCurItalic::set(const char *txt)
1674 int italic = !*txt ? 1 :
1675 strtol(txt,(char**)&txt,0);
1676 if( *txt || italic < 0 || italic > 1 ) return 1;
1677 int style = parser->cur_font.style();
1678 if( italic ) style |= BC_FONT_ITALIC;
1679 else style &= ~BC_FONT_ITALIC;
1680 if( !parser->cur_font.set(0,style) ) return 1;
1684 int TitleCurItalic::unset(const char *txt)
1686 if( pop() ) return 1;
1687 int style = parser->cur_font.style();
1688 parser->cur_font.set(0,style);
1693 int TitleCurFont::style()
1696 if( parser->cur_bold ) style |= BC_FONT_BOLD;
1697 if( parser->cur_italic ) style |= BC_FONT_ITALIC;
1700 BC_FontEntry* TitleCurFont::get(const char *txt, int style)
1702 if( !txt ) txt = parser->plugin->text_font;
1703 else if( !*txt ) txt = parser->plugin->config.font;
1704 return parser->plugin->get_font(txt, style);
1706 BC_FontEntry *TitleCurFont::set(const char *txt, int style)
1708 BC_FontEntry *font = get(txt, style);
1709 if( !font || parser->plugin->load_font(font) ) return 0;
1710 if( !txt ) (BC_FontEntry*&)*this = font;
1713 int TitleCurFont::set(const char *txt)
1715 BC_FontEntry *font = set(txt, style());
1716 if( !font ) return 1;
1720 int TitleCurFont::unset(const char *txt)
1722 if( *txt || pop() ) return 1;
1723 BC_FontEntry *font = *this;
1724 if( !font ) return 1;
1725 font = get(font->displayname, style());
1726 if( !font ) return 1;
1727 (BC_FontEntry*&)*this = font;
1731 int TitleCurCaps::set(const char *txt)
1733 int caps = !*txt ? 1 : strtol(txt,(char **)&txt,0);
1734 if( *txt || caps < -1 || caps > 1 ) return 1;
1739 int TitleCurBlink::set(const char *txt)
1741 float blink = !*txt ? 1 : strtof(txt,(char **)&txt);
1742 if( *txt ) return 1;
1747 int TitleCurFixed::set(const char *txt)
1750 parser->cur_size*3/4 :
1751 strtol(txt,(char **)&txt,0);
1752 if( *txt || fixed < 0 ) return 1;
1757 int TitleCurSuper::set(const char *txt)
1759 int super = !*txt ? 1 : strtol(txt,(char **)&txt,0);
1760 if( *txt || super < -1 || super > 1 ) return 1;
1765 int TitleCurNudge::set(const char *txt)
1767 if( !*txt ) return 1;
1768 short nx = strtol(txt,(char **)&txt,0);
1769 if( *txt++ != ',' ) return 1;
1770 short ny = strtol(txt,(char **)&txt,0);
1771 if( *txt ) return 1;
1772 int nudge = (nx << 16) | (ny & 0xffff);
1777 int TitleParser::set_attributes(int ret)
1779 if( !strcmp(id,_("nudge")) ) return ret>1 ? cur_nudge.unset(text) : cur_nudge.set(text);
1780 if( !strcmp(id,_("color")) ) return ret>1 ? cur_color.unset(text) : cur_color.set(text);
1781 if( !strcmp(id,_("alpha")) ) return ret>1 ? cur_alpha.unset(text) : cur_alpha.set(text);
1782 if( !strcmp(id,_("font")) ) return ret>1 ? cur_font.unset(text) : cur_font.set(text);
1783 if( !strcmp(id,_("size")) ) return ret>1 ? cur_size.unset(text) : cur_size.set(text);
1784 if( !strcmp(id,_("bold")) ) return ret>1 ? cur_bold.unset(text) : cur_bold.set(text);
1785 if( !strcmp(id,_("italic")) ) return ret>1 ? cur_italic.unset(text) : cur_italic.set(text);
1786 if( !strcmp(id,_("caps")) ) return ret>1 ? cur_caps.unset(text) : cur_caps.set(text);
1787 if( !strcmp(id,_("ul")) ) return ret>1 ? cur_under.unset(text) : cur_under.set(text);
1788 if( !strcmp(id,_("blink")) ) return ret>1 ? cur_blink.unset(text) : cur_blink.set(text);
1789 if( !strcmp(id,_("fixed")) ) return ret>1 ? cur_fixed.unset(text) : cur_fixed.set(text);
1790 if( !strcmp(id,_("sup")) ) return ret>1 ? cur_super.unset(text) : cur_super.set(text);
1795 void TitleMain::load_glyphs()
1797 // Build table of all glyphs needed
1798 TitleParser wchrs(this);
1799 int total_packages = 0;
1801 while( !wchrs.eof() ) {
1802 wchar_t wch1 = wchrs.wcur(), wch;
1803 long ipos = wchrs.tell();
1804 int ret = wchrs.wget(wch);
1806 if( !wchrs.set_attributes(ret) ) continue;
1807 if( !strcmp(wchrs.id,"png") && add_image(wchrs.text) ) continue;
1808 wch = wch1; wchrs.seek(ipos+1);
1812 int cur_caps = wchrs.cur_caps;
1813 if( cur_caps > 0 ) wch = towupper(wch);
1814 else if( cur_caps < 0 ) wch = towlower(wch);
1815 BC_FontEntry *cur_font = wchrs.cur_font;
1816 int cur_size = wchrs.cur_size;
1818 int cur_bold = wchrs.cur_bold;
1819 if( cur_bold ) cur_style |= BC_FONT_BOLD;
1820 int cur_italic = wchrs.cur_italic;
1821 if( cur_italic ) cur_style |= BC_FONT_ITALIC;
1822 int cur_super = wchrs.cur_super;
1823 if( cur_super ) cur_size /= 2;
1825 for( int j=0; j<title_glyphs.count(); ++j ) {
1826 TitleGlyph *glyph = title_glyphs[j];
1827 if( glyph->char_code == (FT_ULong)wch && glyph->font == cur_font &&
1828 glyph->size == cur_size && glyph->style == cur_style ) {
1833 if( !exists && cur_font ) {
1835 TitleGlyph *glyph = new TitleGlyph;
1836 glyph->char_code = (FT_ULong)wch;
1837 glyph->font = cur_font;
1838 glyph->size = cur_size;
1839 glyph->style = cur_style;
1840 title_glyphs.append(glyph);
1846 glyph_engine = new GlyphEngine(this, cpus);
1848 glyph_engine->set_package_count(total_packages);
1849 glyph_engine->process_packages();
1853 TitleImage::TitleImage(const char *path, VFrame *vfrm)
1855 this->path = cstrdup(path);
1856 this->vframe = vfrm;
1858 TitleImage::~TitleImage()
1864 TitleChar *TitleChar::init(int typ, void *vp)
1880 TitleRow *TitleRow::init()
1883 x1 = y2 = 0; //MAX_FLT;
1884 y1 = x2 = 0; //MIN_FLT;
1888 int TitleMain::get_text()
1890 // Determine extents of total text
1891 title_chars.reset();
1893 int pitch = config.line_pitch;
1894 float font_h = config.size;
1897 TitleParser wchrs(this);
1900 float row_w = 0, row_h = 0;
1901 float row_x = 0, row_y = 0;
1905 if( !row ) row = title_rows.add();
1907 long ipos = wchrs.tell();
1908 wchar_t wch1 = wchrs.wcur(), wch;
1909 int ret = wchrs.wget(wch);
1910 if( ret < 0 || wch == '\n' ) {
1911 if( row->x1 > row->x2 ) row->x1 = row->x2 = 0;
1912 if( row->y2 > row->y1 ) row->y1 = row->y2 = 0;
1913 int dy = row->y1 - descent;
1914 row_y += pitch ? pitch : dy > font_h ? dy : font_h;
1917 if( row_x > row_w ) row_w = row_x;
1918 if( row_y > row_h ) row_h = row_y;
1919 text_rows = title_rows.count();
1921 if( ret < 0 ) break;
1924 BC_FontEntry *cur_font = wchrs.cur_font;
1925 int cur_color = wchrs.cur_color;
1926 int cur_alpha = wchrs.cur_alpha;
1927 float cur_size = wchrs.cur_size;
1928 int cur_caps = wchrs.cur_caps;
1929 int cur_under = wchrs.cur_under;
1930 float cur_blink = wchrs.cur_blink;
1931 int cur_fixed = wchrs.cur_fixed;
1932 int cur_super = wchrs.cur_super;
1933 int cur_nudge = wchrs.cur_nudge;
1935 int cur_bold = wchrs.cur_bold;
1936 if( cur_bold ) cur_style |= BC_FONT_BOLD;
1937 int cur_italic = wchrs.cur_italic;
1938 if( cur_italic ) cur_style |= BC_FONT_ITALIC;
1939 short nx = cur_nudge >> 16, ny = cur_nudge;
1940 int cx = nx, cy = ny, cw = 0, ch = 0, dx = 0;
1942 if( !wchrs.set_attributes(ret) ) continue;
1944 if( !strcmp(wchrs.id,"png") ) {
1945 VFrame *png_image = get_image(wchrs.text);
1947 chr = title_chars.add(CHAR_IMAGE, png_image);
1948 cy += ch = png_image->get_h();
1949 cw = dx = png_image->get_w();
1954 wch = wch1; wchrs.seek(ipos+1);
1958 if( cur_caps > 0 ) wch = towupper(wch);
1959 else if( cur_caps < 0 ) wch = towlower(wch);
1960 int size = !cur_super ? cur_size : cur_size/2;
1961 TitleGlyph *gp = get_glyph(wch, cur_font, size, cur_style);
1964 if( cur_super > 0 ) cy += cur_size-4*size/3;
1965 else if( cur_super < 0 ) cy -= size/3;
1971 else if( !wchrs.eof() ) {
1972 TitleGlyph *np = get_glyph(wchrs.wcur(), cur_font, cur_size, cur_style);
1973 dx = get_width(gp, np);
1976 chr = title_chars.add(CHAR_GLYPH, gp);
1979 cw = gp->right - gp->left;
1980 ch = gp->top - gp->bottom;
1983 if( !chr ) continue;
1985 chr->size = cur_size;
1986 chr->blink = cur_blink;
1987 chr->color = cur_color;
1988 chr->alpha = cur_alpha;
1990 if( cur_fixed ) chr->flags |= FLAG_FIXED;
1991 if( cur_super > 0 ) chr->flags |= FLAG_SUPER;
1992 if( cur_super < 0 ) chr->flags |= FLAG_SUBER;
1993 if( cur_under ) chr->flags |= FLAG_UNDER;
1994 if( cur_blink ) chr->flags |= FLAG_BLINK;
1995 chr->x = (cx += row_x);
1998 row->bound(cx,cy, cx+cw,cy-ch);
1999 chr->row = text_rows;
2003 if( !row_w || !row_h ) return 1;
2005 // rows boundary, note: row->xy is y up (FT), row_xy is y down (X)
2006 text_x1 = text_y1 = MAX_FLT;
2007 text_x2 = text_y2 = MIN_FLT;
2008 for( int i=0; i<text_rows; ++i ) {
2009 row = title_rows[i];
2010 switch( config.hjustification ) {
2011 case JUSTIFY_LEFT: row->x0 = 0; break;
2012 case JUSTIFY_CENTER: row->x0 = (row_w - (row->x2-row->x1)) / 2; break;
2013 case JUSTIFY_RIGHT: row->x0 = row_w - (row->x2-row->x1); break;
2016 if( text_x1 > (v=row->x0+row->x1) ) text_x1 = v;
2017 if( text_y1 > (v=row->y0-row->y1) ) text_y1 = v;
2018 if( text_x2 < (v=row->x0+row->x2) ) text_x2 = v;
2019 if( text_y2 < (v=row->y0-row->y2) ) text_y2 = v;
2021 if( text_x1 > text_x2 || text_y1 > text_y2 ) return 1;
2022 text_x1 += fuzz1; text_y1 += fuzz1;
2023 text_x2 += fuzz2; text_y2 += fuzz2;
2024 text_w = text_x2 - text_x1;
2025 text_h = text_y2 - text_y1;
2029 int TitleMain::get_visible_text()
2031 // Determine y of visible text
2032 switch( config.motion_strategy ) {
2034 case TOP_TO_BOTTOM: {
2035 float magnitude = config.pixels_per_second *
2036 (get_source_position() - config.prev_keyframe_position) /
2037 PluginVClient::project_frame_rate;
2039 int loop_size = text_h + title_h;
2040 magnitude -= (int)(magnitude / loop_size) * loop_size;
2042 text_y = config.motion_strategy == BOTTOM_TO_TOP ?
2043 title_h - magnitude :
2046 default: switch( config.vjustification ) {
2047 case JUSTIFY_TOP: text_y = 0; break;
2048 case JUSTIFY_MID: text_y = (title_h - text_h) / 2; break;
2049 case JUSTIFY_BOTTOM: text_y = title_h - text_h; break;
2053 // Determine x of visible text
2054 switch( config.motion_strategy ) {
2056 case LEFT_TO_RIGHT: {
2057 float magnitude = config.pixels_per_second *
2058 (get_source_position() - config.prev_keyframe_position) /
2059 PluginVClient::project_frame_rate;
2061 int loop_size = text_w + title_w;
2062 magnitude -= (int)(magnitude / loop_size) * loop_size;
2064 text_x = config.motion_strategy == RIGHT_TO_LEFT ?
2065 title_w - magnitude :
2068 default: switch ( config.hjustification ) {
2069 case JUSTIFY_LEFT: text_x = 0; break;
2070 case JUSTIFY_CENTER: text_x = (title_w - text_w) / 2; break;
2071 case JUSTIFY_RIGHT: text_x = title_w - text_w; break;
2076 // until bottom of this row is visible
2078 int y0 = text_y - text_y1;
2079 while( row1 < text_rows ) {
2080 TitleRow *row = title_rows[row1];
2081 if( y0 + row->y0-row->y2 > 0 ) break;
2084 if( row1 >= text_rows ) return 0;
2086 // until top of next row is not visible
2089 while( row2 < text_rows ) {
2090 TitleRow *row = title_rows[row2];
2091 if( y0 + row->y0-row->y1 >= 0 ) break;
2094 if( row1 >= row2 ) return 0;
2096 //printf("get_visible_rows: visible_row1/2 = %d to %d\n",visible_row1,visible_row2);
2097 // Only draw visible chars
2098 double frame_rate = PluginVClient::get_project_framerate();
2099 int64_t units = get_source_position() - get_source_start();
2100 double position = units / frame_rate;
2102 int char1 = -1, char2 = -1, nchars = title_chars.count();
2103 for( int i=0; i<nchars; ++i ) {
2104 TitleChar *chr = title_chars[i];
2105 if( chr->row < row1 ) continue;
2106 if( chr->row >= row2 ) break;
2107 if( char1 < 0 ) char1 = i;
2109 if( (chr->flags & FLAG_BLINK) != 0 ) {
2111 double rate = 1 / fabs(chr->blink), rate2 = 2 * rate;
2112 double pos = position - ((int64_t)(position / rate2)) * rate2;
2113 chr->fade = chr->blink > 0 ?
2114 (pos > rate ? 0 : 1) : fabs(rate+pos) / rate;
2117 if( char1 < 0 ) char1 = 0;
2119 if( !blinking && visible_row1 == row1 && visible_row2 == row2 ) return 0;
2121 visible_row1 = row1; visible_row2 = row2;
2122 visible_char1 = char1; visible_char2 = char2;
2127 int TitleMain::draw_text(int need_redraw)
2129 // until top of next row is not visible
2130 mask_x1 = mask_y1 = INT_MAX;
2131 mask_x2 = mask_y2 = INT_MIN;
2132 for( int i=visible_row1; i<visible_row2; ++i ) {
2133 int v; TitleRow *row = title_rows[i];
2134 if( mask_x1 > (v=row->x0+row->x1) ) mask_x1 = v;
2135 if( mask_y1 > (v=row->y0-row->y1) ) mask_y1 = v;
2136 if( mask_x2 < (v=row->x0+row->x2) ) mask_x2 = v;
2137 if( mask_y2 < (v=row->y0-row->y2) ) mask_y2 = v;
2139 if( mask_x1 >= mask_x2 || mask_y1 >= mask_y2 ) return 1;
2141 mask_x1 += fuzz1; mask_y1 += fuzz1;
2142 mask_x2 += fuzz2; mask_y2 += fuzz2;
2143 mask_w = mask_x2 - mask_x1;
2144 mask_h = mask_y2 - mask_y1;
2146 //printf("TitleMain::draw_text %d-%d frame %dx%d\n",
2147 // visible_row1, visible_row2, mask_w,mask_h);
2148 if( text_mask && (text_mask->get_color_model() != text_model ||
2149 text_mask->get_w() != mask_w || text_mask->get_h() != mask_h) ) {
2150 delete text_mask; text_mask = 0;
2151 delete stroke_mask; stroke_mask = 0;
2155 // Always use 8 bit because the glyphs are 8 bit
2156 // Need to set YUV to get clear_frame to set the right chroma.
2157 mask_model = text_model;
2158 text_mask = new VFrame;
2159 text_mask->set_use_shm(0);
2160 text_mask->reallocate(0, -1, 0, 0, 0, mask_w, mask_h, mask_model, -1);
2161 int drop = abs(config.dropshadow);
2162 int drop_w = mask_w + drop;
2163 int drop_h = mask_h + drop;
2164 stroke_mask = new VFrame;
2165 stroke_mask->set_use_shm(0);
2166 stroke_mask->reallocate(0, -1, 0, 0, 0, drop_w, drop_h, mask_model, -1);
2170 // Draw on text mask if it has changed
2172 //printf("redraw %d to %d %d,%d %d,%d - %d,%d\n", visible_char1,visible_char2,
2173 // ext_x0, ext_y0, ext_x1,ext_y1, ext_x2,ext_y2);
2175 text_mask->clear_frame();
2176 stroke_mask->clear_frame();
2178 unsigned char *data = text_mask->get_data(); // draw bbox on text
2179 for( int x=0; x<mask_w; ++x ) data[4*x+3] = 0xff;
2180 for( int x=0; x<mask_w; ++x ) data[4*((mask_h-1)*mask_w+x)+3] = 0xff;
2181 for( int y=0; y<mask_h; ++y ) data[4*mask_w*y+3] = 0xff;
2182 for( int y=0; y<mask_h; ++y ) data[4*(mask_w*y + mask_w-1)+3] = 0xff;
2185 title_engine = new TitleEngine(this, cpus);
2187 // Draw dropshadow first
2188 if( config.dropshadow ) {
2189 title_engine->do_dropshadow = 1;
2190 title_engine->set_package_count(visible_char2 - visible_char1);
2191 title_engine->process_packages();
2194 // Then draw foreground
2195 title_engine->do_dropshadow = 0;
2196 title_engine->set_package_count(visible_char2 - visible_char1);
2197 title_engine->process_packages();
2199 draw_underline(text_mask, config.alpha);
2201 // Convert to text outlines
2202 if( config.outline_size > 0 ) {
2204 (text_mask->get_w() != outline_mask->get_w() ||
2205 text_mask->get_h() != outline_mask->get_h()) ) {
2206 delete outline_mask; outline_mask = 0;
2209 if( !outline_mask ) {
2210 outline_mask = new VFrame;
2211 outline_mask->set_use_shm(0);
2212 outline_mask->reallocate(0, -1, 0, 0, 0,
2213 text_mask->get_w(), text_mask->get_h(),
2214 text_mask->get_color_model(), -1);
2216 if( !outline_engine ) outline_engine =
2217 new TitleOutlineEngine(this, cpus);
2218 outline_engine->do_outline();
2226 int TitleMain::draw_underline(VFrame *mask, int alpha)
2228 int row = -1, sz = -1, color = -1, rgb = -1;
2229 int bpp = mask->get_bytes_per_pixel(), bpp1 = bpp-1;
2230 int x1 = 0, x2 = 0, y0 = 0;
2231 for( int i=visible_char1; i<visible_char2; ) {
2232 TitleChar *chr = title_chars[i];
2233 if( (chr->flags & FLAG_UNDER) && row < 0 ) {
2235 rgb = chr->color; int rr, gg, bb;
2236 get_mask_colors(rgb, mask_model, rr, gg, bb);
2237 color = (rr<<16) | (gg<<8) | (bb<<0);
2239 TitleRow *rp = title_rows[row];
2240 x1 = rp->x0 + chr->x;
2243 if( (chr->flags & FLAG_UNDER) && row == chr->row && chr->color == rgb ) {
2244 TitleRow *rp = title_rows[row];
2245 x2 = rp->x0 + chr->x + chr->dx;
2246 if( sz < chr->size ) sz = chr->size;
2247 if( ++i < visible_char2 ) continue;
2249 if( row < 0 ) { ++i; continue; }
2250 x1 -= mask_x1; x2 -= mask_x1; y0 -= mask_y1;
2251 if( x1 < 0 ) x1 = 0;
2252 if( x2 > mask_w ) x2 = mask_w;
2253 int sz1 = sz / 32 + 1, sz2 = sz1/2;
2254 int y1 = y0 - sz2, y2 = y1 + sz1;
2255 if( y1 < 0 ) y1 = 0;
2256 if( y2 > mask_h ) y2 = mask_h;
2257 unsigned char **rows = mask->get_rows();
2258 for( int y=y1; y<y2; ++y ) {
2259 unsigned char *rp = rows[y];
2260 for( int x=x1; x<x2; ++x ) {
2261 unsigned char *bp = rp + bpp * x;
2262 for( int i=bpp1; --i>=0; ) *bp++ = color>>(8*i);
2271 void TitleMain::draw_overlay()
2274 //printf("TitleMain::draw_overlay 1\n");
2276 if( !EQUIV(config.fade_in, 0) ) {
2277 int fade_len = lroundf(config.fade_in * PluginVClient::project_frame_rate);
2278 int fade_position = get_source_position() - config.prev_keyframe_position;
2280 if( fade_position >= 0 && fade_position < fade_len ) {
2281 fade = (float)fade_position / fade_len;
2284 if( !EQUIV(config.fade_out, 0) ) {
2285 int fade_len = lroundf(config.fade_out * PluginVClient::project_frame_rate);
2286 int fade_position = config.next_keyframe_position - get_source_position();
2289 if( fade_position >= 0 && fade_position < fade_len ) {
2290 fade = (float)fade_position / fade_len;
2295 translate = new TitleTranslate(this, cpus);
2296 if( text_x+mask_w > 0 && text_x < title_w ) {
2297 translate->xlat_mask = text_mask;
2298 translate->run_packages();
2299 if( config.stroke_width >= SMALL && (config.style & BC_FONT_OUTLINE) ) {
2300 translate->xlat_mask = stroke_mask;
2301 translate->run_packages();
2306 const char* TitleMain::motion_to_text(int motion)
2309 case NO_MOTION: return _("No motion");
2310 case BOTTOM_TO_TOP: return _("Bottom to top");
2311 case TOP_TO_BOTTOM: return _("Top to bottom");
2312 case RIGHT_TO_LEFT: return _("Right to left");
2313 case LEFT_TO_RIGHT: return _("Left to right");
2315 return _("Unknown");
2318 int TitleMain::text_to_motion(const char *text)
2320 for( int i=0; i<TOTAL_PATHS; ++i ) {
2321 if( !strcasecmp(motion_to_text(i), text) ) return i;
2326 void TitleMain::reset_render()
2328 delete text_mask; text_mask = 0;
2329 delete stroke_mask; stroke_mask = 0;
2330 delete glyph_engine; glyph_engine = 0;
2331 visible_row1 = 0; visible_row2 = 0;
2332 visible_char1 = 0; visible_char2 = 0;
2334 title_glyphs.clear();
2335 title_images.clear();
2336 if( freetype_face ) {
2337 FT_Done_Face(freetype_face);
2342 int TitleMain::init_freetype()
2344 if( !freetype_library )
2345 FT_Init_FreeType(&freetype_library);
2349 void TitleMain::draw_boundry()
2351 VFrame &out = *output;
2352 int iw = output->get_w(), ih = output->get_h();
2353 int mr = MIN(iw, ih)/200 + 2, rr = 2*mr;
2354 int x = title_x, y = title_y, w = title_w, h = title_h;
2356 int x0 = x-r2, x1 = x+(w+1)/2, x2 = x+w+r2;
2357 int y0 = y-r2, y1 = y+(h+1)/2, y2 = y+h+r2;
2359 for( int r=2; r<mr; r<<=1 ) st = r;
2362 for( int r=mr/2; --r>=0; ) { // bbox
2363 int lft = x+r, rgt = x+w-1-r;
2364 int top = y+r, bot = y+h-1-r;
2365 out.draw_line(lft,top, rgt,top);
2366 out.draw_line(rgt,top, rgt,bot);
2367 out.draw_line(rgt,bot, lft,bot);
2368 out.draw_line(lft,bot, lft,top);
2371 for( int r=mr; r<rr; ++r ) { // center
2372 out.draw_smooth(x1-r,y1, x1-r,y1+r, x1,y1+r);
2373 out.draw_smooth(x1,y1+r, x1+r,y1+r, x1+r,y1);
2374 out.draw_smooth(x1+r,y1, x1+r,y1-r, x1,y1-r);
2375 out.draw_smooth(x1,y1-r, x1-r,y1-r, x1-r,y1);
2378 for( int r=rr; --r>=0; ) { // edge arrows
2379 out.draw_line(x1-r,y0+r, x1+r,y0+r);
2380 out.draw_line(x2-r,y1-r, x2-r,y1+r);
2381 out.draw_line(x1-r,y2-r, x1+r,y2-r);
2382 out.draw_line(x0+r,y1+r, x0+r,y1-r);
2384 x0 += r2; y0 += r2; x2 -= r2; y2 -= r2;
2385 for( int r=2*mr; --r>=0; ) { // corner arrows
2386 out.draw_line(x0,y0+r, x0+r,y0);
2387 out.draw_line(x2,y0+r, x2-r,y0);
2388 out.draw_line(x2,y2-r, x2-r,y2);
2389 out.draw_line(x0,y2-r, x0+r,y2);
2394 int TitleMain::process_realtime(VFrame *input_ptr, VFrame *output_ptr)
2398 output = output_ptr;
2399 output_model = output->get_color_model();
2400 text_model = BC_CModels::is_yuv(output_model) ? BC_YUVA8888 : BC_RGBA8888;
2402 if( text_model != mask_model ) need_reconfigure = 1;
2403 need_reconfigure |= load_configuration();
2404 int64_t cur_position = get_source_position();
2405 if( last_position < 0 || last_position+1 != cur_position )
2406 need_reconfigure = 1;
2407 last_position = cur_position;
2409 title_x = config.title_x; title_y = config.title_y;
2410 title_w = config.title_w ? config.title_w : input->get_w();
2411 title_h = config.title_h ? config.title_h : input->get_h();
2413 fuzz1 = -config.outline_size;
2414 fuzz2 = config.outline_size;
2415 if( config.dropshadow < 0 )
2416 fuzz1 += config.dropshadow;
2418 fuzz2 += config.dropshadow;
2419 fuzz = fuzz2 - fuzz1;
2422 if( config.size <= 0 || config.size >= 2048 )
2424 if( config.stroke_width < 0 || config.stroke_width >= 512 )
2425 config.stroke_width = 0.0;
2426 if( !config.wlen && !config.timecode )
2428 if( !strlen(config.encoding) )
2429 strcpy(config.encoding, DEFAULT_ENCODING);
2431 // Always synthesize text and redraw it for timecode
2432 if( config.timecode ) {
2433 int64_t rendered_frame = get_source_position();
2434 if( get_direction() == PLAY_REVERSE )
2435 rendered_frame -= 1;
2437 char text[BCTEXTLEN];
2439 (double)rendered_frame / PluginVClient::project_frame_rate,
2440 config.timecode_format,
2441 PluginVClient::get_project_samplerate(),
2442 PluginVClient::get_project_framerate(),
2444 config.to_wtext(config.encoding, text, strlen(text)+1);
2445 need_reconfigure = 1;
2448 if( config.background )
2451 // Handle reconfiguration
2452 if( need_reconfigure ) {
2453 need_reconfigure = 0;
2455 result = init_freetype();
2458 result = get_text();
2463 result = draw_text(get_visible_text());
2466 // Overlay mask on output
2476 void TitleMain::update_gui()
2479 int reconfigure = load_configuration();
2481 TitleWindow *window = (TitleWindow*)thread->window;
2482 window->lock_window("TitleMain::update_gui");
2484 window->unlock_window();
2485 window->color_thread->update_gui(config.color, 0);
2490 int TitleMain::load_configuration()
2492 KeyFrame *prev_keyframe, *next_keyframe;
2493 prev_keyframe = get_prev_keyframe(get_source_position());
2494 next_keyframe = get_next_keyframe(get_source_position());
2496 TitleConfig old_config, prev_config, next_config;
2497 old_config.copy_from(config);
2498 read_data(prev_keyframe);
2499 prev_config.copy_from(config);
2500 read_data(next_keyframe);
2501 next_config.copy_from(config);
2503 config.prev_keyframe_position = prev_keyframe->position;
2504 config.next_keyframe_position = next_keyframe->position;
2506 // if no previous keyframe exists, it should be start of the plugin, not start of the track
2507 if( config.next_keyframe_position == config.prev_keyframe_position )
2508 config.next_keyframe_position = get_source_start() + get_total_len();
2509 if( config.prev_keyframe_position == 0 )
2510 config.prev_keyframe_position = get_source_start();
2511 // printf("TitleMain::load_configuration 10 %d %d\n",
2512 // config.prev_keyframe_position,
2513 // config.next_keyframe_position);
2515 config.interpolate(prev_config,
2517 (next_keyframe->position == prev_keyframe->position) ?
2518 get_source_position() :
2519 prev_keyframe->position,
2520 (next_keyframe->position == prev_keyframe->position) ?
2521 get_source_position() + 1 :
2522 next_keyframe->position,
2523 get_source_position());
2525 if( !config.equivalent(old_config) )
2531 void TitleMain::save_data(KeyFrame *keyframe)
2535 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
2536 output.tag.set_title("TITLE");
2537 output.tag.set_property("FONT", config.font);
2538 output.tag.set_property("ENCODING", config.encoding);
2539 output.tag.set_property("STYLE", (int64_t)config.style);
2540 output.tag.set_property("SIZE", config.size);
2541 output.tag.set_property("COLOR", config.color);
2542 output.tag.set_property("ALPHA", config.alpha);
2543 output.tag.set_property("OUTLINE_SIZE", config.outline_size);
2544 output.tag.set_property("OUTLINE_COLOR", config.outline_color);
2545 output.tag.set_property("OUTLINE_ALPHA", config.outline_alpha);
2546 output.tag.set_property("COLOR_STROKE", config.color_stroke);
2547 output.tag.set_property("STROKE_WIDTH", config.stroke_width);
2548 output.tag.set_property("MOTION_STRATEGY", config.motion_strategy);
2549 output.tag.set_property("LOOP", config.loop);
2550 output.tag.set_property("LINE_PITCH", config.line_pitch);
2551 output.tag.set_property("PIXELS_PER_SECOND", config.pixels_per_second);
2552 output.tag.set_property("HJUSTIFICATION", config.hjustification);
2553 output.tag.set_property("VJUSTIFICATION", config.vjustification);
2554 output.tag.set_property("FADE_IN", config.fade_in);
2555 output.tag.set_property("FADE_OUT", config.fade_out);
2556 output.tag.set_property("TITLE_X", config.title_x);
2557 output.tag.set_property("TITLE_Y", config.title_y);
2558 output.tag.set_property("TITLE_W", config.title_w);
2559 output.tag.set_property("TITLE_H", config.title_h);
2560 output.tag.set_property("DROPSHADOW", config.dropshadow);
2561 output.tag.set_property("TIMECODE", config.timecode);
2562 output.tag.set_property("TIMECODEFORMAT", config.timecode_format);
2563 output.tag.set_property("WINDOW_W", config.window_w);
2564 output.tag.set_property("WINDOW_H", config.window_h);
2565 output.tag.set_property("DRAG", config.drag);
2566 output.tag.set_property("BACKGROUND", config.background);
2567 output.tag.set_property("BACKGROUND_PATH", config.background_path);
2568 output.tag.set_property("LOOP_PLAYBACK", config.loop_playback);
2569 output.append_tag();
2570 output.append_newline();
2571 char text[BCTEXTLEN];
2572 int text_len = BC_Resources::encode(
2573 BC_Resources::wide_encoding, DEFAULT_ENCODING,
2574 (char*)config.wtext, config.wlen*sizeof(wchar_t),
2575 text, sizeof(text));
2576 output.append_text(text, text_len);
2577 output.tag.set_title("/TITLE");
2578 output.append_tag();
2579 output.append_newline();
2580 output.terminate_string();
2581 //printf("TitleMain::save_data 1\n%s\n", output.string);
2582 //printf("TitleMain::save_data 2\n%s\n", config.text);
2585 void TitleMain::read_data(KeyFrame *keyframe)
2589 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
2593 config.prev_keyframe_position = keyframe->position;
2596 result = input.read_tag();
2599 if( input.tag.title_is("TITLE") ) {
2600 input.tag.get_property("FONT", config.font);
2601 input.tag.get_property("ENCODING", config.encoding);
2602 config.style = input.tag.get_property("STYLE", (int64_t)config.style);
2603 config.size = input.tag.get_property("SIZE", config.size);
2604 config.color = input.tag.get_property("COLOR", config.color);
2605 config.alpha = input.tag.get_property("ALPHA", config.alpha);
2606 config.outline_size = input.tag.get_property("OUTLINE_SIZE", config.outline_size);
2607 config.outline_color = input.tag.get_property("OUTLINE_COLOR", config.outline_color);
2608 config.outline_alpha = input.tag.get_property("OUTLINE_ALPHA", config.outline_alpha);
2609 config.color_stroke = input.tag.get_property("COLOR_STROKE", config.color_stroke);
2610 config.stroke_width = input.tag.get_property("STROKE_WIDTH", config.stroke_width);
2611 config.motion_strategy = input.tag.get_property("MOTION_STRATEGY", config.motion_strategy);
2612 config.loop = input.tag.get_property("LOOP", config.loop);
2613 config.line_pitch = input.tag.get_property("LINE_PITCH", config.line_pitch);
2614 config.pixels_per_second = input.tag.get_property("PIXELS_PER_SECOND", config.pixels_per_second);
2615 config.hjustification = input.tag.get_property("HJUSTIFICATION", config.hjustification);
2616 config.vjustification = input.tag.get_property("VJUSTIFICATION", config.vjustification);
2617 config.fade_in = input.tag.get_property("FADE_IN", config.fade_in);
2618 config.fade_out = input.tag.get_property("FADE_OUT", config.fade_out);
2619 config.title_x = input.tag.get_property("TITLE_X", config.title_x);
2620 config.title_y = input.tag.get_property("TITLE_Y", config.title_y);
2621 config.title_w = input.tag.get_property("TITLE_W", config.title_w);
2622 config.title_h = input.tag.get_property("TITLE_H", config.title_h);
2623 config.dropshadow = input.tag.get_property("DROPSHADOW", config.dropshadow);
2624 config.timecode = input.tag.get_property("TIMECODE", config.timecode);
2625 config.timecode_format = input.tag.get_property("TIMECODEFORMAT", config.timecode_format);
2626 config.window_w = input.tag.get_property("WINDOW_W", config.window_w);
2627 config.window_h = input.tag.get_property("WINDOW_H", config.window_h);
2628 config.drag = input.tag.get_property("DRAG", config.drag);
2629 config.background = input.tag.get_property("BACKGROUND", config.background);
2630 input.tag.get_property("BACKGROUND_PATH", config.background_path);
2631 config.loop_playback = input.tag.get_property("LOOP_PLAYBACK", config.loop_playback);
2632 const char *text = input.read_text();
2633 config.to_wtext(config.encoding, text, strlen(text)+1);
2635 else if( input.tag.title_is("/TITLE") ) {
2641 void TitleMain::insert_text(const char *txt, int pos)
2643 int ilen = strlen(txt);
2644 wchar_t *wtext = config.wtext;
2645 int wsize = sizeof(config.wtext)-1;
2646 int wlen = config.wlen;
2647 if( pos < 0 ) pos = 0;
2648 if( pos > wlen ) pos = wlen;
2650 for( int i=wlen-1, j=wlen+ilen-1; i>=pos; --i,--j ) {
2651 if( j >= wsize ) continue;
2652 wtext[j] = wtext[i];
2654 for( int i=pos, j=0; j<ilen; ++i,++j ) {
2655 if( i >= wsize ) break;
2659 if( (wlen+=ilen) > wsize ) wlen = wsize;