12 #include "stream/stream.h"
13 #include "stream/stream_dvdnav.h"
14 #define OSD_NAV_BOX_ALPHA 0x7f
17 #ifdef HAVE_TV_TELETEXT
18 #include "stream/tv.h"
19 #include "osdep/timer.h"
25 #include "video_out.h"
26 #include "font_load.h"
29 #include "libavutil/common.h"
34 // Structures needed for the new splitting algorithm.
35 // osd_text_t contains the single subtitle word.
36 // osd_text_p is used to mark the lines of subtitles
38 int osd_kerning
, //kerning with the previous word
39 osd_length
, //orizontal length inside the bbox
40 text_length
, //number of characters
42 struct osd_text_t
*prev
,
48 struct osd_text_t
*ott
;
49 struct osd_text_p
*prev
,
54 char * sub_osd_names
[]={
62 MSGTR_VO_SUB_Contrast
,
63 MSGTR_VO_SUB_Saturation
,
65 MSGTR_VO_SUB_Brightness
,
69 char * sub_osd_names_short
[] ={ "", "|>", "||", "[]", "<<" , ">>", "", "", "", "", "", "", "" };
71 //static int vo_font_loaded=-1;
72 font_desc_t
* vo_font
=NULL
;
73 font_desc_t
* sub_font
=NULL
;
75 unsigned char* vo_osd_text
=NULL
;
76 #ifdef HAVE_TV_TELETEXT
77 void* vo_osd_teletext_page
=NULL
;
78 int vo_osd_teletext_half
= 0;
79 int vo_osd_teletext_mode
=0;
80 int vo_osd_teletext_format
=0;
81 int vo_osd_teletext_scale
=0;
87 int sub_alignment
=2; /* 0=top, 1=center, 2=bottom */
89 int sub_bg_color
=0; /* subtitles background color */
93 static nav_highlight_t nav_hl
;
96 // return the real height of a char:
97 static inline int get_height(int c
,int h
){
99 if ((font
=vo_font
->font
[c
])>=0)
100 if(h
<vo_font
->pic_a
[font
]->h
) h
=vo_font
->pic_a
[font
]->h
;
104 // renders char to a big per-object buffer where alpha and bitmap are separated
105 static void draw_alpha_buf(mp_osd_obj_t
* obj
, int x0
,int y0
, int w
,int h
, unsigned char* src
, unsigned char *srca
, int stride
)
107 int dststride
= obj
->stride
;
108 int dstskip
= obj
->stride
-w
;
109 int srcskip
= stride
-w
;
111 unsigned char *b
= obj
->bitmap_buffer
+ (y0
-obj
->bbox
.y1
)*dststride
+ (x0
-obj
->bbox
.x1
);
112 unsigned char *a
= obj
->alpha_buffer
+ (y0
-obj
->bbox
.y1
)*dststride
+ (x0
-obj
->bbox
.x1
);
113 unsigned char *bs
= src
;
114 unsigned char *as
= srca
;
116 if (x0
< obj
->bbox
.x1
|| x0
+w
> obj
->bbox
.x2
|| y0
< obj
->bbox
.y1
|| y0
+h
> obj
->bbox
.y2
) {
117 fprintf(stderr
, "osd text out of range: bbox [%d %d %d %d], txt [%d %d %d %d]\n",
118 obj
->bbox
.x1
, obj
->bbox
.x2
, obj
->bbox
.y1
, obj
->bbox
.y2
,
123 for (i
= 0; i
< h
; i
++) {
124 for (j
= 0; j
< w
; j
++, b
++, a
++, bs
++, as
++) {
125 if (*b
< *bs
) *b
= *bs
;
127 if (*a
== 0 || *a
> *as
) *a
= *as
;
137 // allocates/enlarges the alpha/bitmap buffer
138 static void alloc_buf(mp_osd_obj_t
* obj
)
141 if (obj
->bbox
.x2
< obj
->bbox
.x1
) obj
->bbox
.x2
= obj
->bbox
.x1
;
142 if (obj
->bbox
.y2
< obj
->bbox
.y1
) obj
->bbox
.y2
= obj
->bbox
.y1
;
143 obj
->stride
= ((obj
->bbox
.x2
-obj
->bbox
.x1
)+7)&(~7);
144 len
= obj
->stride
*(obj
->bbox
.y2
-obj
->bbox
.y1
);
145 if (obj
->allocated
<len
) {
146 obj
->allocated
= len
;
147 free(obj
->bitmap_buffer
);
148 free(obj
->alpha_buffer
);
149 obj
->bitmap_buffer
= (unsigned char *)memalign(16, len
);
150 obj
->alpha_buffer
= (unsigned char *)memalign(16, len
);
152 memset(obj
->bitmap_buffer
, sub_bg_color
, len
);
153 memset(obj
->alpha_buffer
, sub_bg_alpha
, len
);
156 // renders the buffer
157 inline static void vo_draw_text_from_buffer(mp_osd_obj_t
* obj
,void (*draw_alpha
)(int x0
,int y0
, int w
,int h
, unsigned char* src
, unsigned char *srca
, int stride
)){
158 if (obj
->allocated
> 0) {
159 draw_alpha(obj
->bbox
.x1
,obj
->bbox
.y1
,
160 obj
->bbox
.x2
-obj
->bbox
.x1
,
161 obj
->bbox
.y2
-obj
->bbox
.y1
,
168 unsigned utf8_get_char(const char **str
) {
169 const uint8_t *strp
= (const uint8_t *)*str
;
171 GET_UTF8(c
, *strp
++, goto no_utf8
;);
172 *str
= (const char *)strp
;
176 strp
= (const uint8_t *)*str
;
178 *str
= (const char *)strp
;
182 inline static void vo_update_text_osd(mp_osd_obj_t
* obj
,int dxs
,int dys
){
183 const char *cp
=vo_osd_text
;
188 obj
->bbox
.x1
=obj
->x
=x
;
189 obj
->bbox
.y1
=obj
->y
=10;
192 uint16_t c
=utf8_get_char(&cp
);
193 render_one_glyph(vo_font
, c
);
194 x
+=vo_font
->width
[c
]+vo_font
->charspace
;
198 obj
->bbox
.x2
=x
-vo_font
->charspace
;
199 obj
->bbox
.y2
=obj
->bbox
.y1
+h
;
200 obj
->flags
|=OSDFLAG_BBOX
;
207 uint16_t c
=utf8_get_char(&cp
);
208 if ((font
=vo_font
->font
[c
])>=0)
209 draw_alpha_buf(obj
,x
,obj
->y
,
211 vo_font
->pic_a
[font
]->h
,
212 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
213 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
214 vo_font
->pic_a
[font
]->w
);
215 x
+=vo_font
->width
[c
]+vo_font
->charspace
;
220 void osd_set_nav_box (uint16_t sx
, uint16_t sy
, uint16_t ex
, uint16_t ey
) {
227 inline static void vo_update_nav (mp_osd_obj_t
*obj
, int dxs
, int dys
) {
230 obj
->bbox
.x1
= obj
->x
= nav_hl
.sx
;
231 obj
->bbox
.y1
= obj
->y
= nav_hl
.sy
;
232 obj
->bbox
.x2
= nav_hl
.ex
;
233 obj
->bbox
.y2
= nav_hl
.ey
;
236 len
= obj
->stride
* (obj
->bbox
.y2
- obj
->bbox
.y1
);
237 memset (obj
->bitmap_buffer
, OSD_NAV_BOX_ALPHA
, len
);
238 memset (obj
->alpha_buffer
, OSD_NAV_BOX_ALPHA
, len
);
239 obj
->flags
|= OSDFLAG_BBOX
| OSDFLAG_CHANGED
;
240 if (obj
->bbox
.y2
> obj
->bbox
.y1
&& obj
->bbox
.x2
> obj
->bbox
.x1
)
241 obj
->flags
|= OSDFLAG_VISIBLE
;
245 #ifdef HAVE_TV_TELETEXT
246 // renders char to a big per-object buffer where alpha and bitmap are separated
247 static void tt_draw_alpha_buf(mp_osd_obj_t
* obj
, int x0
,int y0
, int w
,int h
, unsigned char* src
, int stride
,int fg
,int bg
,int alpha
)
249 int dststride
= obj
->stride
;
250 int dstskip
= obj
->stride
-w
;
251 int srcskip
= stride
-w
;
253 unsigned char *b
= obj
->bitmap_buffer
+ (y0
-obj
->bbox
.y1
)*dststride
+ (x0
-obj
->bbox
.x1
);
254 unsigned char *a
= obj
->alpha_buffer
+ (y0
-obj
->bbox
.y1
)*dststride
+ (x0
-obj
->bbox
.x1
);
255 unsigned char *bs
= src
;
256 if (x0
< obj
->bbox
.x1
|| x0
+w
> obj
->bbox
.x2
|| y0
< obj
->bbox
.y1
|| y0
+h
> obj
->bbox
.y2
) {
257 mp_msg(MSGT_OSD
,MSGL_ERR
,"tt osd text out of range: bbox [%d %d %d %d], txt [%d %d %d %d]\n",
258 obj
->bbox
.x1
, obj
->bbox
.x2
, obj
->bbox
.y1
, obj
->bbox
.y2
,
262 for (i
= 0; i
< h
; i
++) {
263 for (j
= 0; j
< w
; j
++, b
++, a
++, bs
++) {
264 *b
=(fg
-bg
)*(*bs
)/255+bg
;
272 inline static void vo_update_text_teletext(mp_osd_obj_t
*obj
, int dxs
, int dys
)
274 int h
=0,w
=0,i
,j
,font
,flashon
;
282 int start_row
,max_rows
;
283 int b
,ax
[6],ay
[6],aw
[6],ah
[6];
285 tt_char
* tdp
=vo_osd_teletext_page
;
286 unsigned char colors
[8]={1,85,150,226,70,105,179,254};
287 unsigned char* buf
[9];
289 obj
->flags
|=OSDFLAG_CHANGED
|OSDFLAG_VISIBLE
;
290 if (!tdp
|| !vo_osd_teletext_mode
) {
291 obj
->flags
&=~OSDFLAG_VISIBLE
;
294 flashon
=(GetTimer()/1000000)%2;
295 switch(vo_osd_teletext_half
){
296 case TT_ZOOM_TOP_HALF
:
300 case TT_ZOOM_BOTTOM_HALF
:
301 start_row
=VBI_ROWS
/2;
310 for(i
=start_row
;i
<max_rows
;i
++){
311 for(j
=0;j
<VBI_COLUMNS
;j
++){
312 tc
=tdp
[i
*VBI_COLUMNS
+j
];
313 if(!tc
.ctl
&& !tc
.gfx
)
315 render_one_glyph(vo_font
, tc
.unicode
);
316 if (wm
<vo_font
->width
[tc
.unicode
])
317 wm
=vo_font
->width
[tc
.unicode
];
322 hm
=vo_font
->height
+1;
323 wm
=dxs
*hm
*max_rows
/(dys
*VBI_COLUMNS
);
325 //very simple teletext font auto scaling
326 if(!vo_osd_teletext_scale
&& hm
*(max_rows
+1)>dys
){
327 text_font_scale_factor
*=1.0*(dys
)/((max_rows
+1)*hm
);
329 vo_osd_teletext_scale
=text_font_scale_factor
;
330 obj
->flags
&=~OSDFLAG_VISIBLE
;
341 w
=cols
*wm
-vo_font
->charspace
;
342 h
=rows
*hm
-vo_font
->charspace
;
382 obj
->flags
|= OSDFLAG_BBOX
;
386 buf
[i
]=malloc(wm
*hm
);
389 if(vo_osd_teletext_format
==TT_FORMAT_OPAQUE
||vo_osd_teletext_format
==TT_FORMAT_OPAQUE_INV
)
393 memset(buf
[8],color
,wm
*hm
);
395 if(vo_osd_teletext_format
==TT_FORMAT_OPAQUE
||vo_osd_teletext_format
==TT_FORMAT_TRANSPARENT
){
397 memset(buf
[i
],(unsigned char)(1.0*(255-color
)*colors
[i
]/255),wm
*hm
);
401 memset(buf
[i
],(unsigned char)(1.0*(255-color
)*colors
[7-i
]/255),wm
*hm
);
408 tc
=tdp
[(i
+start_row
)*VBI_COLUMNS
+j
];
409 if (tc
.hidden
) { x
+=wm
; continue;}
410 if(!tc
.gfx
|| (tc
.flh
&& !flashon
)){
411 /* Rendering one text character */
412 draw_alpha_buf(obj
,x
,y
,wm
,hm
,buf
[tc
.bg
],buf
[8],wm
);
413 if(tc
.unicode
!=0x20 && tc
.unicode
!=0x00 && !tc
.ctl
&&
414 (!tc
.flh
|| flashon
) &&
415 (font
=vo_font
->font
[tc
.unicode
])>=0 && y
+hm
<dys
){
416 tt_draw_alpha_buf(obj
,x
,y
,vo_font
->width
[tc
.unicode
],vo_font
->height
,
417 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[tc
.unicode
]-vo_font
->charspace
*vo_font
->pic_a
[font
]->w
,
418 vo_font
->pic_b
[font
]->w
,
419 buf
[tc
.fg
][0],buf
[tc
.bg
][0],buf
[8][0]);
423 Rendering one graphics character
424 TODO: support for separated graphics symbols (where six rectangles does not touch each other)
428 |10| <= |34| <= 00100110 <= 0x26
432 (0:wm/2) (wm/2:wm-wm/2)
434 ********** *********** (0:hm/3)
436 *** 1 **** **** 2 ****
438 ********** ***********
439 ********** ***********
441 ********** *********** (hm/3:hm-2*hm/3)
442 ********** ***********
444 *** 3 **** **** 4 ****
446 ********** ***********
447 ********** ***********
448 ********** ***********
450 ********** *********** (hm-hm/3:hm/3)
452 *** 5 **** **** 6 ****
454 ********** ***********
455 ********** ***********
458 if(tc
.gfx
>1){ //separated gfx
460 color
=(tc
.unicode
>>b
)&1?tc
.fg
:tc
.bg
;
461 draw_alpha_buf(obj
,x
+ax
[b
]+1,y
+ay
[b
]+1,aw
[b
]-2,ah
[b
]-2,buf
[color
],buf
[8],wm
);
463 //separated gfx (background borders)
465 draw_alpha_buf(obj
,x
,y
,1,hm
,buf
[tc
.bg
],buf
[8],wm
);
466 draw_alpha_buf(obj
,x
+ax
[1]-1,y
,2,hm
,buf
[tc
.bg
],buf
[8],wm
);
467 draw_alpha_buf(obj
,x
+ax
[1]+aw
[1]-1,y
,wm
-ax
[1]-aw
[1]+1,hm
,buf
[tc
.bg
],buf
[8],wm
);
469 draw_alpha_buf(obj
,x
,y
,wm
,1,buf
[tc
.bg
],buf
[8],wm
);
470 draw_alpha_buf(obj
,x
,y
+ay
[0]+ah
[0]-1,wm
,2,buf
[tc
.bg
],buf
[8],wm
);
471 draw_alpha_buf(obj
,x
,y
+ay
[2]+ah
[2]-1,wm
,2,buf
[tc
.bg
],buf
[8],wm
);
472 draw_alpha_buf(obj
,x
,y
+ay
[4]+ah
[4]-1,wm
,hm
-ay
[4]-ah
[4]+1,buf
[tc
.bg
],buf
[8],wm
);
475 color
=(tc
.unicode
>>b
)&1?tc
.fg
:tc
.bg
;
476 draw_alpha_buf(obj
,x
+ax
[b
],y
+ay
[b
],aw
[b
],ah
[b
],buf
[color
],buf
[8],wm
);
489 int vo_osd_progbar_type
=-1;
490 int vo_osd_progbar_value
=100; // 0..256
492 // if we have n=256 bars then OSD progbar looks like below
494 // 0 1 2 3 ... 256 <= vo_osd_progbar_value
496 // [ === === === ... === ]
498 // the above schema is rescalled to n=elems bars
500 inline static void vo_update_text_progbar(mp_osd_obj_t
* obj
,int dxs
,int dys
){
502 obj
->flags
|=OSDFLAG_CHANGED
|OSDFLAG_VISIBLE
;
504 if(vo_osd_progbar_type
<0 || !vo_font
){
505 obj
->flags
&=~OSDFLAG_VISIBLE
;
509 render_one_glyph(vo_font
, OSD_PB_START
);
510 render_one_glyph(vo_font
, OSD_PB_END
);
511 render_one_glyph(vo_font
, OSD_PB_0
);
512 render_one_glyph(vo_font
, OSD_PB_1
);
513 render_one_glyph(vo_font
, vo_osd_progbar_type
);
515 // calculate bbox corners:
517 int y
=(dys
-vo_font
->height
)/2;
518 int delimw
=vo_font
->width
[OSD_PB_START
]
519 +vo_font
->width
[OSD_PB_END
]
521 int width
=(2*dxs
-3*delimw
)/3;
522 int charw
=vo_font
->width
[OSD_PB_0
]+vo_font
->charspace
;
523 int elems
=width
/charw
;
524 int x
=(dxs
-elems
*charw
-delimw
)/2;
526 h
=get_height(OSD_PB_START
,h
);
527 h
=get_height(OSD_PB_END
,h
);
528 h
=get_height(OSD_PB_0
,h
);
529 h
=get_height(OSD_PB_1
,h
);
530 if (vo_osd_progbar_type
>0 && vo_font
->font
[vo_osd_progbar_type
]>=0){
531 delta
= vo_font
->width
[vo_osd_progbar_type
]+vo_font
->spacewidth
;
532 delta
= (x
-delta
> 0) ? delta
: x
;
533 h
=get_height(vo_osd_progbar_type
,h
);
535 obj
->bbox
.x1
=obj
->x
=x
;
536 obj
->bbox
.y1
=obj
->y
=y
;
537 obj
->bbox
.x2
=x
+width
+delimw
;
538 obj
->bbox
.y2
=y
+h
; //vo_font->height;
539 obj
->flags
|=OSDFLAG_BBOX
;
540 obj
->params
.progbar
.elems
=elems
;
541 obj
->bbox
.x1
-=delta
; // space for an icon
547 int minw
= vo_font
->width
[OSD_PB_START
]+vo_font
->width
[OSD_PB_END
]+vo_font
->width
[OSD_PB_0
];
548 if (vo_osd_progbar_type
>0 && vo_font
->font
[vo_osd_progbar_type
]>=0){
549 minw
+= vo_font
->width
[vo_osd_progbar_type
]+vo_font
->charspace
+vo_font
->spacewidth
;
551 if (obj
->bbox
.x2
- obj
->bbox
.x1
< minw
) return; // space too small, don't render anything
561 int charw
=vo_font
->width
[OSD_PB_0
]+vo_font
->charspace
;
562 int elems
=obj
->params
.progbar
.elems
;
564 if (vo_osd_progbar_value
<=0)
567 int ev
=vo_osd_progbar_value
*elems
;
569 if (ev
& 0xFF) mark
++;
570 if (mark
>elems
) mark
=elems
;
574 // printf("osd.progbar width=%d xpos=%d\n",width,x);
576 c
=vo_osd_progbar_type
;
577 if(vo_osd_progbar_type
>0 && (font
=vo_font
->font
[c
])>=0) {
578 int xp
=x
-vo_font
->width
[c
]-vo_font
->spacewidth
;
579 draw_alpha_buf(obj
,(xp
<0?0:xp
),y
,
581 vo_font
->pic_a
[font
]->h
,
582 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
583 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
584 vo_font
->pic_a
[font
]->w
);
588 if ((font
=vo_font
->font
[c
])>=0)
589 draw_alpha_buf(obj
,x
,y
,
591 vo_font
->pic_a
[font
]->h
,
592 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
593 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
594 vo_font
->pic_a
[font
]->w
);
595 x
+=vo_font
->width
[c
]+vo_font
->charspace
;
598 if ((font
=vo_font
->font
[c
])>=0){
600 h
=vo_font
->pic_a
[font
]->h
;
601 s
=vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
];
602 sa
=vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
];
603 st
=vo_font
->pic_a
[font
]->w
;
605 draw_alpha_buf(obj
,x
,y
,w
,h
,s
,sa
,st
);
611 if ((font
=vo_font
->font
[c
])>=0){
613 h
=vo_font
->pic_a
[font
]->h
;
614 s
=vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
];
615 sa
=vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
];
616 st
=vo_font
->pic_a
[font
]->w
;
617 if ((i
=elems
-mark
)) do {
618 draw_alpha_buf(obj
,x
,y
,w
,h
,s
,sa
,st
);
624 if ((font
=vo_font
->font
[c
])>=0)
625 draw_alpha_buf(obj
,x
,y
,
627 vo_font
->pic_a
[font
]->h
,
628 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
629 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
630 vo_font
->pic_a
[font
]->w
);
631 // x+=vo_font->width[c]+vo_font->charspace;
634 // vo_osd_progbar_value=(vo_osd_progbar_value+1)&0xFF;
638 subtitle
* vo_sub
=NULL
;
640 // vo_draw_text_sub(int dxs,int dys,void (*draw_alpha)(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride))
642 inline static void vo_update_text_sub(mp_osd_obj_t
* obj
,int dxs
,int dys
){
644 int c
,i
,j
,l
,x
,y
,font
,prevc
,counter
;
646 int lastStripPosition
;
652 obj
->flags
|=OSDFLAG_CHANGED
|OSDFLAG_VISIBLE
;
654 if(!vo_sub
|| !sub_font
|| !sub_visibility
|| (sub_font
->font
[40]<0)){
655 obj
->flags
&=~OSDFLAG_VISIBLE
;
659 obj
->bbox
.y2
=obj
->y
=dys
;
660 obj
->params
.subtitle
.lines
=0;
662 // too long lines divide into a smaller ones
665 lastStripPosition
=-1;
669 struct osd_text_t
*osl
, *cp_ott
, *tmp_ott
, *tmp
;
670 struct osd_text_p
*otp_sub
= NULL
, *otp_sub_tmp
, // these are used to store the whole sub text osd
671 *otp
, *tmp_otp
, *pmt
; // these are used to manage sub text osd coming from a single sub line
672 int *char_seq
, char_position
, xlimit
= dxs
* sub_width_p
/ 100, counter
;
675 xsize
= -sub_font
->charspace
;
679 char_seq
= calloc(strlen(t
), sizeof(int));
687 // reading the subtitle words from vo_sub->text[]
690 c
= utf8_get_char(&t
);
691 else if ((c
= *t
++) >= 0x80 && sub_unicode
)
694 t
+= strlen(t
); // end here
695 mp_msg(MSGT_OSD
,MSGL_WARN
,"\nMAX_UCS exceeded!\n");
697 if (!c
) c
++; // avoid UCS 0
698 render_one_glyph(sub_font
, c
);
701 struct osd_text_t
*tmp_ott
= (struct osd_text_t
*) calloc(1, sizeof(struct osd_text_t
));
704 osl
= cp_ott
= tmp_ott
;
706 tmp_ott
->prev
= cp_ott
;
707 cp_ott
->next
= tmp_ott
;
708 tmp_ott
->osd_kerning
=
709 sub_font
->charspace
+ sub_font
->width
[' '];
712 tmp_ott
->osd_length
= xsize
;
713 tmp_ott
->text_length
= char_position
;
714 tmp_ott
->text
= (int *) malloc(char_position
* sizeof(int));
715 for (counter
= 0; counter
< char_position
; ++counter
)
716 tmp_ott
->text
[counter
] = char_seq
[counter
];
721 int delta_xsize
= sub_font
->width
[c
] + sub_font
->charspace
+ kerning(sub_font
, prevc
, c
);
723 if (xsize
+ delta_xsize
<= dxs
) {
726 char_seq
[char_position
++] = c
;
727 xsize
+= delta_xsize
;
728 if ((!suboverlap_enabled
) && ((font
= sub_font
->font
[c
]) >= 0)) {
729 if (sub_font
->pic_a
[font
]->h
> h
) {
730 h
= sub_font
->pic_a
[font
]->h
;
735 mp_msg(MSGT_OSD
, MSGL_WARN
, "\nSubtitle word '%s' too long!\n", t
);
740 }// for len (all words from subtitle line read)
742 // osl holds an ordered (as they appear in the lines) chain of the subtitle words
744 struct osd_text_t
*tmp_ott
= (struct osd_text_t
*) calloc(1, sizeof(struct osd_text_t
));
747 osl
= cp_ott
= tmp_ott
;
749 tmp_ott
->prev
= cp_ott
;
750 cp_ott
->next
= tmp_ott
;
751 tmp_ott
->osd_kerning
=
752 sub_font
->charspace
+ sub_font
->width
[' '];
755 tmp_ott
->osd_length
= xsize
;
756 tmp_ott
->text_length
= char_position
;
757 tmp_ott
->text
= (int *) malloc(char_position
* sizeof(int));
758 for (counter
= 0; counter
< char_position
; ++counter
)
759 tmp_ott
->text
[counter
] = char_seq
[counter
];
761 xsize
= -sub_font
->charspace
;
766 int value
= 0, exit
= 0, minimum
= 0;
768 // otp will contain the chain of the osd subtitle lines coming from the single vo_sub line.
769 otp
= tmp_otp
= (struct osd_text_p
*) calloc(1, sizeof(struct osd_text_p
));
771 for (tmp_ott
= tmp_otp
->ott
; exit
== 0; ) {
773 value
+= tmp_ott
->osd_kerning
+ tmp_ott
->osd_length
;
774 tmp_ott
= tmp_ott
->next
;
775 } while ((tmp_ott
!= NULL
) && (value
+ tmp_ott
->osd_kerning
+ tmp_ott
->osd_length
<= xlimit
));
776 if (tmp_ott
!= NULL
) {
777 struct osd_text_p
*tmp
= (struct osd_text_p
*) calloc(1, sizeof(struct osd_text_p
));
779 tmp_otp
->value
= value
;
783 tmp_otp
->ott
= tmp_ott
;
784 value
= -2 * sub_font
->charspace
- sub_font
->width
[' '];
786 tmp_otp
->value
= value
;
793 // minimum holds the 'sum of the differences in length among the lines',
794 // a measure of the evenness of the lengths of the lines
795 for (tmp_otp
= otp
; tmp_otp
->next
!= NULL
; tmp_otp
= tmp_otp
->next
) {
797 while (pmt
!= NULL
) {
798 minimum
+= abs(tmp_otp
->value
- pmt
->value
);
803 if (otp
->next
!= NULL
) {
805 struct osd_text_p
*mem
, *hold
;
808 // until the last word of a line can be moved to the beginning of following line
809 // reducing the 'sum of the differences in length among the lines', it is done
813 for (tmp_otp
= otp
; tmp_otp
->next
!= NULL
; tmp_otp
= tmp_otp
->next
) {
815 for (tmp
= tmp_otp
->ott
; tmp
->next
!= pmt
->ott
; tmp
= tmp
->next
);
816 if (pmt
->value
+ tmp
->osd_length
+ pmt
->ott
->osd_kerning
<= xlimit
) {
817 mem1
= tmp_otp
->value
;
819 tmp_otp
->value
= mem1
- tmp
->osd_length
- tmp
->osd_kerning
;
820 pmt
->value
= mem2
+ tmp
->osd_length
+ pmt
->ott
->osd_kerning
;
823 for (mem
= otp
; mem
->next
!= NULL
; mem
= mem
->next
) {
825 while (pmt
!= NULL
) {
826 value
+= abs(mem
->value
- pmt
->value
);
830 if (value
< minimum
) {
835 tmp_otp
->value
= mem1
;
836 tmp_otp
->next
->value
= mem2
;
843 for (tmp
= tmp_otp
->ott
; tmp
->next
!= pmt
->ott
; tmp
= tmp
->next
);
844 mem1
= tmp_otp
->value
;
846 tmp_otp
->value
= mem1
- tmp
->osd_length
- tmp
->osd_kerning
;
847 pmt
->value
= mem2
+ tmp
->osd_length
+ pmt
->ott
->osd_kerning
;
851 }//~if(otp->next!=NULL)
854 // adding otp (containing splitted lines) to otp chain
855 if (otp_sub
== NULL
) {
857 for (otp_sub_tmp
= otp_sub
; otp_sub_tmp
->next
!= NULL
; otp_sub_tmp
= otp_sub_tmp
->next
);
861 while (tmp
->next
!= NULL
) tmp
= tmp
->next
;
862 tmp
->next
= otp
->ott
;
863 otp
->ott
->prev
= tmp
;
864 //attaching new subtitle line at the end
865 otp_sub_tmp
->next
= otp
;
866 otp
->prev
= otp_sub_tmp
;
868 otp_sub_tmp
= otp_sub_tmp
->next
;
869 while (otp_sub_tmp
->next
!= NULL
);
874 // write lines into utbl
878 obj
->params
.subtitle
.lines
= 0;
879 for (tmp_otp
= otp_sub
; tmp_otp
!= NULL
; tmp_otp
= tmp_otp
->next
) {
881 if ((obj
->params
.subtitle
.lines
++) >= MAX_UCSLINES
)
884 if (h
> obj
->y
) { // out of the screen so end parsing
885 obj
->y
-= lasth
- sub_font
->height
; // correct the y position
888 xsize
= tmp_otp
->value
;
889 obj
->params
.subtitle
.xtbl
[xtblc
++] = (dxs
- xsize
) / 2;
890 if (xmin
> (dxs
- xsize
) / 2)
891 xmin
= (dxs
- xsize
) / 2;
892 if (xmax
< (dxs
+ xsize
) / 2)
893 xmax
= (dxs
+ xsize
) / 2;
895 tmp
= (tmp_otp
->next
== NULL
) ? NULL
: tmp_otp
->next
->ott
;
896 for (tmp_ott
= tmp_otp
->ott
; tmp_ott
!= tmp
; tmp_ott
= tmp_ott
->next
) {
897 for (counter
= 0; counter
< tmp_ott
->text_length
; ++counter
) {
898 if (utblc
> MAX_UCS
) {
901 c
= tmp_ott
->text
[counter
];
902 render_one_glyph(sub_font
, c
);
903 obj
->params
.subtitle
.utbl
[utblc
++] = c
;
906 obj
->params
.subtitle
.utbl
[utblc
++] = ' ';
908 obj
->params
.subtitle
.utbl
[utblc
- 1] = 0;
909 obj
->y
-= sub_font
->height
;
911 if(obj
->params
.subtitle
.lines
)
912 obj
->y
= dys
- ((obj
->params
.subtitle
.lines
- 1) * sub_font
->height
+ sub_font
->pic_a
[sub_font
->font
[40]]->h
);
915 if (otp_sub
!= NULL
) {
916 for (tmp
= otp_sub
->ott
; tmp
->next
!= NULL
; free(tmp
->prev
)) {
923 for(pmt
= otp_sub
; pmt
->next
!= NULL
; free(pmt
->prev
)) {
930 /// vertical alignment
932 if (sub_alignment
== 2)
933 obj
->y
= dys
* sub_pos
/ 100 - h
;
934 else if (sub_alignment
== 1)
935 obj
->y
= dys
* sub_pos
/ 100 - h
/ 2;
937 obj
->y
= dys
* sub_pos
/ 100;
941 if (obj
->y
> dys
- h
)
944 obj
->bbox
.y2
= obj
->y
+ h
;
947 if (sub_justify
) xmin
= 10;
951 // obj->bbox.y2=obj->y+obj->params.subtitle.lines*sub_font->height;
952 obj
->flags
|=OSDFLAG_BBOX
;
959 switch(vo_sub
->alignment
) {
960 case SUB_ALIGNMENT_BOTTOMLEFT
:
961 case SUB_ALIGNMENT_MIDDLELEFT
:
962 case SUB_ALIGNMENT_TOPLEFT
:
963 obj
->alignment
|= 0x1;
965 case SUB_ALIGNMENT_BOTTOMRIGHT
:
966 case SUB_ALIGNMENT_MIDDLERIGHT
:
967 case SUB_ALIGNMENT_TOPRIGHT
:
968 obj
->alignment
|= 0x2;
970 case SUB_ALIGNMENT_BOTTOMCENTER
:
971 case SUB_ALIGNMENT_MIDDLECENTER
:
972 case SUB_ALIGNMENT_TOPCENTER
:
974 obj
->alignment
|= 0x0;
978 if ((l
= obj
->params
.subtitle
.lines
)) {
979 for(counter
= dxs
; i
< l
; ++i
)
980 if (obj
->params
.subtitle
.xtbl
[i
] < counter
) counter
= obj
->params
.subtitle
.xtbl
[i
];
981 for (i
= 0; i
< l
; ++i
) {
982 switch (obj
->alignment
&0x3) {
989 x
= 2 * obj
->params
.subtitle
.xtbl
[i
] - counter
- ((obj
->params
.subtitle
.xtbl
[i
] == counter
) ? 0 : 1);
993 x
= obj
->params
.subtitle
.xtbl
[i
];
996 while ((c
=obj
->params
.subtitle
.utbl
[j
++])){
997 x
+= kerning(sub_font
,prevc
,c
);
998 if ((font
=sub_font
->font
[c
])>=0)
999 draw_alpha_buf(obj
,x
,y
,
1001 sub_font
->pic_a
[font
]->h
+y
<obj
->dys
? sub_font
->pic_a
[font
]->h
: obj
->dys
-y
,
1002 sub_font
->pic_b
[font
]->bmp
+sub_font
->start
[c
],
1003 sub_font
->pic_a
[font
]->bmp
+sub_font
->start
[c
],
1004 sub_font
->pic_a
[font
]->w
);
1005 x
+=sub_font
->width
[c
]+sub_font
->charspace
;
1008 y
+=sub_font
->height
;
1014 inline static void vo_update_spudec_sub(mp_osd_obj_t
* obj
, int dxs
, int dys
)
1016 unsigned int bbox
[4];
1017 spudec_calc_bbox(vo_spudec
, dxs
, dys
, bbox
);
1018 obj
->bbox
.x1
= bbox
[0];
1019 obj
->bbox
.x2
= bbox
[1];
1020 obj
->bbox
.y1
= bbox
[2];
1021 obj
->bbox
.y2
= bbox
[3];
1022 obj
->flags
|= OSDFLAG_BBOX
;
1025 inline static void vo_draw_spudec_sub(mp_osd_obj_t
* obj
, void (*draw_alpha
)(int x0
, int y0
, int w
, int h
, unsigned char* src
, unsigned char* srca
, int stride
))
1027 spudec_draw_scaled(vo_spudec
, obj
->dxs
, obj
->dys
, draw_alpha
);
1030 void *vo_spudec
=NULL
;
1031 void *vo_vobsub
=NULL
;
1033 static int draw_alpha_init_flag
=0;
1035 extern void vo_draw_alpha_init(void);
1037 mp_osd_obj_t
* vo_osd_list
=NULL
;
1039 static mp_osd_obj_t
* new_osd_obj(int type
){
1040 mp_osd_obj_t
* osd
=malloc(sizeof(mp_osd_obj_t
));
1041 memset(osd
,0,sizeof(mp_osd_obj_t
));
1042 osd
->next
=vo_osd_list
;
1045 osd
->alpha_buffer
= NULL
;
1046 osd
->bitmap_buffer
= NULL
;
1047 osd
->allocated
= -1;
1051 void free_osd_list(void){
1052 mp_osd_obj_t
* obj
=vo_osd_list
;
1054 mp_osd_obj_t
* next
=obj
->next
;
1055 if (obj
->alpha_buffer
) free(obj
->alpha_buffer
);
1056 if (obj
->bitmap_buffer
) free(obj
->bitmap_buffer
);
1063 #define FONT_LOAD_DEFER 6
1065 int vo_update_osd(int dxs
,int dys
){
1066 mp_osd_obj_t
* obj
=vo_osd_list
;
1068 #ifdef HAVE_FREETYPE
1069 static int defer_counter
= 0, prev_dxs
= 0, prev_dys
= 0;
1072 #ifdef HAVE_FREETYPE
1073 // here is the right place to get screen dimensions
1074 if (((dxs
!= vo_image_width
)
1075 && (subtitle_autoscale
== 2 || subtitle_autoscale
== 3))
1076 || ((dys
!= vo_image_height
)
1077 && (subtitle_autoscale
== 1 || subtitle_autoscale
== 3)))
1079 // screen dimensions changed
1080 // wait a while to avoid useless reloading of the font
1081 if (dxs
== prev_dxs
|| dys
== prev_dys
) {
1088 if (defer_counter
>= FONT_LOAD_DEFER
) force_load_font
= 1;
1091 if (force_load_font
) {
1092 force_load_font
= 0;
1093 load_font_ft(dxs
, dys
, &vo_font
, font_name
);
1095 load_font_ft(dxs
, dys
, &sub_font
, sub_font_name
);
1103 load_font_ft(dxs
, dys
, &vo_font
, font_name
);
1106 load_font_ft(dxs
, dys
, &sub_font
, sub_font_name
);
1114 if(dxs
!=obj
->dxs
|| dys
!=obj
->dys
|| obj
->flags
&OSDFLAG_FORCE_UPDATE
){
1115 int vis
=obj
->flags
&OSDFLAG_VISIBLE
;
1116 obj
->flags
&=~OSDFLAG_BBOX
;
1119 case OSDTYPE_DVDNAV
:
1120 vo_update_nav(obj
,dxs
,dys
);
1123 case OSDTYPE_SUBTITLE
:
1124 vo_update_text_sub(obj
,dxs
,dys
);
1126 #ifdef HAVE_TV_TELETEXT
1127 case OSDTYPE_TELETEXT
:
1128 vo_update_text_teletext(obj
,dxs
,dys
);
1131 case OSDTYPE_PROGBAR
:
1132 vo_update_text_progbar(obj
,dxs
,dys
);
1135 if(sub_visibility
&& vo_spudec
&& spudec_visible(vo_spudec
)){
1136 vo_update_spudec_sub(obj
, dxs
, dys
);
1137 obj
->flags
|=OSDFLAG_VISIBLE
|OSDFLAG_CHANGED
;
1140 obj
->flags
&=~OSDFLAG_VISIBLE
;
1143 if(vo_font
&& vo_osd_text
&& vo_osd_text
[0]){
1144 vo_update_text_osd(obj
,dxs
,dys
); // update bbox
1145 obj
->flags
|=OSDFLAG_VISIBLE
|OSDFLAG_CHANGED
;
1147 obj
->flags
&=~OSDFLAG_VISIBLE
;
1151 if(!(obj
->flags
&OSDFLAG_BBOX
)){
1152 // we don't know, so assume the whole screen changed :(
1153 obj
->bbox
.x1
=obj
->bbox
.y1
=0;
1156 obj
->flags
|=OSDFLAG_BBOX
;
1158 // check bbox, reduce it if it's out of bounds (corners):
1159 if(obj
->bbox
.x1
<0) obj
->bbox
.x1
=0;
1160 if(obj
->bbox
.y1
<0) obj
->bbox
.y1
=0;
1161 if(obj
->bbox
.x2
>dxs
) obj
->bbox
.x2
=dxs
;
1162 if(obj
->bbox
.y2
>dys
) obj
->bbox
.y2
=dys
;
1163 if(obj
->flags
&OSDFLAG_VISIBLE
)
1165 mp_msg(MSGT_OSD
,MSGL_DBG2
,"OSD update: %d;%d %dx%d \n",
1166 obj
->bbox
.x1
,obj
->bbox
.y1
,obj
->bbox
.x2
-obj
->bbox
.x1
,
1167 obj
->bbox
.y2
-obj
->bbox
.y1
);
1169 // check if visibility changed:
1170 if(vis
!= (obj
->flags
&OSDFLAG_VISIBLE
) ) obj
->flags
|=OSDFLAG_CHANGED
;
1171 // remove the cause of automatic update:
1172 obj
->dxs
=dxs
; obj
->dys
=dys
;
1173 obj
->flags
&=~OSDFLAG_FORCE_UPDATE
;
1175 if(obj
->flags
&OSDFLAG_CHANGED
){
1177 mp_msg(MSGT_OSD
,MSGL_DBG2
,"OSD chg: %d V: %s pb:%d \n",obj
->type
,(obj
->flags
&OSDFLAG_VISIBLE
)?"yes":"no",vo_osd_progbar_type
);
1184 void vo_init_osd(void){
1185 if(!draw_alpha_init_flag
){
1186 draw_alpha_init_flag
=1;
1187 vo_draw_alpha_init();
1189 if(vo_osd_list
) free_osd_list();
1190 // temp hack, should be moved to mplayer/mencoder later
1191 new_osd_obj(OSDTYPE_OSD
);
1192 new_osd_obj(OSDTYPE_SUBTITLE
);
1193 new_osd_obj(OSDTYPE_PROGBAR
);
1194 new_osd_obj(OSDTYPE_SPU
);
1196 new_osd_obj(OSDTYPE_DVDNAV
);
1198 #if HAVE_TV_TELETEXT
1199 new_osd_obj(OSDTYPE_TELETEXT
);
1201 #ifdef HAVE_FREETYPE
1202 force_load_font
= 1;
1206 int vo_osd_changed_flag
=0;
1208 void vo_remove_text(int dxs
,int dys
,void (*remove
)(int x0
,int y0
, int w
,int h
)){
1209 mp_osd_obj_t
* obj
=vo_osd_list
;
1210 vo_update_osd(dxs
,dys
);
1212 if(((obj
->flags
&OSDFLAG_CHANGED
) || (obj
->flags
&OSDFLAG_VISIBLE
)) &&
1213 (obj
->flags
&OSDFLAG_OLD_BBOX
)){
1214 int w
=obj
->old_bbox
.x2
-obj
->old_bbox
.x1
;
1215 int h
=obj
->old_bbox
.y2
-obj
->old_bbox
.y1
;
1217 vo_osd_changed_flag
=obj
->flags
&OSDFLAG_CHANGED
; // temp hack
1218 remove(obj
->old_bbox
.x1
,obj
->old_bbox
.y1
,w
,h
);
1220 // obj->flags&=~OSDFLAG_OLD_BBOX;
1226 void vo_draw_text(int dxs
,int dys
,void (*draw_alpha
)(int x0
,int y0
, int w
,int h
, unsigned char* src
, unsigned char *srca
, int stride
)){
1227 mp_osd_obj_t
* obj
=vo_osd_list
;
1228 vo_update_osd(dxs
,dys
);
1230 if(obj
->flags
&OSDFLAG_VISIBLE
){
1231 vo_osd_changed_flag
=obj
->flags
&OSDFLAG_CHANGED
; // temp hack
1234 vo_draw_spudec_sub(obj
, draw_alpha
); // FIXME
1237 case OSDTYPE_DVDNAV
:
1239 #ifdef HAVE_TV_TELETEXT
1240 case OSDTYPE_TELETEXT
:
1243 case OSDTYPE_SUBTITLE
:
1244 case OSDTYPE_PROGBAR
:
1245 vo_draw_text_from_buffer(obj
,draw_alpha
);
1248 obj
->old_bbox
=obj
->bbox
;
1249 obj
->flags
|=OSDFLAG_OLD_BBOX
;
1251 obj
->flags
&=~OSDFLAG_CHANGED
;
1256 static int vo_osd_changed_status
= 0;
1258 int vo_osd_changed(int new_value
)
1260 mp_osd_obj_t
* obj
=vo_osd_list
;
1261 int ret
= vo_osd_changed_status
;
1262 vo_osd_changed_status
= new_value
;
1265 if(obj
->type
==new_value
) obj
->flags
|=OSDFLAG_FORCE_UPDATE
;
1272 // BBBBBBBBBBBB AAAAAAAAAAAAA BBBBBBBBBBB
1273 // BBBBBBBBBBBB BBBBBBBBBBBBB
1276 // return TRUE if we have osd in the specified rectangular area:
1277 int vo_osd_check_range_update(int x1
,int y1
,int x2
,int y2
){
1278 mp_osd_obj_t
* obj
=vo_osd_list
;
1280 if(obj
->flags
&OSDFLAG_VISIBLE
){
1281 if( (obj
->bbox
.x1
<=x2
&& obj
->bbox
.x2
>=x1
) &&
1282 (obj
->bbox
.y1
<=y2
&& obj
->bbox
.y2
>=y1
) &&
1283 obj
->bbox
.y2
> obj
->bbox
.y1
&& obj
->bbox
.x2
> obj
->bbox
.x1