2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "stream/stream.h"
29 #include "stream/stream_dvdnav.h"
30 #define OSD_NAV_BOX_ALPHA 0x7f
32 #include "stream/tv.h"
33 #include "osdep/timer.h"
38 #include "video_out.h"
39 #include "font_load.h"
42 #include "libavutil/common.h"
47 // Structures needed for the new splitting algorithm.
48 // osd_text_t contains the single subtitle word.
49 // osd_text_p is used to mark the lines of subtitles
51 int osd_kerning
, //kerning with the previous word
52 osd_length
, //orizontal length inside the bbox
53 text_length
, //number of characters
55 struct osd_text_t
*prev
,
61 struct osd_text_t
*ott
;
62 struct osd_text_p
*prev
,
67 char * sub_osd_names
[]={
75 MSGTR_VO_SUB_Contrast
,
76 MSGTR_VO_SUB_Saturation
,
78 MSGTR_VO_SUB_Brightness
,
82 char * sub_osd_names_short
[] ={ "", "|>", "||", "[]", "<<" , ">>", "", "", "", "", "", "", "" };
84 //static int vo_font_loaded=-1;
85 font_desc_t
* vo_font
=NULL
;
86 font_desc_t
* sub_font
=NULL
;
88 unsigned char* vo_osd_text
=NULL
;
89 #ifdef CONFIG_TV_TELETEXT
90 void* vo_osd_teletext_page
=NULL
;
91 int vo_osd_teletext_half
= 0;
92 int vo_osd_teletext_mode
=0;
93 int vo_osd_teletext_format
=0;
94 int vo_osd_teletext_scale
=0;
100 int sub_alignment
=2; /* 0=top, 1=center, 2=bottom */
101 int sub_visibility
=1;
102 int sub_bg_color
=0; /* subtitles background color */
106 static nav_highlight_t nav_hl
;
109 // return the real height of a char:
110 static inline int get_height(int c
,int h
){
112 if ((font
=vo_font
->font
[c
])>=0)
113 if(h
<vo_font
->pic_a
[font
]->h
) h
=vo_font
->pic_a
[font
]->h
;
117 // renders char to a big per-object buffer where alpha and bitmap are separated
118 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
)
120 int dststride
= obj
->stride
;
121 int dstskip
= obj
->stride
-w
;
122 int srcskip
= stride
-w
;
124 unsigned char *b
= obj
->bitmap_buffer
+ (y0
-obj
->bbox
.y1
)*dststride
+ (x0
-obj
->bbox
.x1
);
125 unsigned char *a
= obj
->alpha_buffer
+ (y0
-obj
->bbox
.y1
)*dststride
+ (x0
-obj
->bbox
.x1
);
126 unsigned char *bs
= src
;
127 unsigned char *as
= srca
;
129 if (x0
< obj
->bbox
.x1
|| x0
+w
> obj
->bbox
.x2
|| y0
< obj
->bbox
.y1
|| y0
+h
> obj
->bbox
.y2
) {
130 fprintf(stderr
, "osd text out of range: bbox [%d %d %d %d], txt [%d %d %d %d]\n",
131 obj
->bbox
.x1
, obj
->bbox
.x2
, obj
->bbox
.y1
, obj
->bbox
.y2
,
136 for (i
= 0; i
< h
; i
++) {
137 for (j
= 0; j
< w
; j
++, b
++, a
++, bs
++, as
++) {
138 if (*b
< *bs
) *b
= *bs
;
140 if (*a
== 0 || *a
> *as
) *a
= *as
;
150 // allocates/enlarges the alpha/bitmap buffer
151 static void alloc_buf(mp_osd_obj_t
* obj
)
154 if (obj
->bbox
.x2
< obj
->bbox
.x1
) obj
->bbox
.x2
= obj
->bbox
.x1
;
155 if (obj
->bbox
.y2
< obj
->bbox
.y1
) obj
->bbox
.y2
= obj
->bbox
.y1
;
156 obj
->stride
= ((obj
->bbox
.x2
-obj
->bbox
.x1
)+7)&(~7);
157 len
= obj
->stride
*(obj
->bbox
.y2
-obj
->bbox
.y1
);
158 if (obj
->allocated
<len
) {
159 obj
->allocated
= len
;
160 free(obj
->bitmap_buffer
);
161 free(obj
->alpha_buffer
);
162 obj
->bitmap_buffer
= (unsigned char *)memalign(16, len
);
163 obj
->alpha_buffer
= (unsigned char *)memalign(16, len
);
165 memset(obj
->bitmap_buffer
, sub_bg_color
, len
);
166 memset(obj
->alpha_buffer
, sub_bg_alpha
, len
);
169 // renders the buffer
170 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
)){
171 if (obj
->allocated
> 0) {
172 draw_alpha(obj
->bbox
.x1
,obj
->bbox
.y1
,
173 obj
->bbox
.x2
-obj
->bbox
.x1
,
174 obj
->bbox
.y2
-obj
->bbox
.y1
,
181 unsigned utf8_get_char(const char **str
) {
182 const uint8_t *strp
= (const uint8_t *)*str
;
184 GET_UTF8(c
, *strp
++, goto no_utf8
;);
185 *str
= (const char *)strp
;
189 strp
= (const uint8_t *)*str
;
191 *str
= (const char *)strp
;
195 inline static void vo_update_text_osd(mp_osd_obj_t
* obj
,int dxs
,int dys
){
196 const char *cp
=vo_osd_text
;
201 obj
->bbox
.x1
=obj
->x
=x
;
202 obj
->bbox
.y1
=obj
->y
=10;
205 uint16_t c
=utf8_get_char(&cp
);
206 render_one_glyph(vo_font
, c
);
207 x
+=vo_font
->width
[c
]+vo_font
->charspace
;
211 obj
->bbox
.x2
=x
-vo_font
->charspace
;
212 obj
->bbox
.y2
=obj
->bbox
.y1
+h
;
213 obj
->flags
|=OSDFLAG_BBOX
;
220 uint16_t c
=utf8_get_char(&cp
);
221 if ((font
=vo_font
->font
[c
])>=0)
222 draw_alpha_buf(obj
,x
,obj
->y
,
224 vo_font
->pic_a
[font
]->h
,
225 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
226 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
227 vo_font
->pic_a
[font
]->w
);
228 x
+=vo_font
->width
[c
]+vo_font
->charspace
;
233 void osd_set_nav_box (uint16_t sx
, uint16_t sy
, uint16_t ex
, uint16_t ey
) {
240 inline static void vo_update_nav (mp_osd_obj_t
*obj
, int dxs
, int dys
, int left_border
, int top_border
,
241 int right_border
, int bottom_border
, int orig_w
, int orig_h
) {
243 int sx
= nav_hl
.sx
, sy
= nav_hl
.sy
;
244 int ex
= nav_hl
.ex
, ey
= nav_hl
.ey
;
245 int scaled_w
= dxs
- left_border
- right_border
;
246 int scaled_h
= dys
- top_border
- bottom_border
;
247 if (scaled_w
!= orig_w
) {
248 sx
= sx
* scaled_w
/ orig_w
;
249 ex
= ex
* scaled_w
/ orig_w
;
251 if (scaled_h
!= orig_h
) {
252 sy
= sy
* scaled_h
/ orig_h
;
253 ey
= ey
* scaled_h
/ orig_h
;
255 sx
+= left_border
; ex
+= left_border
;
256 sy
+= top_border
; ey
+= top_border
;
257 sx
= FFMIN(FFMAX(sx
, 0), dxs
);
258 ex
= FFMIN(FFMAX(ex
, 0), dxs
);
259 sy
= FFMIN(FFMAX(sy
, 0), dys
);
260 ey
= FFMIN(FFMAX(ey
, 0), dys
);
262 obj
->bbox
.x1
= obj
->x
= sx
;
263 obj
->bbox
.y1
= obj
->y
= sy
;
268 len
= obj
->stride
* (obj
->bbox
.y2
- obj
->bbox
.y1
);
269 memset (obj
->bitmap_buffer
, OSD_NAV_BOX_ALPHA
, len
);
270 memset (obj
->alpha_buffer
, OSD_NAV_BOX_ALPHA
, len
);
271 obj
->flags
|= OSDFLAG_BBOX
| OSDFLAG_CHANGED
;
272 if (obj
->bbox
.y2
> obj
->bbox
.y1
&& obj
->bbox
.x2
> obj
->bbox
.x1
)
273 obj
->flags
|= OSDFLAG_VISIBLE
;
277 #ifdef CONFIG_TV_TELETEXT
278 // renders char to a big per-object buffer where alpha and bitmap are separated
279 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
)
281 int dststride
= obj
->stride
;
282 int dstskip
= obj
->stride
-w
;
283 int srcskip
= stride
-w
;
285 unsigned char *b
= obj
->bitmap_buffer
+ (y0
-obj
->bbox
.y1
)*dststride
+ (x0
-obj
->bbox
.x1
);
286 unsigned char *a
= obj
->alpha_buffer
+ (y0
-obj
->bbox
.y1
)*dststride
+ (x0
-obj
->bbox
.x1
);
287 unsigned char *bs
= src
;
288 if (x0
< obj
->bbox
.x1
|| x0
+w
> obj
->bbox
.x2
|| y0
< obj
->bbox
.y1
|| y0
+h
> obj
->bbox
.y2
) {
289 mp_msg(MSGT_OSD
,MSGL_ERR
,"tt osd text out of range: bbox [%d %d %d %d], txt [%d %d %d %d]\n",
290 obj
->bbox
.x1
, obj
->bbox
.x2
, obj
->bbox
.y1
, obj
->bbox
.y2
,
294 for (i
= 0; i
< h
; i
++) {
295 for (j
= 0; j
< w
; j
++, b
++, a
++, bs
++) {
296 *b
=(fg
-bg
)*(*bs
)/255+bg
;
304 inline static void vo_update_text_teletext(mp_osd_obj_t
*obj
, int dxs
, int dys
)
306 int h
=0,w
=0,i
,j
,font
,flashon
;
314 int start_row
,max_rows
;
315 int b
,ax
[6],ay
[6],aw
[6],ah
[6];
317 tt_char
* tdp
=vo_osd_teletext_page
;
318 unsigned char colors
[8]={1,85,150,226,70,105,179,254};
319 unsigned char* buf
[9];
321 obj
->flags
|=OSDFLAG_CHANGED
|OSDFLAG_VISIBLE
;
322 if (!tdp
|| !vo_osd_teletext_mode
) {
323 obj
->flags
&=~OSDFLAG_VISIBLE
;
326 flashon
=(GetTimer()/1000000)%2;
327 switch(vo_osd_teletext_half
){
328 case TT_ZOOM_TOP_HALF
:
332 case TT_ZOOM_BOTTOM_HALF
:
333 start_row
=VBI_ROWS
/2;
342 for(i
=start_row
;i
<max_rows
;i
++){
343 for(j
=0;j
<VBI_COLUMNS
;j
++){
344 tc
=tdp
[i
*VBI_COLUMNS
+j
];
345 if(!tc
.ctl
&& !tc
.gfx
)
347 render_one_glyph(vo_font
, tc
.unicode
);
348 if (wm
<vo_font
->width
[tc
.unicode
])
349 wm
=vo_font
->width
[tc
.unicode
];
354 hm
=vo_font
->height
+1;
355 wm
=dxs
*hm
*max_rows
/(dys
*VBI_COLUMNS
);
357 //very simple teletext font auto scaling
358 if(!vo_osd_teletext_scale
&& hm
*(max_rows
+1)>dys
){
359 text_font_scale_factor
*=1.0*(dys
)/((max_rows
+1)*hm
);
361 vo_osd_teletext_scale
=text_font_scale_factor
;
362 obj
->flags
&=~OSDFLAG_VISIBLE
;
373 w
=cols
*wm
-vo_font
->charspace
;
374 h
=rows
*hm
-vo_font
->charspace
;
414 obj
->flags
|= OSDFLAG_BBOX
;
418 buf
[i
]=malloc(wm
*hm
);
421 if(vo_osd_teletext_format
==TT_FORMAT_OPAQUE
||vo_osd_teletext_format
==TT_FORMAT_OPAQUE_INV
)
425 memset(buf
[8],color
,wm
*hm
);
427 if(vo_osd_teletext_format
==TT_FORMAT_OPAQUE
||vo_osd_teletext_format
==TT_FORMAT_TRANSPARENT
){
429 memset(buf
[i
],(unsigned char)(1.0*(255-color
)*colors
[i
]/255),wm
*hm
);
433 memset(buf
[i
],(unsigned char)(1.0*(255-color
)*colors
[7-i
]/255),wm
*hm
);
440 tc
=tdp
[(i
+start_row
)*VBI_COLUMNS
+j
];
441 if (tc
.hidden
) { x
+=wm
; continue;}
442 if(!tc
.gfx
|| (tc
.flh
&& !flashon
)){
443 /* Rendering one text character */
444 draw_alpha_buf(obj
,x
,y
,wm
,hm
,buf
[tc
.bg
],buf
[8],wm
);
445 if(tc
.unicode
!=0x20 && tc
.unicode
!=0x00 && !tc
.ctl
&&
446 (!tc
.flh
|| flashon
) &&
447 (font
=vo_font
->font
[tc
.unicode
])>=0 && y
+hm
<dys
){
448 tt_draw_alpha_buf(obj
,x
,y
,vo_font
->width
[tc
.unicode
],vo_font
->height
,
449 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[tc
.unicode
]-vo_font
->charspace
*vo_font
->pic_a
[font
]->w
,
450 vo_font
->pic_b
[font
]->w
,
451 buf
[tc
.fg
][0],buf
[tc
.bg
][0],buf
[8][0]);
455 Rendering one graphics character
456 TODO: support for separated graphics symbols (where six rectangles does not touch each other)
460 |10| <= |34| <= 00100110 <= 0x26
464 (0:wm/2) (wm/2:wm-wm/2)
466 ********** *********** (0:hm/3)
468 *** 1 **** **** 2 ****
470 ********** ***********
471 ********** ***********
473 ********** *********** (hm/3:hm-2*hm/3)
474 ********** ***********
476 *** 3 **** **** 4 ****
478 ********** ***********
479 ********** ***********
480 ********** ***********
482 ********** *********** (hm-hm/3:hm/3)
484 *** 5 **** **** 6 ****
486 ********** ***********
487 ********** ***********
490 if(tc
.gfx
>1){ //separated gfx
492 color
=(tc
.unicode
>>b
)&1?tc
.fg
:tc
.bg
;
493 draw_alpha_buf(obj
,x
+ax
[b
]+1,y
+ay
[b
]+1,aw
[b
]-2,ah
[b
]-2,buf
[color
],buf
[8],wm
);
495 //separated gfx (background borders)
497 draw_alpha_buf(obj
,x
,y
,1,hm
,buf
[tc
.bg
],buf
[8],wm
);
498 draw_alpha_buf(obj
,x
+ax
[1]-1,y
,2,hm
,buf
[tc
.bg
],buf
[8],wm
);
499 draw_alpha_buf(obj
,x
+ax
[1]+aw
[1]-1,y
,wm
-ax
[1]-aw
[1]+1,hm
,buf
[tc
.bg
],buf
[8],wm
);
501 draw_alpha_buf(obj
,x
,y
,wm
,1,buf
[tc
.bg
],buf
[8],wm
);
502 draw_alpha_buf(obj
,x
,y
+ay
[0]+ah
[0]-1,wm
,2,buf
[tc
.bg
],buf
[8],wm
);
503 draw_alpha_buf(obj
,x
,y
+ay
[2]+ah
[2]-1,wm
,2,buf
[tc
.bg
],buf
[8],wm
);
504 draw_alpha_buf(obj
,x
,y
+ay
[4]+ah
[4]-1,wm
,hm
-ay
[4]-ah
[4]+1,buf
[tc
.bg
],buf
[8],wm
);
507 color
=(tc
.unicode
>>b
)&1?tc
.fg
:tc
.bg
;
508 draw_alpha_buf(obj
,x
+ax
[b
],y
+ay
[b
],aw
[b
],ah
[b
],buf
[color
],buf
[8],wm
);
521 int vo_osd_progbar_type
=-1;
522 int vo_osd_progbar_value
=100; // 0..256
524 // if we have n=256 bars then OSD progbar looks like below
526 // 0 1 2 3 ... 256 <= vo_osd_progbar_value
528 // [ === === === ... === ]
530 // the above schema is rescalled to n=elems bars
532 inline static void vo_update_text_progbar(mp_osd_obj_t
* obj
,int dxs
,int dys
){
534 obj
->flags
|=OSDFLAG_CHANGED
|OSDFLAG_VISIBLE
;
536 if(vo_osd_progbar_type
<0 || !vo_font
){
537 obj
->flags
&=~OSDFLAG_VISIBLE
;
541 render_one_glyph(vo_font
, OSD_PB_START
);
542 render_one_glyph(vo_font
, OSD_PB_END
);
543 render_one_glyph(vo_font
, OSD_PB_0
);
544 render_one_glyph(vo_font
, OSD_PB_1
);
545 render_one_glyph(vo_font
, vo_osd_progbar_type
);
547 // calculate bbox corners:
549 int y
=(dys
-vo_font
->height
)/2;
550 int delimw
=vo_font
->width
[OSD_PB_START
]
551 +vo_font
->width
[OSD_PB_END
]
553 int width
=(2*dxs
-3*delimw
)/3;
554 int charw
=vo_font
->width
[OSD_PB_0
]+vo_font
->charspace
;
555 int elems
=width
/charw
;
556 int x
=(dxs
-elems
*charw
-delimw
)/2;
558 h
=get_height(OSD_PB_START
,h
);
559 h
=get_height(OSD_PB_END
,h
);
560 h
=get_height(OSD_PB_0
,h
);
561 h
=get_height(OSD_PB_1
,h
);
562 if (vo_osd_progbar_type
>0 && vo_font
->font
[vo_osd_progbar_type
]>=0){
563 delta
= vo_font
->width
[vo_osd_progbar_type
]+vo_font
->spacewidth
;
564 delta
= (x
-delta
> 0) ? delta
: x
;
565 h
=get_height(vo_osd_progbar_type
,h
);
567 obj
->bbox
.x1
=obj
->x
=x
;
568 obj
->bbox
.y1
=obj
->y
=y
;
569 obj
->bbox
.x2
=x
+width
+delimw
;
570 obj
->bbox
.y2
=y
+h
; //vo_font->height;
571 obj
->flags
|=OSDFLAG_BBOX
;
572 obj
->params
.progbar
.elems
=elems
;
573 obj
->bbox
.x1
-=delta
; // space for an icon
579 int minw
= vo_font
->width
[OSD_PB_START
]+vo_font
->width
[OSD_PB_END
]+vo_font
->width
[OSD_PB_0
];
580 if (vo_osd_progbar_type
>0 && vo_font
->font
[vo_osd_progbar_type
]>=0){
581 minw
+= vo_font
->width
[vo_osd_progbar_type
]+vo_font
->charspace
+vo_font
->spacewidth
;
583 if (obj
->bbox
.x2
- obj
->bbox
.x1
< minw
) return; // space too small, don't render anything
593 int charw
=vo_font
->width
[OSD_PB_0
]+vo_font
->charspace
;
594 int elems
=obj
->params
.progbar
.elems
;
596 if (vo_osd_progbar_value
<=0)
599 int ev
=vo_osd_progbar_value
*elems
;
601 if (ev
& 0xFF) mark
++;
602 if (mark
>elems
) mark
=elems
;
606 // printf("osd.progbar width=%d xpos=%d\n",width,x);
608 c
=vo_osd_progbar_type
;
609 if(vo_osd_progbar_type
>0 && (font
=vo_font
->font
[c
])>=0) {
610 int xp
=x
-vo_font
->width
[c
]-vo_font
->spacewidth
;
611 draw_alpha_buf(obj
,(xp
<0?0:xp
),y
,
613 vo_font
->pic_a
[font
]->h
,
614 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
615 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
616 vo_font
->pic_a
[font
]->w
);
620 if ((font
=vo_font
->font
[c
])>=0)
621 draw_alpha_buf(obj
,x
,y
,
623 vo_font
->pic_a
[font
]->h
,
624 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
625 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
626 vo_font
->pic_a
[font
]->w
);
627 x
+=vo_font
->width
[c
]+vo_font
->charspace
;
630 if ((font
=vo_font
->font
[c
])>=0){
632 h
=vo_font
->pic_a
[font
]->h
;
633 s
=vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
];
634 sa
=vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
];
635 st
=vo_font
->pic_a
[font
]->w
;
637 draw_alpha_buf(obj
,x
,y
,w
,h
,s
,sa
,st
);
643 if ((font
=vo_font
->font
[c
])>=0){
645 h
=vo_font
->pic_a
[font
]->h
;
646 s
=vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
];
647 sa
=vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
];
648 st
=vo_font
->pic_a
[font
]->w
;
649 if ((i
=elems
-mark
)) do {
650 draw_alpha_buf(obj
,x
,y
,w
,h
,s
,sa
,st
);
656 if ((font
=vo_font
->font
[c
])>=0)
657 draw_alpha_buf(obj
,x
,y
,
659 vo_font
->pic_a
[font
]->h
,
660 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
661 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
662 vo_font
->pic_a
[font
]->w
);
663 // x+=vo_font->width[c]+vo_font->charspace;
666 // vo_osd_progbar_value=(vo_osd_progbar_value+1)&0xFF;
670 subtitle
* vo_sub
=NULL
;
672 inline static void vo_update_text_sub(mp_osd_obj_t
* obj
,int dxs
,int dys
){
674 int c
,i
,j
,l
,x
,y
,font
,prevc
,counter
;
676 int lastStripPosition
;
682 obj
->flags
|=OSDFLAG_CHANGED
|OSDFLAG_VISIBLE
;
684 if(!vo_sub
|| !sub_font
|| !sub_visibility
|| (sub_font
->font
[40]<0)){
685 obj
->flags
&=~OSDFLAG_VISIBLE
;
689 obj
->bbox
.y2
=obj
->y
=dys
;
690 obj
->params
.subtitle
.lines
=0;
692 // too long lines divide into a smaller ones
695 lastStripPosition
=-1;
699 struct osd_text_t
*osl
, *cp_ott
, *tmp_ott
, *tmp
;
700 struct osd_text_p
*otp_sub
= NULL
, *otp_sub_tmp
, // these are used to store the whole sub text osd
701 *otp
, *tmp_otp
, *pmt
; // these are used to manage sub text osd coming from a single sub line
702 int *char_seq
, char_position
, xlimit
= dxs
* sub_width_p
/ 100, counter
;
705 xsize
= -sub_font
->charspace
;
709 char_seq
= calloc(strlen(t
), sizeof(int));
717 // reading the subtitle words from vo_sub->text[]
720 c
= utf8_get_char(&t
);
721 else if ((c
= *t
++) >= 0x80 && sub_unicode
)
724 t
+= strlen(t
); // end here
725 mp_msg(MSGT_OSD
,MSGL_WARN
,"\nMAX_UCS exceeded!\n");
727 if (!c
) c
++; // avoid UCS 0
728 render_one_glyph(sub_font
, c
);
731 struct osd_text_t
*tmp_ott
= (struct osd_text_t
*) calloc(1, sizeof(struct osd_text_t
));
734 osl
= cp_ott
= tmp_ott
;
736 tmp_ott
->prev
= cp_ott
;
737 cp_ott
->next
= tmp_ott
;
738 tmp_ott
->osd_kerning
=
739 sub_font
->charspace
+ sub_font
->width
[' '];
742 tmp_ott
->osd_length
= xsize
;
743 tmp_ott
->text_length
= char_position
;
744 tmp_ott
->text
= (int *) malloc(char_position
* sizeof(int));
745 for (counter
= 0; counter
< char_position
; ++counter
)
746 tmp_ott
->text
[counter
] = char_seq
[counter
];
751 int delta_xsize
= sub_font
->width
[c
] + sub_font
->charspace
+ kerning(sub_font
, prevc
, c
);
753 if (xsize
+ delta_xsize
<= dxs
) {
756 char_seq
[char_position
++] = c
;
757 xsize
+= delta_xsize
;
758 if ((!suboverlap_enabled
) && ((font
= sub_font
->font
[c
]) >= 0)) {
759 if (sub_font
->pic_a
[font
]->h
> h
) {
760 h
= sub_font
->pic_a
[font
]->h
;
765 mp_msg(MSGT_OSD
, MSGL_WARN
, "\nSubtitle word '%s' too long!\n", t
);
770 }// for len (all words from subtitle line read)
772 // osl holds an ordered (as they appear in the lines) chain of the subtitle words
774 struct osd_text_t
*tmp_ott
= (struct osd_text_t
*) calloc(1, sizeof(struct osd_text_t
));
777 osl
= cp_ott
= tmp_ott
;
779 tmp_ott
->prev
= cp_ott
;
780 cp_ott
->next
= tmp_ott
;
781 tmp_ott
->osd_kerning
=
782 sub_font
->charspace
+ sub_font
->width
[' '];
785 tmp_ott
->osd_length
= xsize
;
786 tmp_ott
->text_length
= char_position
;
787 tmp_ott
->text
= (int *) malloc(char_position
* sizeof(int));
788 for (counter
= 0; counter
< char_position
; ++counter
)
789 tmp_ott
->text
[counter
] = char_seq
[counter
];
791 xsize
= -sub_font
->charspace
;
796 int value
= 0, exit
= 0, minimum
= 0;
798 // otp will contain the chain of the osd subtitle lines coming from the single vo_sub line.
799 otp
= tmp_otp
= (struct osd_text_p
*) calloc(1, sizeof(struct osd_text_p
));
801 for (tmp_ott
= tmp_otp
->ott
; exit
== 0; ) {
803 value
+= tmp_ott
->osd_kerning
+ tmp_ott
->osd_length
;
804 tmp_ott
= tmp_ott
->next
;
805 } while ((tmp_ott
!= NULL
) && (value
+ tmp_ott
->osd_kerning
+ tmp_ott
->osd_length
<= xlimit
));
806 if (tmp_ott
!= NULL
) {
807 struct osd_text_p
*tmp
= (struct osd_text_p
*) calloc(1, sizeof(struct osd_text_p
));
809 tmp_otp
->value
= value
;
813 tmp_otp
->ott
= tmp_ott
;
814 value
= -2 * sub_font
->charspace
- sub_font
->width
[' '];
816 tmp_otp
->value
= value
;
823 // minimum holds the 'sum of the differences in length among the lines',
824 // a measure of the evenness of the lengths of the lines
825 for (tmp_otp
= otp
; tmp_otp
->next
!= NULL
; tmp_otp
= tmp_otp
->next
) {
827 while (pmt
!= NULL
) {
828 minimum
+= abs(tmp_otp
->value
- pmt
->value
);
833 if (otp
->next
!= NULL
) {
835 struct osd_text_p
*mem
, *hold
;
838 // until the last word of a line can be moved to the beginning of following line
839 // reducing the 'sum of the differences in length among the lines', it is done
843 for (tmp_otp
= otp
; tmp_otp
->next
!= NULL
; tmp_otp
= tmp_otp
->next
) {
845 for (tmp
= tmp_otp
->ott
; tmp
->next
!= pmt
->ott
; tmp
= tmp
->next
);
846 if (pmt
->value
+ tmp
->osd_length
+ pmt
->ott
->osd_kerning
<= xlimit
) {
847 mem1
= tmp_otp
->value
;
849 tmp_otp
->value
= mem1
- tmp
->osd_length
- tmp
->osd_kerning
;
850 pmt
->value
= mem2
+ tmp
->osd_length
+ pmt
->ott
->osd_kerning
;
853 for (mem
= otp
; mem
->next
!= NULL
; mem
= mem
->next
) {
855 while (pmt
!= NULL
) {
856 value
+= abs(mem
->value
- pmt
->value
);
860 if (value
< minimum
) {
865 tmp_otp
->value
= mem1
;
866 tmp_otp
->next
->value
= mem2
;
873 for (tmp
= tmp_otp
->ott
; tmp
->next
!= pmt
->ott
; tmp
= tmp
->next
);
874 mem1
= tmp_otp
->value
;
876 tmp_otp
->value
= mem1
- tmp
->osd_length
- tmp
->osd_kerning
;
877 pmt
->value
= mem2
+ tmp
->osd_length
+ pmt
->ott
->osd_kerning
;
881 }//~if(otp->next!=NULL)
884 // adding otp (containing splitted lines) to otp chain
885 if (otp_sub
== NULL
) {
887 for (otp_sub_tmp
= otp_sub
; otp_sub_tmp
->next
!= NULL
; otp_sub_tmp
= otp_sub_tmp
->next
);
891 while (tmp
->next
!= NULL
) tmp
= tmp
->next
;
892 tmp
->next
= otp
->ott
;
893 otp
->ott
->prev
= tmp
;
894 //attaching new subtitle line at the end
895 otp_sub_tmp
->next
= otp
;
896 otp
->prev
= otp_sub_tmp
;
898 otp_sub_tmp
= otp_sub_tmp
->next
;
899 while (otp_sub_tmp
->next
!= NULL
);
904 // write lines into utbl
908 obj
->params
.subtitle
.lines
= 0;
909 for (tmp_otp
= otp_sub
; tmp_otp
!= NULL
; tmp_otp
= tmp_otp
->next
) {
911 if ((obj
->params
.subtitle
.lines
++) >= MAX_UCSLINES
)
914 if (h
> obj
->y
) { // out of the screen so end parsing
915 obj
->y
-= lasth
- sub_font
->height
; // correct the y position
918 xsize
= tmp_otp
->value
;
919 obj
->params
.subtitle
.xtbl
[xtblc
++] = (dxs
- xsize
) / 2;
920 if (xmin
> (dxs
- xsize
) / 2)
921 xmin
= (dxs
- xsize
) / 2;
922 if (xmax
< (dxs
+ xsize
) / 2)
923 xmax
= (dxs
+ xsize
) / 2;
925 tmp
= (tmp_otp
->next
== NULL
) ? NULL
: tmp_otp
->next
->ott
;
926 for (tmp_ott
= tmp_otp
->ott
; tmp_ott
!= tmp
; tmp_ott
= tmp_ott
->next
) {
927 for (counter
= 0; counter
< tmp_ott
->text_length
; ++counter
) {
928 if (utblc
> MAX_UCS
) {
931 c
= tmp_ott
->text
[counter
];
932 render_one_glyph(sub_font
, c
);
933 obj
->params
.subtitle
.utbl
[utblc
++] = c
;
936 obj
->params
.subtitle
.utbl
[utblc
++] = ' ';
938 obj
->params
.subtitle
.utbl
[utblc
- 1] = 0;
939 obj
->y
-= sub_font
->height
;
941 if(obj
->params
.subtitle
.lines
)
942 obj
->y
= dys
- ((obj
->params
.subtitle
.lines
- 1) * sub_font
->height
+ sub_font
->pic_a
[sub_font
->font
[40]]->h
);
945 if (otp_sub
!= NULL
) {
946 for (tmp
= otp_sub
->ott
; tmp
->next
!= NULL
; free(tmp
->prev
)) {
953 for(pmt
= otp_sub
; pmt
->next
!= NULL
; free(pmt
->prev
)) {
960 /// vertical alignment
962 if (sub_alignment
== 2)
963 obj
->y
= dys
* sub_pos
/ 100 - h
;
964 else if (sub_alignment
== 1)
965 obj
->y
= dys
* sub_pos
/ 100 - h
/ 2;
967 obj
->y
= dys
* sub_pos
/ 100;
971 if (obj
->y
> dys
- h
)
974 obj
->bbox
.y2
= obj
->y
+ h
;
977 if (sub_justify
) xmin
= 10;
981 // obj->bbox.y2=obj->y+obj->params.subtitle.lines*sub_font->height;
982 obj
->flags
|=OSDFLAG_BBOX
;
989 switch(vo_sub
->alignment
) {
990 case SUB_ALIGNMENT_BOTTOMLEFT
:
991 case SUB_ALIGNMENT_MIDDLELEFT
:
992 case SUB_ALIGNMENT_TOPLEFT
:
993 obj
->alignment
|= 0x1;
995 case SUB_ALIGNMENT_BOTTOMRIGHT
:
996 case SUB_ALIGNMENT_MIDDLERIGHT
:
997 case SUB_ALIGNMENT_TOPRIGHT
:
998 obj
->alignment
|= 0x2;
1000 case SUB_ALIGNMENT_BOTTOMCENTER
:
1001 case SUB_ALIGNMENT_MIDDLECENTER
:
1002 case SUB_ALIGNMENT_TOPCENTER
:
1004 obj
->alignment
|= 0x0;
1008 if ((l
= obj
->params
.subtitle
.lines
)) {
1009 for(counter
= dxs
; i
< l
; ++i
)
1010 if (obj
->params
.subtitle
.xtbl
[i
] < counter
) counter
= obj
->params
.subtitle
.xtbl
[i
];
1011 for (i
= 0; i
< l
; ++i
) {
1012 switch (obj
->alignment
&0x3) {
1019 x
= 2 * obj
->params
.subtitle
.xtbl
[i
] - counter
- ((obj
->params
.subtitle
.xtbl
[i
] == counter
) ? 0 : 1);
1023 x
= obj
->params
.subtitle
.xtbl
[i
];
1026 while ((c
=obj
->params
.subtitle
.utbl
[j
++])){
1027 x
+= kerning(sub_font
,prevc
,c
);
1028 if ((font
=sub_font
->font
[c
])>=0)
1029 draw_alpha_buf(obj
,x
,y
,
1031 sub_font
->pic_a
[font
]->h
+y
<obj
->dys
? sub_font
->pic_a
[font
]->h
: obj
->dys
-y
,
1032 sub_font
->pic_b
[font
]->bmp
+sub_font
->start
[c
],
1033 sub_font
->pic_a
[font
]->bmp
+sub_font
->start
[c
],
1034 sub_font
->pic_a
[font
]->w
);
1035 x
+=sub_font
->width
[c
]+sub_font
->charspace
;
1038 y
+=sub_font
->height
;
1044 inline static void vo_update_spudec_sub(mp_osd_obj_t
* obj
, int dxs
, int dys
)
1046 unsigned int bbox
[4];
1047 spudec_calc_bbox(vo_spudec
, dxs
, dys
, bbox
);
1048 obj
->bbox
.x1
= bbox
[0];
1049 obj
->bbox
.x2
= bbox
[1];
1050 obj
->bbox
.y1
= bbox
[2];
1051 obj
->bbox
.y2
= bbox
[3];
1052 obj
->flags
|= OSDFLAG_BBOX
;
1055 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
))
1057 spudec_draw_scaled(vo_spudec
, obj
->dxs
, obj
->dys
, draw_alpha
);
1060 void *vo_spudec
=NULL
;
1061 void *vo_vobsub
=NULL
;
1063 static int draw_alpha_init_flag
=0;
1065 void vo_draw_alpha_init(void);
1067 mp_osd_obj_t
* vo_osd_list
=NULL
;
1069 static mp_osd_obj_t
* new_osd_obj(int type
){
1070 mp_osd_obj_t
* osd
=malloc(sizeof(mp_osd_obj_t
));
1071 memset(osd
,0,sizeof(mp_osd_obj_t
));
1072 osd
->next
=vo_osd_list
;
1075 osd
->alpha_buffer
= NULL
;
1076 osd
->bitmap_buffer
= NULL
;
1077 osd
->allocated
= -1;
1081 void free_osd_list(void){
1082 mp_osd_obj_t
* obj
=vo_osd_list
;
1084 mp_osd_obj_t
* next
=obj
->next
;
1085 if (obj
->alpha_buffer
) free(obj
->alpha_buffer
);
1086 if (obj
->bitmap_buffer
) free(obj
->bitmap_buffer
);
1093 #define FONT_LOAD_DEFER 6
1095 int vo_update_osd_ext(int dxs
,int dys
, int left_border
, int top_border
,
1096 int right_border
, int bottom_border
, int orig_w
, int orig_h
){
1097 mp_osd_obj_t
* obj
=vo_osd_list
;
1099 #ifdef CONFIG_FREETYPE
1100 static int defer_counter
= 0, prev_dxs
= 0, prev_dys
= 0;
1103 #ifdef CONFIG_FREETYPE
1104 // here is the right place to get screen dimensions
1105 if (((dxs
!= vo_image_width
)
1106 && (subtitle_autoscale
== 2 || subtitle_autoscale
== 3))
1107 || ((dys
!= vo_image_height
)
1108 && (subtitle_autoscale
== 1 || subtitle_autoscale
== 3)))
1110 // screen dimensions changed
1111 // wait a while to avoid useless reloading of the font
1112 if (dxs
== prev_dxs
|| dys
== prev_dys
) {
1119 if (defer_counter
>= FONT_LOAD_DEFER
) force_load_font
= 1;
1122 if (force_load_font
) {
1123 force_load_font
= 0;
1124 load_font_ft(dxs
, dys
, &vo_font
, font_name
, osd_font_scale_factor
);
1126 load_font_ft(dxs
, dys
, &sub_font
, sub_font_name
, text_font_scale_factor
);
1128 load_font_ft(dxs
, dys
, &sub_font
, font_name
, text_font_scale_factor
);
1134 load_font_ft(dxs
, dys
, &vo_font
, font_name
, osd_font_scale_factor
);
1137 load_font_ft(dxs
, dys
, &sub_font
, sub_font_name
, text_font_scale_factor
);
1139 load_font_ft(dxs
, dys
, &sub_font
, font_name
, text_font_scale_factor
);
1145 if(dxs
!=obj
->dxs
|| dys
!=obj
->dys
|| obj
->flags
&OSDFLAG_FORCE_UPDATE
){
1146 int vis
=obj
->flags
&OSDFLAG_VISIBLE
;
1147 obj
->flags
&=~OSDFLAG_BBOX
;
1149 #ifdef CONFIG_DVDNAV
1150 case OSDTYPE_DVDNAV
:
1151 vo_update_nav(obj
,dxs
,dys
, left_border
, top_border
, right_border
, bottom_border
, orig_w
, orig_h
);
1154 case OSDTYPE_SUBTITLE
:
1155 vo_update_text_sub(obj
,dxs
,dys
);
1157 #ifdef CONFIG_TV_TELETEXT
1158 case OSDTYPE_TELETEXT
:
1159 vo_update_text_teletext(obj
,dxs
,dys
);
1162 case OSDTYPE_PROGBAR
:
1163 vo_update_text_progbar(obj
,dxs
,dys
);
1166 if(sub_visibility
&& vo_spudec
&& spudec_visible(vo_spudec
)){
1167 vo_update_spudec_sub(obj
, dxs
, dys
);
1168 obj
->flags
|=OSDFLAG_VISIBLE
|OSDFLAG_CHANGED
;
1171 obj
->flags
&=~OSDFLAG_VISIBLE
;
1174 if(vo_font
&& vo_osd_text
&& vo_osd_text
[0]){
1175 vo_update_text_osd(obj
,dxs
,dys
); // update bbox
1176 obj
->flags
|=OSDFLAG_VISIBLE
|OSDFLAG_CHANGED
;
1178 obj
->flags
&=~OSDFLAG_VISIBLE
;
1182 if(!(obj
->flags
&OSDFLAG_BBOX
)){
1183 // we don't know, so assume the whole screen changed :(
1184 obj
->bbox
.x1
=obj
->bbox
.y1
=0;
1187 obj
->flags
|=OSDFLAG_BBOX
;
1189 // check bbox, reduce it if it's out of bounds (corners):
1190 if(obj
->bbox
.x1
<0) obj
->bbox
.x1
=0;
1191 if(obj
->bbox
.y1
<0) obj
->bbox
.y1
=0;
1192 if(obj
->bbox
.x2
>dxs
) obj
->bbox
.x2
=dxs
;
1193 if(obj
->bbox
.y2
>dys
) obj
->bbox
.y2
=dys
;
1194 if(obj
->flags
&OSDFLAG_VISIBLE
)
1196 mp_msg(MSGT_OSD
,MSGL_DBG2
,"OSD update: %d;%d %dx%d \n",
1197 obj
->bbox
.x1
,obj
->bbox
.y1
,obj
->bbox
.x2
-obj
->bbox
.x1
,
1198 obj
->bbox
.y2
-obj
->bbox
.y1
);
1200 // check if visibility changed:
1201 if(vis
!= (obj
->flags
&OSDFLAG_VISIBLE
) ) obj
->flags
|=OSDFLAG_CHANGED
;
1202 // remove the cause of automatic update:
1203 obj
->dxs
=dxs
; obj
->dys
=dys
;
1204 obj
->flags
&=~OSDFLAG_FORCE_UPDATE
;
1206 if(obj
->flags
&OSDFLAG_CHANGED
){
1208 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
);
1215 int vo_update_osd(int dxs
, int dys
) {
1216 return vo_update_osd_ext(dxs
, dys
, 0, 0, 0, 0, dxs
, dys
);
1219 void vo_init_osd(void){
1220 if(!draw_alpha_init_flag
){
1221 draw_alpha_init_flag
=1;
1222 vo_draw_alpha_init();
1224 if(vo_osd_list
) free_osd_list();
1225 // temp hack, should be moved to mplayer/mencoder later
1226 new_osd_obj(OSDTYPE_OSD
);
1227 new_osd_obj(OSDTYPE_SUBTITLE
);
1228 new_osd_obj(OSDTYPE_PROGBAR
);
1229 new_osd_obj(OSDTYPE_SPU
);
1230 #ifdef CONFIG_DVDNAV
1231 new_osd_obj(OSDTYPE_DVDNAV
);
1233 #ifdef CONFIG_TV_TELETEXT
1234 new_osd_obj(OSDTYPE_TELETEXT
);
1236 #ifdef CONFIG_FREETYPE
1237 force_load_font
= 1;
1241 int vo_osd_changed_flag
=0;
1243 void vo_remove_text(int dxs
,int dys
,void (*remove
)(int x0
,int y0
, int w
,int h
)){
1244 mp_osd_obj_t
* obj
=vo_osd_list
;
1245 vo_update_osd(dxs
,dys
);
1247 if(((obj
->flags
&OSDFLAG_CHANGED
) || (obj
->flags
&OSDFLAG_VISIBLE
)) &&
1248 (obj
->flags
&OSDFLAG_OLD_BBOX
)){
1249 int w
=obj
->old_bbox
.x2
-obj
->old_bbox
.x1
;
1250 int h
=obj
->old_bbox
.y2
-obj
->old_bbox
.y1
;
1252 vo_osd_changed_flag
=obj
->flags
&OSDFLAG_CHANGED
; // temp hack
1253 remove(obj
->old_bbox
.x1
,obj
->old_bbox
.y1
,w
,h
);
1255 // obj->flags&=~OSDFLAG_OLD_BBOX;
1261 void vo_draw_text_ext(int dxs
, int dys
, int left_border
, int top_border
,
1262 int right_border
, int bottom_border
, int orig_w
, int orig_h
,
1263 void (*draw_alpha
)(int x0
, int y0
, int w
,int h
, unsigned char* src
, unsigned char *srca
, int stride
)) {
1264 mp_osd_obj_t
* obj
=vo_osd_list
;
1265 vo_update_osd_ext(dxs
, dys
, left_border
, top_border
, right_border
, bottom_border
, orig_w
, orig_h
);
1267 if(obj
->flags
&OSDFLAG_VISIBLE
){
1268 vo_osd_changed_flag
=obj
->flags
&OSDFLAG_CHANGED
; // temp hack
1271 vo_draw_spudec_sub(obj
, draw_alpha
); // FIXME
1273 #ifdef CONFIG_DVDNAV
1274 case OSDTYPE_DVDNAV
:
1276 #ifdef CONFIG_TV_TELETEXT
1277 case OSDTYPE_TELETEXT
:
1280 case OSDTYPE_SUBTITLE
:
1281 case OSDTYPE_PROGBAR
:
1282 vo_draw_text_from_buffer(obj
,draw_alpha
);
1285 obj
->old_bbox
=obj
->bbox
;
1286 obj
->flags
|=OSDFLAG_OLD_BBOX
;
1288 obj
->flags
&=~OSDFLAG_CHANGED
;
1293 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
)) {
1294 vo_draw_text_ext(dxs
, dys
, 0, 0, 0, 0, dxs
, dys
, draw_alpha
);
1297 static int vo_osd_changed_status
= 0;
1299 int vo_osd_changed(int new_value
)
1301 mp_osd_obj_t
* obj
=vo_osd_list
;
1302 int ret
= vo_osd_changed_status
;
1303 vo_osd_changed_status
= new_value
;
1306 if(obj
->type
==new_value
) obj
->flags
|=OSDFLAG_FORCE_UPDATE
;
1313 // BBBBBBBBBBBB AAAAAAAAAAAAA BBBBBBBBBBB
1314 // BBBBBBBBBBBB BBBBBBBBBBBBB
1317 // return TRUE if we have osd in the specified rectangular area:
1318 int vo_osd_check_range_update(int x1
,int y1
,int x2
,int y2
){
1319 mp_osd_obj_t
* obj
=vo_osd_list
;
1321 if(obj
->flags
&OSDFLAG_VISIBLE
){
1322 if( (obj
->bbox
.x1
<=x2
&& obj
->bbox
.x2
>=x1
) &&
1323 (obj
->bbox
.y1
<=y2
&& obj
->bbox
.y2
>=y1
) &&
1324 obj
->bbox
.y2
> obj
->bbox
.y1
&& obj
->bbox
.x2
> obj
->bbox
.x1