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 "libmpcodecs/dec_teletext.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 void* vo_osd_teletext_page
=NULL
;
90 int vo_osd_teletext_half
= 0;
91 int vo_osd_teletext_mode
=0;
92 int vo_osd_teletext_format
=0;
93 int vo_osd_teletext_scale
=0;
98 int sub_alignment
=2; /* 0=top, 1=center, 2=bottom */
100 int sub_bg_color
=0; /* subtitles background color */
104 static nav_highlight_t nav_hl
;
107 // return the real height of a char:
108 static inline int get_height(int c
,int h
){
110 if ((font
=vo_font
->font
[c
])>=0)
111 if(h
<vo_font
->pic_a
[font
]->h
) h
=vo_font
->pic_a
[font
]->h
;
115 // renders char to a big per-object buffer where alpha and bitmap are separated
116 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
)
118 int dststride
= obj
->stride
;
119 int dstskip
= obj
->stride
-w
;
120 int srcskip
= stride
-w
;
122 unsigned char *b
= obj
->bitmap_buffer
+ (y0
-obj
->bbox
.y1
)*dststride
+ (x0
-obj
->bbox
.x1
);
123 unsigned char *a
= obj
->alpha_buffer
+ (y0
-obj
->bbox
.y1
)*dststride
+ (x0
-obj
->bbox
.x1
);
124 unsigned char *bs
= src
;
125 unsigned char *as
= srca
;
127 if (x0
< obj
->bbox
.x1
|| x0
+w
> obj
->bbox
.x2
|| y0
< obj
->bbox
.y1
|| y0
+h
> obj
->bbox
.y2
) {
128 fprintf(stderr
, "osd text out of range: bbox [%d %d %d %d], txt [%d %d %d %d]\n",
129 obj
->bbox
.x1
, obj
->bbox
.x2
, obj
->bbox
.y1
, obj
->bbox
.y2
,
134 for (i
= 0; i
< h
; i
++) {
135 for (j
= 0; j
< w
; j
++, b
++, a
++, bs
++, as
++) {
136 if (*b
< *bs
) *b
= *bs
;
138 if (*a
== 0 || *a
> *as
) *a
= *as
;
148 // allocates/enlarges the alpha/bitmap buffer
149 static void alloc_buf(mp_osd_obj_t
* obj
)
152 if (obj
->bbox
.x2
< obj
->bbox
.x1
) obj
->bbox
.x2
= obj
->bbox
.x1
;
153 if (obj
->bbox
.y2
< obj
->bbox
.y1
) obj
->bbox
.y2
= obj
->bbox
.y1
;
154 obj
->stride
= ((obj
->bbox
.x2
-obj
->bbox
.x1
)+7)&(~7);
155 len
= obj
->stride
*(obj
->bbox
.y2
-obj
->bbox
.y1
);
156 if (obj
->allocated
<len
) {
157 obj
->allocated
= len
;
158 free(obj
->bitmap_buffer
);
159 free(obj
->alpha_buffer
);
160 obj
->bitmap_buffer
= (unsigned char *)memalign(16, len
);
161 obj
->alpha_buffer
= (unsigned char *)memalign(16, len
);
163 memset(obj
->bitmap_buffer
, sub_bg_color
, len
);
164 memset(obj
->alpha_buffer
, sub_bg_alpha
, len
);
167 // renders the buffer
168 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
)){
169 if (obj
->allocated
> 0) {
170 draw_alpha(obj
->bbox
.x1
,obj
->bbox
.y1
,
171 obj
->bbox
.x2
-obj
->bbox
.x1
,
172 obj
->bbox
.y2
-obj
->bbox
.y1
,
179 unsigned utf8_get_char(const char **str
) {
180 const uint8_t *strp
= (const uint8_t *)*str
;
182 GET_UTF8(c
, *strp
++, goto no_utf8
;);
183 *str
= (const char *)strp
;
187 strp
= (const uint8_t *)*str
;
189 *str
= (const char *)strp
;
193 inline static void vo_update_text_osd(mp_osd_obj_t
* obj
,int dxs
,int dys
){
194 const char *cp
=vo_osd_text
;
199 obj
->bbox
.x1
=obj
->x
=x
;
200 obj
->bbox
.y1
=obj
->y
=10;
203 uint16_t c
=utf8_get_char(&cp
);
204 render_one_glyph(vo_font
, c
);
205 x
+=vo_font
->width
[c
]+vo_font
->charspace
;
209 obj
->bbox
.x2
=x
-vo_font
->charspace
;
210 obj
->bbox
.y2
=obj
->bbox
.y1
+h
;
211 obj
->flags
|=OSDFLAG_BBOX
;
218 uint16_t c
=utf8_get_char(&cp
);
219 if ((font
=vo_font
->font
[c
])>=0)
220 draw_alpha_buf(obj
,x
,obj
->y
,
222 vo_font
->pic_a
[font
]->h
,
223 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
224 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
225 vo_font
->pic_a
[font
]->w
);
226 x
+=vo_font
->width
[c
]+vo_font
->charspace
;
231 void osd_set_nav_box (uint16_t sx
, uint16_t sy
, uint16_t ex
, uint16_t ey
) {
238 inline static void vo_update_nav (mp_osd_obj_t
*obj
, int dxs
, int dys
, int left_border
, int top_border
,
239 int right_border
, int bottom_border
, int orig_w
, int orig_h
) {
241 int sx
= nav_hl
.sx
, sy
= nav_hl
.sy
;
242 int ex
= nav_hl
.ex
, ey
= nav_hl
.ey
;
243 int scaled_w
= dxs
- left_border
- right_border
;
244 int scaled_h
= dys
- top_border
- bottom_border
;
245 if (scaled_w
!= orig_w
) {
246 sx
= sx
* scaled_w
/ orig_w
;
247 ex
= ex
* scaled_w
/ orig_w
;
249 if (scaled_h
!= orig_h
) {
250 sy
= sy
* scaled_h
/ orig_h
;
251 ey
= ey
* scaled_h
/ orig_h
;
253 sx
+= left_border
; ex
+= left_border
;
254 sy
+= top_border
; ey
+= top_border
;
255 sx
= FFMIN(FFMAX(sx
, 0), dxs
);
256 ex
= FFMIN(FFMAX(ex
, 0), dxs
);
257 sy
= FFMIN(FFMAX(sy
, 0), dys
);
258 ey
= FFMIN(FFMAX(ey
, 0), dys
);
260 obj
->bbox
.x1
= obj
->x
= sx
;
261 obj
->bbox
.y1
= obj
->y
= sy
;
266 len
= obj
->stride
* (obj
->bbox
.y2
- obj
->bbox
.y1
);
267 memset (obj
->bitmap_buffer
, OSD_NAV_BOX_ALPHA
, len
);
268 memset (obj
->alpha_buffer
, OSD_NAV_BOX_ALPHA
, len
);
269 obj
->flags
|= OSDFLAG_BBOX
| OSDFLAG_CHANGED
;
270 if (obj
->bbox
.y2
> obj
->bbox
.y1
&& obj
->bbox
.x2
> obj
->bbox
.x1
)
271 obj
->flags
|= OSDFLAG_VISIBLE
;
275 // renders char to a big per-object buffer where alpha and bitmap are separated
276 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
)
278 int dststride
= obj
->stride
;
279 int dstskip
= obj
->stride
-w
;
280 int srcskip
= stride
-w
;
282 unsigned char *b
= obj
->bitmap_buffer
+ (y0
-obj
->bbox
.y1
)*dststride
+ (x0
-obj
->bbox
.x1
);
283 unsigned char *a
= obj
->alpha_buffer
+ (y0
-obj
->bbox
.y1
)*dststride
+ (x0
-obj
->bbox
.x1
);
284 unsigned char *bs
= src
;
285 if (x0
< obj
->bbox
.x1
|| x0
+w
> obj
->bbox
.x2
|| y0
< obj
->bbox
.y1
|| y0
+h
> obj
->bbox
.y2
) {
286 mp_msg(MSGT_OSD
,MSGL_ERR
,"tt osd text out of range: bbox [%d %d %d %d], txt [%d %d %d %d]\n",
287 obj
->bbox
.x1
, obj
->bbox
.x2
, obj
->bbox
.y1
, obj
->bbox
.y2
,
291 for (i
= 0; i
< h
; i
++) {
292 for (j
= 0; j
< w
; j
++, b
++, a
++, bs
++) {
293 *b
=(fg
-bg
)*(*bs
)/255+bg
;
301 inline static void vo_update_text_teletext(mp_osd_obj_t
*obj
, int dxs
, int dys
)
303 int h
=0,w
=0,i
,j
,font
,flashon
;
311 int start_row
,max_rows
;
312 int b
,ax
[6],ay
[6],aw
[6],ah
[6];
314 tt_char
* tdp
=vo_osd_teletext_page
;
315 static const uint8_t colors
[8]={1,85,150,226,70,105,179,254};
316 unsigned char* buf
[9];
318 obj
->flags
|=OSDFLAG_CHANGED
|OSDFLAG_VISIBLE
;
319 if (!tdp
|| !vo_osd_teletext_mode
) {
320 obj
->flags
&=~OSDFLAG_VISIBLE
;
323 flashon
=(GetTimer()/1000000)%2;
324 switch(vo_osd_teletext_half
){
325 case TT_ZOOM_TOP_HALF
:
329 case TT_ZOOM_BOTTOM_HALF
:
330 start_row
=VBI_ROWS
/2;
339 for(i
=start_row
;i
<max_rows
;i
++){
340 for(j
=0;j
<VBI_COLUMNS
;j
++){
341 tc
=tdp
[i
*VBI_COLUMNS
+j
];
342 if(!tc
.ctl
&& !tc
.gfx
)
344 render_one_glyph(vo_font
, tc
.unicode
);
345 if (wm
<vo_font
->width
[tc
.unicode
])
346 wm
=vo_font
->width
[tc
.unicode
];
351 hm
=vo_font
->height
+1;
352 wm
=dxs
*hm
*max_rows
/(dys
*VBI_COLUMNS
);
354 #ifdef CONFIG_FREETYPE
355 //very simple teletext font auto scaling
356 if(!vo_osd_teletext_scale
&& hm
*(max_rows
+1)>dys
){
357 osd_font_scale_factor
*=1.0*(dys
)/((max_rows
+1)*hm
);
359 vo_osd_teletext_scale
=osd_font_scale_factor
;
360 obj
->flags
&=~OSDFLAG_VISIBLE
;
372 w
=cols
*wm
-vo_font
->charspace
;
373 h
=rows
*hm
-vo_font
->charspace
;
413 obj
->flags
|= OSDFLAG_BBOX
;
417 buf
[i
]=malloc(wm
*hm
);
420 if(vo_osd_teletext_format
==TT_FORMAT_OPAQUE
||vo_osd_teletext_format
==TT_FORMAT_OPAQUE_INV
)
424 memset(buf
[8],color
,wm
*hm
);
426 if(vo_osd_teletext_format
==TT_FORMAT_OPAQUE
||vo_osd_teletext_format
==TT_FORMAT_TRANSPARENT
){
428 memset(buf
[i
],(unsigned char)(1.0*(255-color
)*colors
[i
]/255),wm
*hm
);
432 memset(buf
[i
],(unsigned char)(1.0*(255-color
)*colors
[7-i
]/255),wm
*hm
);
439 tc
=tdp
[(i
+start_row
)*VBI_COLUMNS
+j
];
440 if (tc
.hidden
) { x
+=wm
; continue;}
441 if(!tc
.gfx
|| (tc
.flh
&& !flashon
)){
442 /* Rendering one text character */
443 draw_alpha_buf(obj
,x
,y
,wm
,hm
,buf
[tc
.bg
],buf
[8],wm
);
444 if(tc
.unicode
!=0x20 && tc
.unicode
!=0x00 && !tc
.ctl
&&
445 (!tc
.flh
|| flashon
) &&
446 (font
=vo_font
->font
[tc
.unicode
])>=0 && y
+hm
<dys
){
447 tt_draw_alpha_buf(obj
,x
,y
,vo_font
->width
[tc
.unicode
],vo_font
->height
,
448 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[tc
.unicode
]-vo_font
->charspace
*vo_font
->pic_a
[font
]->w
,
449 vo_font
->pic_b
[font
]->w
,
450 buf
[tc
.fg
][0],buf
[tc
.bg
][0],buf
[8][0]);
454 Rendering one graphics character
455 TODO: support for separated graphics symbols (where six rectangles does not touch each other)
459 |10| <= |34| <= 00100110 <= 0x26
463 (0:wm/2) (wm/2:wm-wm/2)
465 ********** *********** (0:hm/3)
467 *** 1 **** **** 2 ****
469 ********** ***********
470 ********** ***********
472 ********** *********** (hm/3:hm-2*hm/3)
473 ********** ***********
475 *** 3 **** **** 4 ****
477 ********** ***********
478 ********** ***********
479 ********** ***********
481 ********** *********** (hm-hm/3:hm/3)
483 *** 5 **** **** 6 ****
485 ********** ***********
486 ********** ***********
489 if(tc
.gfx
>1){ //separated gfx
491 color
=(tc
.unicode
>>b
)&1?tc
.fg
:tc
.bg
;
492 draw_alpha_buf(obj
,x
+ax
[b
]+1,y
+ay
[b
]+1,aw
[b
]-2,ah
[b
]-2,buf
[color
],buf
[8],wm
);
494 //separated gfx (background borders)
496 draw_alpha_buf(obj
,x
,y
,1,hm
,buf
[tc
.bg
],buf
[8],wm
);
497 draw_alpha_buf(obj
,x
+ax
[1]-1,y
,2,hm
,buf
[tc
.bg
],buf
[8],wm
);
498 draw_alpha_buf(obj
,x
+ax
[1]+aw
[1]-1,y
,wm
-ax
[1]-aw
[1]+1,hm
,buf
[tc
.bg
],buf
[8],wm
);
500 draw_alpha_buf(obj
,x
,y
,wm
,1,buf
[tc
.bg
],buf
[8],wm
);
501 draw_alpha_buf(obj
,x
,y
+ay
[0]+ah
[0]-1,wm
,2,buf
[tc
.bg
],buf
[8],wm
);
502 draw_alpha_buf(obj
,x
,y
+ay
[2]+ah
[2]-1,wm
,2,buf
[tc
.bg
],buf
[8],wm
);
503 draw_alpha_buf(obj
,x
,y
+ay
[4]+ah
[4]-1,wm
,hm
-ay
[4]-ah
[4]+1,buf
[tc
.bg
],buf
[8],wm
);
506 color
=(tc
.unicode
>>b
)&1?tc
.fg
:tc
.bg
;
507 draw_alpha_buf(obj
,x
+ax
[b
],y
+ay
[b
],aw
[b
],ah
[b
],buf
[color
],buf
[8],wm
);
519 int vo_osd_progbar_type
=-1;
520 int vo_osd_progbar_value
=100; // 0..256
522 // if we have n=256 bars then OSD progbar looks like below
524 // 0 1 2 3 ... 256 <= vo_osd_progbar_value
526 // [ === === === ... === ]
528 // the above schema is rescalled to n=elems bars
530 inline static void vo_update_text_progbar(mp_osd_obj_t
* obj
,int dxs
,int dys
){
532 obj
->flags
|=OSDFLAG_CHANGED
|OSDFLAG_VISIBLE
;
534 if(vo_osd_progbar_type
<0 || !vo_font
){
535 obj
->flags
&=~OSDFLAG_VISIBLE
;
539 render_one_glyph(vo_font
, OSD_PB_START
);
540 render_one_glyph(vo_font
, OSD_PB_END
);
541 render_one_glyph(vo_font
, OSD_PB_0
);
542 render_one_glyph(vo_font
, OSD_PB_1
);
543 render_one_glyph(vo_font
, vo_osd_progbar_type
);
545 // calculate bbox corners:
547 int y
=(dys
-vo_font
->height
)/2;
548 int delimw
=vo_font
->width
[OSD_PB_START
]
549 +vo_font
->width
[OSD_PB_END
]
551 int width
=(2*dxs
-3*delimw
)/3;
552 int charw
=vo_font
->width
[OSD_PB_0
]+vo_font
->charspace
;
553 int elems
=width
/charw
;
554 int x
=(dxs
-elems
*charw
-delimw
)/2;
556 h
=get_height(OSD_PB_START
,h
);
557 h
=get_height(OSD_PB_END
,h
);
558 h
=get_height(OSD_PB_0
,h
);
559 h
=get_height(OSD_PB_1
,h
);
560 if (vo_osd_progbar_type
>0 && vo_font
->font
[vo_osd_progbar_type
]>=0){
561 delta
= vo_font
->width
[vo_osd_progbar_type
]+vo_font
->spacewidth
;
562 delta
= (x
-delta
> 0) ? delta
: x
;
563 h
=get_height(vo_osd_progbar_type
,h
);
565 obj
->bbox
.x1
=obj
->x
=x
;
566 obj
->bbox
.y1
=obj
->y
=y
;
567 obj
->bbox
.x2
=x
+width
+delimw
;
568 obj
->bbox
.y2
=y
+h
; //vo_font->height;
569 obj
->flags
|=OSDFLAG_BBOX
;
570 obj
->params
.progbar
.elems
=elems
;
571 obj
->bbox
.x1
-=delta
; // space for an icon
577 int minw
= vo_font
->width
[OSD_PB_START
]+vo_font
->width
[OSD_PB_END
]+vo_font
->width
[OSD_PB_0
];
578 if (vo_osd_progbar_type
>0 && vo_font
->font
[vo_osd_progbar_type
]>=0){
579 minw
+= vo_font
->width
[vo_osd_progbar_type
]+vo_font
->charspace
+vo_font
->spacewidth
;
581 if (obj
->bbox
.x2
- obj
->bbox
.x1
< minw
) return; // space too small, don't render anything
591 int charw
=vo_font
->width
[OSD_PB_0
]+vo_font
->charspace
;
592 int elems
=obj
->params
.progbar
.elems
;
594 if (vo_osd_progbar_value
<=0)
597 int ev
=vo_osd_progbar_value
*elems
;
599 if (ev
& 0xFF) mark
++;
600 if (mark
>elems
) mark
=elems
;
604 // printf("osd.progbar width=%d xpos=%d\n",width,x);
606 c
=vo_osd_progbar_type
;
607 if(vo_osd_progbar_type
>0 && (font
=vo_font
->font
[c
])>=0) {
608 int xp
=x
-vo_font
->width
[c
]-vo_font
->spacewidth
;
609 draw_alpha_buf(obj
,(xp
<0?0:xp
),y
,
611 vo_font
->pic_a
[font
]->h
,
612 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
613 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
614 vo_font
->pic_a
[font
]->w
);
618 if ((font
=vo_font
->font
[c
])>=0)
619 draw_alpha_buf(obj
,x
,y
,
621 vo_font
->pic_a
[font
]->h
,
622 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
623 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
624 vo_font
->pic_a
[font
]->w
);
625 x
+=vo_font
->width
[c
]+vo_font
->charspace
;
628 if ((font
=vo_font
->font
[c
])>=0){
630 h
=vo_font
->pic_a
[font
]->h
;
631 s
=vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
];
632 sa
=vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
];
633 st
=vo_font
->pic_a
[font
]->w
;
635 draw_alpha_buf(obj
,x
,y
,w
,h
,s
,sa
,st
);
641 if ((font
=vo_font
->font
[c
])>=0){
643 h
=vo_font
->pic_a
[font
]->h
;
644 s
=vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
];
645 sa
=vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
];
646 st
=vo_font
->pic_a
[font
]->w
;
647 if ((i
=elems
-mark
)) do {
648 draw_alpha_buf(obj
,x
,y
,w
,h
,s
,sa
,st
);
654 if ((font
=vo_font
->font
[c
])>=0)
655 draw_alpha_buf(obj
,x
,y
,
657 vo_font
->pic_a
[font
]->h
,
658 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
659 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
660 vo_font
->pic_a
[font
]->w
);
661 // x+=vo_font->width[c]+vo_font->charspace;
664 // vo_osd_progbar_value=(vo_osd_progbar_value+1)&0xFF;
668 subtitle
* vo_sub
=NULL
;
670 inline static void vo_update_text_sub(mp_osd_obj_t
* obj
,int dxs
,int dys
){
672 int c
,i
,j
,l
,x
,y
,font
,prevc
,counter
;
674 int lastStripPosition
;
680 obj
->flags
|=OSDFLAG_CHANGED
|OSDFLAG_VISIBLE
;
682 if(!vo_sub
|| !sub_font
|| !sub_visibility
|| (sub_font
->font
[40]<0)){
683 obj
->flags
&=~OSDFLAG_VISIBLE
;
687 obj
->bbox
.y2
=obj
->y
=dys
;
688 obj
->params
.subtitle
.lines
=0;
690 // too long lines divide into a smaller ones
693 lastStripPosition
=-1;
697 struct osd_text_t
*osl
, *cp_ott
, *tmp_ott
, *tmp
;
698 struct osd_text_p
*otp_sub
= NULL
, *otp_sub_tmp
, // these are used to store the whole sub text osd
699 *otp
, *tmp_otp
, *pmt
; // these are used to manage sub text osd coming from a single sub line
700 int *char_seq
, char_position
, xlimit
= dxs
* sub_width_p
/ 100, counter
;
703 xsize
= -sub_font
->charspace
;
707 char_seq
= calloc(strlen(t
), sizeof(int));
715 // reading the subtitle words from vo_sub->text[]
718 c
= utf8_get_char(&t
);
719 else if ((c
= *t
++) >= 0x80 && sub_unicode
)
722 t
+= strlen(t
); // end here
723 mp_msg(MSGT_OSD
,MSGL_WARN
,"\nMAX_UCS exceeded!\n");
725 if (!c
) c
++; // avoid UCS 0
726 render_one_glyph(sub_font
, c
);
729 struct osd_text_t
*tmp_ott
= calloc(1, sizeof(struct osd_text_t
));
732 osl
= cp_ott
= tmp_ott
;
734 tmp_ott
->prev
= cp_ott
;
735 cp_ott
->next
= tmp_ott
;
736 tmp_ott
->osd_kerning
=
737 sub_font
->charspace
+ sub_font
->width
[' '];
740 tmp_ott
->osd_length
= xsize
;
741 tmp_ott
->text_length
= char_position
;
742 tmp_ott
->text
= malloc(char_position
* sizeof(int));
743 for (counter
= 0; counter
< char_position
; ++counter
)
744 tmp_ott
->text
[counter
] = char_seq
[counter
];
749 int delta_xsize
= sub_font
->width
[c
] + sub_font
->charspace
+ kerning(sub_font
, prevc
, c
);
751 if (xsize
+ delta_xsize
<= dxs
) {
754 char_seq
[char_position
++] = c
;
755 xsize
+= delta_xsize
;
756 if ((!suboverlap_enabled
) && ((font
= sub_font
->font
[c
]) >= 0)) {
757 if (sub_font
->pic_a
[font
]->h
> h
) {
758 h
= sub_font
->pic_a
[font
]->h
;
763 mp_msg(MSGT_OSD
, MSGL_WARN
, "\nSubtitle word '%s' too long!\n", t
);
768 }// for len (all words from subtitle line read)
770 // osl holds an ordered (as they appear in the lines) chain of the subtitle words
772 struct osd_text_t
*tmp_ott
= calloc(1, sizeof(struct osd_text_t
));
775 osl
= cp_ott
= tmp_ott
;
777 tmp_ott
->prev
= cp_ott
;
778 cp_ott
->next
= tmp_ott
;
779 tmp_ott
->osd_kerning
=
780 sub_font
->charspace
+ sub_font
->width
[' '];
783 tmp_ott
->osd_length
= xsize
;
784 tmp_ott
->text_length
= char_position
;
785 tmp_ott
->text
= malloc(char_position
* sizeof(int));
786 for (counter
= 0; counter
< char_position
; ++counter
)
787 tmp_ott
->text
[counter
] = char_seq
[counter
];
789 xsize
= -sub_font
->charspace
;
794 int value
= 0, exit
= 0, minimum
= 0;
796 // otp will contain the chain of the osd subtitle lines coming from the single vo_sub line.
797 otp
= tmp_otp
= calloc(1, sizeof(struct osd_text_p
));
799 for (tmp_ott
= tmp_otp
->ott
; exit
== 0; ) {
801 value
+= tmp_ott
->osd_kerning
+ tmp_ott
->osd_length
;
802 tmp_ott
= tmp_ott
->next
;
803 } while ((tmp_ott
!= NULL
) && (value
+ tmp_ott
->osd_kerning
+ tmp_ott
->osd_length
<= xlimit
));
804 if (tmp_ott
!= NULL
) {
805 struct osd_text_p
*tmp
= calloc(1, sizeof(struct osd_text_p
));
807 tmp_otp
->value
= value
;
811 tmp_otp
->ott
= tmp_ott
;
812 value
= -2 * sub_font
->charspace
- sub_font
->width
[' '];
814 tmp_otp
->value
= value
;
821 // minimum holds the 'sum of the differences in length among the lines',
822 // a measure of the evenness of the lengths of the lines
823 for (tmp_otp
= otp
; tmp_otp
->next
!= NULL
; tmp_otp
= tmp_otp
->next
) {
825 while (pmt
!= NULL
) {
826 minimum
+= abs(tmp_otp
->value
- pmt
->value
);
831 if (otp
->next
!= NULL
) {
833 struct osd_text_p
*mem
, *hold
;
836 // until the last word of a line can be moved to the beginning of following line
837 // reducing the 'sum of the differences in length among the lines', it is done
841 for (tmp_otp
= otp
; tmp_otp
->next
!= NULL
; tmp_otp
= tmp_otp
->next
) {
843 for (tmp
= tmp_otp
->ott
; tmp
->next
!= pmt
->ott
; tmp
= tmp
->next
);
844 if (pmt
->value
+ tmp
->osd_length
+ pmt
->ott
->osd_kerning
<= xlimit
) {
845 mem1
= tmp_otp
->value
;
847 tmp_otp
->value
= mem1
- tmp
->osd_length
- tmp
->osd_kerning
;
848 pmt
->value
= mem2
+ tmp
->osd_length
+ pmt
->ott
->osd_kerning
;
851 for (mem
= otp
; mem
->next
!= NULL
; mem
= mem
->next
) {
853 while (pmt
!= NULL
) {
854 value
+= abs(mem
->value
- pmt
->value
);
858 if (value
< minimum
) {
863 tmp_otp
->value
= mem1
;
864 tmp_otp
->next
->value
= mem2
;
871 for (tmp
= tmp_otp
->ott
; tmp
->next
!= pmt
->ott
; tmp
= tmp
->next
);
872 mem1
= tmp_otp
->value
;
874 tmp_otp
->value
= mem1
- tmp
->osd_length
- tmp
->osd_kerning
;
875 pmt
->value
= mem2
+ tmp
->osd_length
+ pmt
->ott
->osd_kerning
;
879 }//~if(otp->next!=NULL)
882 // adding otp (containing splitted lines) to otp chain
883 if (otp_sub
== NULL
) {
885 for (otp_sub_tmp
= otp_sub
; otp_sub_tmp
->next
!= NULL
; otp_sub_tmp
= otp_sub_tmp
->next
);
889 while (tmp
->next
!= NULL
) tmp
= tmp
->next
;
890 tmp
->next
= otp
->ott
;
891 otp
->ott
->prev
= tmp
;
892 //attaching new subtitle line at the end
893 otp_sub_tmp
->next
= otp
;
894 otp
->prev
= otp_sub_tmp
;
896 otp_sub_tmp
= otp_sub_tmp
->next
;
897 while (otp_sub_tmp
->next
!= NULL
);
902 // write lines into utbl
906 obj
->params
.subtitle
.lines
= 0;
907 for (tmp_otp
= otp_sub
; tmp_otp
!= NULL
; tmp_otp
= tmp_otp
->next
) {
909 if ((obj
->params
.subtitle
.lines
++) >= MAX_UCSLINES
)
912 if (h
> obj
->y
) { // out of the screen so end parsing
913 obj
->y
-= lasth
- sub_font
->height
; // correct the y position
916 xsize
= tmp_otp
->value
;
917 obj
->params
.subtitle
.xtbl
[xtblc
++] = (dxs
- xsize
) / 2;
918 if (xmin
> (dxs
- xsize
) / 2)
919 xmin
= (dxs
- xsize
) / 2;
920 if (xmax
< (dxs
+ xsize
) / 2)
921 xmax
= (dxs
+ xsize
) / 2;
923 tmp
= (tmp_otp
->next
== NULL
) ? NULL
: tmp_otp
->next
->ott
;
924 for (tmp_ott
= tmp_otp
->ott
; tmp_ott
!= tmp
; tmp_ott
= tmp_ott
->next
) {
925 for (counter
= 0; counter
< tmp_ott
->text_length
; ++counter
) {
926 if (utblc
> MAX_UCS
) {
929 c
= tmp_ott
->text
[counter
];
930 render_one_glyph(sub_font
, c
);
931 obj
->params
.subtitle
.utbl
[utblc
++] = c
;
934 obj
->params
.subtitle
.utbl
[utblc
++] = ' ';
936 obj
->params
.subtitle
.utbl
[utblc
- 1] = 0;
937 obj
->y
-= sub_font
->height
;
939 if(obj
->params
.subtitle
.lines
)
940 obj
->y
= dys
- ((obj
->params
.subtitle
.lines
- 1) * sub_font
->height
+ sub_font
->pic_a
[sub_font
->font
[40]]->h
);
943 if (otp_sub
!= NULL
) {
944 for (tmp
= otp_sub
->ott
; tmp
->next
!= NULL
; free(tmp
->prev
)) {
951 for(pmt
= otp_sub
; pmt
->next
!= NULL
; free(pmt
->prev
)) {
958 /// vertical alignment
960 if (sub_alignment
== 2)
961 obj
->y
= dys
* sub_pos
/ 100 - h
;
962 else if (sub_alignment
== 1)
963 obj
->y
= dys
* sub_pos
/ 100 - h
/ 2;
965 obj
->y
= dys
* sub_pos
/ 100;
969 if (obj
->y
> dys
- h
)
972 obj
->bbox
.y2
= obj
->y
+ h
;
975 if (sub_justify
) xmin
= 10;
979 // obj->bbox.y2=obj->y+obj->params.subtitle.lines*sub_font->height;
980 obj
->flags
|=OSDFLAG_BBOX
;
987 switch(vo_sub
->alignment
) {
988 case SUB_ALIGNMENT_BOTTOMLEFT
:
989 case SUB_ALIGNMENT_MIDDLELEFT
:
990 case SUB_ALIGNMENT_TOPLEFT
:
991 obj
->alignment
|= 0x1;
993 case SUB_ALIGNMENT_BOTTOMRIGHT
:
994 case SUB_ALIGNMENT_MIDDLERIGHT
:
995 case SUB_ALIGNMENT_TOPRIGHT
:
996 obj
->alignment
|= 0x2;
998 case SUB_ALIGNMENT_BOTTOMCENTER
:
999 case SUB_ALIGNMENT_MIDDLECENTER
:
1000 case SUB_ALIGNMENT_TOPCENTER
:
1002 obj
->alignment
|= 0x0;
1006 if ((l
= obj
->params
.subtitle
.lines
)) {
1007 for(counter
= dxs
; i
< l
; ++i
)
1008 if (obj
->params
.subtitle
.xtbl
[i
] < counter
) counter
= obj
->params
.subtitle
.xtbl
[i
];
1009 for (i
= 0; i
< l
; ++i
) {
1010 switch (obj
->alignment
&0x3) {
1017 x
= 2 * obj
->params
.subtitle
.xtbl
[i
] - counter
- ((obj
->params
.subtitle
.xtbl
[i
] == counter
) ? 0 : 1);
1021 x
= obj
->params
.subtitle
.xtbl
[i
];
1024 while ((c
=obj
->params
.subtitle
.utbl
[j
++])){
1025 x
+= kerning(sub_font
,prevc
,c
);
1026 if ((font
=sub_font
->font
[c
])>=0)
1027 draw_alpha_buf(obj
,x
,y
,
1029 sub_font
->pic_a
[font
]->h
+y
<obj
->dys
? sub_font
->pic_a
[font
]->h
: obj
->dys
-y
,
1030 sub_font
->pic_b
[font
]->bmp
+sub_font
->start
[c
],
1031 sub_font
->pic_a
[font
]->bmp
+sub_font
->start
[c
],
1032 sub_font
->pic_a
[font
]->w
);
1033 x
+=sub_font
->width
[c
]+sub_font
->charspace
;
1036 y
+=sub_font
->height
;
1042 inline static void vo_update_spudec_sub(mp_osd_obj_t
* obj
, int dxs
, int dys
)
1044 unsigned int bbox
[4];
1045 spudec_calc_bbox(vo_spudec
, dxs
, dys
, bbox
);
1046 obj
->bbox
.x1
= bbox
[0];
1047 obj
->bbox
.x2
= bbox
[1];
1048 obj
->bbox
.y1
= bbox
[2];
1049 obj
->bbox
.y2
= bbox
[3];
1050 obj
->flags
|= OSDFLAG_BBOX
;
1053 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
))
1055 spudec_draw_scaled(vo_spudec
, obj
->dxs
, obj
->dys
, draw_alpha
);
1058 void *vo_spudec
=NULL
;
1059 void *vo_vobsub
=NULL
;
1061 static int draw_alpha_init_flag
=0;
1063 void vo_draw_alpha_init(void);
1065 mp_osd_obj_t
* vo_osd_list
=NULL
;
1067 static mp_osd_obj_t
* new_osd_obj(int type
){
1068 mp_osd_obj_t
* osd
=malloc(sizeof(mp_osd_obj_t
));
1069 memset(osd
,0,sizeof(mp_osd_obj_t
));
1070 osd
->next
=vo_osd_list
;
1073 osd
->alpha_buffer
= NULL
;
1074 osd
->bitmap_buffer
= NULL
;
1075 osd
->allocated
= -1;
1079 void free_osd_list(void){
1080 mp_osd_obj_t
* obj
=vo_osd_list
;
1082 mp_osd_obj_t
* next
=obj
->next
;
1083 if (obj
->alpha_buffer
) free(obj
->alpha_buffer
);
1084 if (obj
->bitmap_buffer
) free(obj
->bitmap_buffer
);
1091 #define FONT_LOAD_DEFER 6
1093 static int vo_update_osd_ext(int dxs
,int dys
, int left_border
, int top_border
,
1094 int right_border
, int bottom_border
, int orig_w
,
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 case OSDTYPE_TELETEXT
:
1158 vo_update_text_teletext(obj
,dxs
,dys
);
1160 case OSDTYPE_PROGBAR
:
1161 vo_update_text_progbar(obj
,dxs
,dys
);
1164 if(sub_visibility
&& vo_spudec
&& spudec_visible(vo_spudec
)){
1165 vo_update_spudec_sub(obj
, dxs
, dys
);
1166 obj
->flags
|=OSDFLAG_VISIBLE
|OSDFLAG_CHANGED
;
1169 obj
->flags
&=~OSDFLAG_VISIBLE
;
1172 if(vo_font
&& vo_osd_text
&& vo_osd_text
[0]){
1173 vo_update_text_osd(obj
,dxs
,dys
); // update bbox
1174 obj
->flags
|=OSDFLAG_VISIBLE
|OSDFLAG_CHANGED
;
1176 obj
->flags
&=~OSDFLAG_VISIBLE
;
1180 if(!(obj
->flags
&OSDFLAG_BBOX
)){
1181 // we don't know, so assume the whole screen changed :(
1182 obj
->bbox
.x1
=obj
->bbox
.y1
=0;
1185 obj
->flags
|=OSDFLAG_BBOX
;
1187 // check bbox, reduce it if it's out of bounds (corners):
1188 if(obj
->bbox
.x1
<0) obj
->bbox
.x1
=0;
1189 if(obj
->bbox
.y1
<0) obj
->bbox
.y1
=0;
1190 if(obj
->bbox
.x2
>dxs
) obj
->bbox
.x2
=dxs
;
1191 if(obj
->bbox
.y2
>dys
) obj
->bbox
.y2
=dys
;
1192 if(obj
->flags
&OSDFLAG_VISIBLE
)
1194 mp_msg(MSGT_OSD
,MSGL_DBG2
,"OSD update: %d;%d %dx%d \n",
1195 obj
->bbox
.x1
,obj
->bbox
.y1
,obj
->bbox
.x2
-obj
->bbox
.x1
,
1196 obj
->bbox
.y2
-obj
->bbox
.y1
);
1198 // check if visibility changed:
1199 if(vis
!= (obj
->flags
&OSDFLAG_VISIBLE
) ) obj
->flags
|=OSDFLAG_CHANGED
;
1200 // remove the cause of automatic update:
1201 obj
->dxs
=dxs
; obj
->dys
=dys
;
1202 obj
->flags
&=~OSDFLAG_FORCE_UPDATE
;
1204 if(obj
->flags
&OSDFLAG_CHANGED
){
1206 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
);
1213 int vo_update_osd(int dxs
, int dys
) {
1214 return vo_update_osd_ext(dxs
, dys
, 0, 0, 0, 0, dxs
, dys
);
1217 void vo_init_osd(void){
1218 if(!draw_alpha_init_flag
){
1219 draw_alpha_init_flag
=1;
1220 vo_draw_alpha_init();
1222 if(vo_osd_list
) free_osd_list();
1223 // temp hack, should be moved to mplayer/mencoder later
1224 new_osd_obj(OSDTYPE_OSD
);
1225 new_osd_obj(OSDTYPE_SUBTITLE
);
1226 new_osd_obj(OSDTYPE_PROGBAR
);
1227 new_osd_obj(OSDTYPE_SPU
);
1228 #ifdef CONFIG_DVDNAV
1229 new_osd_obj(OSDTYPE_DVDNAV
);
1231 new_osd_obj(OSDTYPE_TELETEXT
);
1232 #ifdef CONFIG_FREETYPE
1233 force_load_font
= 1;
1237 int vo_osd_changed_flag
=0;
1239 void vo_remove_text(int dxs
,int dys
,void (*remove
)(int x0
,int y0
, int w
,int h
)){
1240 mp_osd_obj_t
* obj
=vo_osd_list
;
1241 vo_update_osd(dxs
,dys
);
1243 if(((obj
->flags
&OSDFLAG_CHANGED
) || (obj
->flags
&OSDFLAG_VISIBLE
)) &&
1244 (obj
->flags
&OSDFLAG_OLD_BBOX
)){
1245 int w
=obj
->old_bbox
.x2
-obj
->old_bbox
.x1
;
1246 int h
=obj
->old_bbox
.y2
-obj
->old_bbox
.y1
;
1248 vo_osd_changed_flag
=obj
->flags
&OSDFLAG_CHANGED
; // temp hack
1249 remove(obj
->old_bbox
.x1
,obj
->old_bbox
.y1
,w
,h
);
1251 // obj->flags&=~OSDFLAG_OLD_BBOX;
1257 void vo_draw_text_ext(int dxs
, int dys
, int left_border
, int top_border
,
1258 int right_border
, int bottom_border
, int orig_w
, int orig_h
,
1259 void (*draw_alpha
)(int x0
, int y0
, int w
,int h
, unsigned char* src
, unsigned char *srca
, int stride
)) {
1260 mp_osd_obj_t
* obj
=vo_osd_list
;
1261 vo_update_osd_ext(dxs
, dys
, left_border
, top_border
, right_border
, bottom_border
, orig_w
, orig_h
);
1263 if(obj
->flags
&OSDFLAG_VISIBLE
){
1264 vo_osd_changed_flag
=obj
->flags
&OSDFLAG_CHANGED
; // temp hack
1267 vo_draw_spudec_sub(obj
, draw_alpha
); // FIXME
1269 #ifdef CONFIG_DVDNAV
1270 case OSDTYPE_DVDNAV
:
1272 case OSDTYPE_TELETEXT
:
1274 case OSDTYPE_SUBTITLE
:
1275 case OSDTYPE_PROGBAR
:
1276 vo_draw_text_from_buffer(obj
,draw_alpha
);
1279 obj
->old_bbox
=obj
->bbox
;
1280 obj
->flags
|=OSDFLAG_OLD_BBOX
;
1282 obj
->flags
&=~OSDFLAG_CHANGED
;
1287 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
)) {
1288 vo_draw_text_ext(dxs
, dys
, 0, 0, 0, 0, dxs
, dys
, draw_alpha
);
1291 static int vo_osd_changed_status
= 0;
1293 int vo_osd_changed(int new_value
)
1295 mp_osd_obj_t
* obj
=vo_osd_list
;
1296 int ret
= vo_osd_changed_status
;
1297 vo_osd_changed_status
= new_value
;
1300 if(obj
->type
==new_value
) obj
->flags
|=OSDFLAG_FORCE_UPDATE
;
1307 // BBBBBBBBBBBB AAAAAAAAAAAAA BBBBBBBBBBB
1308 // BBBBBBBBBBBB BBBBBBBBBBBBB
1311 // return TRUE if we have osd in the specified rectangular area:
1312 int vo_osd_check_range_update(int x1
,int y1
,int x2
,int y2
){
1313 mp_osd_obj_t
* obj
=vo_osd_list
;
1315 if(obj
->flags
&OSDFLAG_VISIBLE
){
1316 if( (obj
->bbox
.x1
<=x2
&& obj
->bbox
.x2
>=x1
) &&
1317 (obj
->bbox
.y1
<=y2
&& obj
->bbox
.y2
>=y1
) &&
1318 obj
->bbox
.y2
> obj
->bbox
.y1
&& obj
->bbox
.x2
> obj
->bbox
.x1