12 #include "stream/stream.h"
13 #include "stream/stream_dvdnav.h"
14 #define OSD_NAV_BOX_ALPHA 0x7f
17 #ifdef CONFIG_TV_TELETEXT
18 #include "stream/tv.h"
19 #include "osdep/timer.h"
26 #include "video_out.h"
27 #include "font_load.h"
30 #include "libavutil/common.h"
35 // Structures needed for the new splitting algorithm.
36 // osd_text_t contains the single subtitle word.
37 // osd_text_p is used to mark the lines of subtitles
39 int osd_kerning
, //kerning with the previous word
40 osd_length
, //orizontal length inside the bbox
41 text_length
, //number of characters
43 struct osd_text_t
*prev
,
49 struct osd_text_t
*ott
;
50 struct osd_text_p
*prev
,
55 char * const sub_osd_names
[]={
63 MSGTR_VO_SUB_Contrast
,
64 MSGTR_VO_SUB_Saturation
,
66 MSGTR_VO_SUB_Brightness
,
70 char * const sub_osd_names_short
[] ={ "", "|>", "||", "[]", "<<" , ">>", "", "", "", "", "", "", "" };
72 //static int vo_font_loaded=-1;
73 font_desc_t
* vo_font
=NULL
;
75 #ifdef CONFIG_TV_TELETEXT
76 void* vo_osd_teletext_page
=NULL
;
77 int vo_osd_teletext_half
= 0;
78 int vo_osd_teletext_mode
=0;
79 int vo_osd_teletext_format
=0;
80 int vo_osd_teletext_scale
=0;
86 int sub_alignment
=2; /* 0=top, 1=center, 2=bottom */
88 int sub_bg_color
=0; /* subtitles background color */
92 static nav_highlight_t nav_hl
;
95 // return the real height of a char:
96 static inline int get_height(int c
,int h
){
98 if ((font
=vo_font
->font
[c
])>=0)
99 if(h
<vo_font
->pic_a
[font
]->h
) h
=vo_font
->pic_a
[font
]->h
;
103 // renders char to a big per-object buffer where alpha and bitmap are separated
104 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
)
106 int dststride
= obj
->stride
;
107 int dstskip
= obj
->stride
-w
;
108 int srcskip
= stride
-w
;
110 unsigned char *b
= obj
->bitmap_buffer
+ (y0
-obj
->bbox
.y1
)*dststride
+ (x0
-obj
->bbox
.x1
);
111 unsigned char *a
= obj
->alpha_buffer
+ (y0
-obj
->bbox
.y1
)*dststride
+ (x0
-obj
->bbox
.x1
);
112 unsigned char *bs
= src
;
113 unsigned char *as
= srca
;
115 if (x0
< obj
->bbox
.x1
|| x0
+w
> obj
->bbox
.x2
|| y0
< obj
->bbox
.y1
|| y0
+h
> obj
->bbox
.y2
) {
116 fprintf(stderr
, "osd text out of range: bbox [%d %d %d %d], txt [%d %d %d %d]\n",
117 obj
->bbox
.x1
, obj
->bbox
.x2
, obj
->bbox
.y1
, obj
->bbox
.y2
,
122 for (i
= 0; i
< h
; i
++) {
123 for (j
= 0; j
< w
; j
++, b
++, a
++, bs
++, as
++) {
124 if (*b
< *bs
) *b
= *bs
;
126 if (*a
== 0 || *a
> *as
) *a
= *as
;
136 // allocates/enlarges the alpha/bitmap buffer
137 static void alloc_buf(mp_osd_obj_t
* obj
)
140 if (obj
->bbox
.x2
< obj
->bbox
.x1
) obj
->bbox
.x2
= obj
->bbox
.x1
;
141 if (obj
->bbox
.y2
< obj
->bbox
.y1
) obj
->bbox
.y2
= obj
->bbox
.y1
;
142 obj
->stride
= ((obj
->bbox
.x2
-obj
->bbox
.x1
)+7)&(~7);
143 len
= obj
->stride
*(obj
->bbox
.y2
-obj
->bbox
.y1
);
144 if (obj
->allocated
<len
) {
145 obj
->allocated
= len
;
146 free(obj
->bitmap_buffer
);
147 free(obj
->alpha_buffer
);
148 obj
->bitmap_buffer
= (unsigned char *)memalign(16, len
);
149 obj
->alpha_buffer
= (unsigned char *)memalign(16, len
);
151 memset(obj
->bitmap_buffer
, sub_bg_color
, len
);
152 memset(obj
->alpha_buffer
, sub_bg_alpha
, len
);
155 // renders the buffer
156 inline static void vo_draw_text_from_buffer(mp_osd_obj_t
* obj
,void (*draw_alpha
)(void *ctx
, int x0
,int y0
, int w
,int h
, unsigned char* src
, unsigned char *srca
, int stride
), void *ctx
)
158 if (obj
->allocated
> 0) {
160 obj
->bbox
.x1
,obj
->bbox
.y1
,
161 obj
->bbox
.x2
-obj
->bbox
.x1
,
162 obj
->bbox
.y2
-obj
->bbox
.y1
,
169 unsigned utf8_get_char(const char **str
) {
170 const uint8_t *strp
= (const uint8_t *)*str
;
172 GET_UTF8(c
, *strp
++, goto no_utf8
;);
173 *str
= (const char *)strp
;
177 strp
= (const uint8_t *)*str
;
179 *str
= (const char *)strp
;
183 inline static void vo_update_text_osd(struct osd_state
*osd
, mp_osd_obj_t
* obj
,
186 const char *cp
= osd
->osd_text
;
191 obj
->bbox
.x1
=obj
->x
=x
;
192 obj
->bbox
.y1
=obj
->y
=10;
195 uint16_t c
=utf8_get_char(&cp
);
196 render_one_glyph(vo_font
, c
);
197 x
+=vo_font
->width
[c
]+vo_font
->charspace
;
201 obj
->bbox
.x2
=x
-vo_font
->charspace
;
202 obj
->bbox
.y2
=obj
->bbox
.y1
+h
;
203 obj
->flags
|=OSDFLAG_BBOX
;
210 uint16_t c
=utf8_get_char(&cp
);
211 if ((font
=vo_font
->font
[c
])>=0)
212 draw_alpha_buf(obj
,x
,obj
->y
,
214 vo_font
->pic_a
[font
]->h
,
215 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
216 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
217 vo_font
->pic_a
[font
]->w
);
218 x
+=vo_font
->width
[c
]+vo_font
->charspace
;
223 void osd_set_nav_box (uint16_t sx
, uint16_t sy
, uint16_t ex
, uint16_t ey
) {
230 inline static void vo_update_nav (mp_osd_obj_t
*obj
, int dxs
, int dys
, int left_border
, int top_border
,
231 int right_border
, int bottom_border
, int orig_w
, int orig_h
) {
233 int sx
= nav_hl
.sx
, sy
= nav_hl
.sy
;
234 int ex
= nav_hl
.ex
, ey
= nav_hl
.ey
;
235 int scaled_w
= dxs
- left_border
- right_border
;
236 int scaled_h
= dys
- top_border
- bottom_border
;
237 if (scaled_w
!= orig_w
) {
238 sx
= sx
* scaled_w
/ orig_w
;
239 ex
= ex
* scaled_w
/ orig_w
;
241 if (scaled_h
!= orig_h
) {
242 sy
= sy
* scaled_h
/ orig_h
;
243 ey
= ey
* scaled_h
/ orig_h
;
245 sx
+= left_border
; ex
+= left_border
;
246 sy
+= top_border
; ey
+= top_border
;
247 sx
= FFMIN(FFMAX(sx
, 0), dxs
);
248 ex
= FFMIN(FFMAX(ex
, 0), dxs
);
249 sy
= FFMIN(FFMAX(sy
, 0), dys
);
250 ey
= FFMIN(FFMAX(ey
, 0), dys
);
252 obj
->bbox
.x1
= obj
->x
= sx
;
253 obj
->bbox
.y1
= obj
->y
= sy
;
258 len
= obj
->stride
* (obj
->bbox
.y2
- obj
->bbox
.y1
);
259 memset (obj
->bitmap_buffer
, OSD_NAV_BOX_ALPHA
, len
);
260 memset (obj
->alpha_buffer
, OSD_NAV_BOX_ALPHA
, len
);
261 obj
->flags
|= OSDFLAG_BBOX
| OSDFLAG_CHANGED
;
262 if (obj
->bbox
.y2
> obj
->bbox
.y1
&& obj
->bbox
.x2
> obj
->bbox
.x1
)
263 obj
->flags
|= OSDFLAG_VISIBLE
;
267 #ifdef CONFIG_TV_TELETEXT
268 // renders char to a big per-object buffer where alpha and bitmap are separated
269 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
)
271 int dststride
= obj
->stride
;
272 int dstskip
= obj
->stride
-w
;
273 int srcskip
= stride
-w
;
275 unsigned char *b
= obj
->bitmap_buffer
+ (y0
-obj
->bbox
.y1
)*dststride
+ (x0
-obj
->bbox
.x1
);
276 unsigned char *a
= obj
->alpha_buffer
+ (y0
-obj
->bbox
.y1
)*dststride
+ (x0
-obj
->bbox
.x1
);
277 unsigned char *bs
= src
;
278 if (x0
< obj
->bbox
.x1
|| x0
+w
> obj
->bbox
.x2
|| y0
< obj
->bbox
.y1
|| y0
+h
> obj
->bbox
.y2
) {
279 mp_msg(MSGT_OSD
,MSGL_ERR
,"tt osd text out of range: bbox [%d %d %d %d], txt [%d %d %d %d]\n",
280 obj
->bbox
.x1
, obj
->bbox
.x2
, obj
->bbox
.y1
, obj
->bbox
.y2
,
284 for (i
= 0; i
< h
; i
++) {
285 for (j
= 0; j
< w
; j
++, b
++, a
++, bs
++) {
286 *b
=(fg
-bg
)*(*bs
)/255+bg
;
294 inline static void vo_update_text_teletext(mp_osd_obj_t
*obj
, int dxs
, int dys
)
296 int h
=0,w
=0,i
,j
,font
,flashon
;
304 int start_row
,max_rows
;
305 int b
,ax
[6],ay
[6],aw
[6],ah
[6];
307 tt_char
* tdp
=vo_osd_teletext_page
;
308 unsigned char colors
[8]={1,85,150,226,70,105,179,254};
309 unsigned char* buf
[9];
311 obj
->flags
|=OSDFLAG_CHANGED
|OSDFLAG_VISIBLE
;
312 if (!tdp
|| !vo_osd_teletext_mode
) {
313 obj
->flags
&=~OSDFLAG_VISIBLE
;
316 flashon
=(GetTimer()/1000000)%2;
317 switch(vo_osd_teletext_half
){
318 case TT_ZOOM_TOP_HALF
:
322 case TT_ZOOM_BOTTOM_HALF
:
323 start_row
=VBI_ROWS
/2;
332 for(i
=start_row
;i
<max_rows
;i
++){
333 for(j
=0;j
<VBI_COLUMNS
;j
++){
334 tc
=tdp
[i
*VBI_COLUMNS
+j
];
335 if(!tc
.ctl
&& !tc
.gfx
)
337 render_one_glyph(vo_font
, tc
.unicode
);
338 if (wm
<vo_font
->width
[tc
.unicode
])
339 wm
=vo_font
->width
[tc
.unicode
];
344 hm
=vo_font
->height
+1;
345 wm
=dxs
*hm
*max_rows
/(dys
*VBI_COLUMNS
);
347 //very simple teletext font auto scaling
348 if(!vo_osd_teletext_scale
&& hm
*(max_rows
+1)>dys
){
349 text_font_scale_factor
*=1.0*(dys
)/((max_rows
+1)*hm
);
351 vo_osd_teletext_scale
=text_font_scale_factor
;
352 obj
->flags
&=~OSDFLAG_VISIBLE
;
363 w
=cols
*wm
-vo_font
->charspace
;
364 h
=rows
*hm
-vo_font
->charspace
;
404 obj
->flags
|= OSDFLAG_BBOX
;
408 buf
[i
]=malloc(wm
*hm
);
411 if(vo_osd_teletext_format
==TT_FORMAT_OPAQUE
||vo_osd_teletext_format
==TT_FORMAT_OPAQUE_INV
)
415 memset(buf
[8],color
,wm
*hm
);
417 if(vo_osd_teletext_format
==TT_FORMAT_OPAQUE
||vo_osd_teletext_format
==TT_FORMAT_TRANSPARENT
){
419 memset(buf
[i
],(unsigned char)(1.0*(255-color
)*colors
[i
]/255),wm
*hm
);
423 memset(buf
[i
],(unsigned char)(1.0*(255-color
)*colors
[7-i
]/255),wm
*hm
);
430 tc
=tdp
[(i
+start_row
)*VBI_COLUMNS
+j
];
431 if (tc
.hidden
) { x
+=wm
; continue;}
432 if(!tc
.gfx
|| (tc
.flh
&& !flashon
)){
433 /* Rendering one text character */
434 draw_alpha_buf(obj
,x
,y
,wm
,hm
,buf
[tc
.bg
],buf
[8],wm
);
435 if(tc
.unicode
!=0x20 && tc
.unicode
!=0x00 && !tc
.ctl
&&
436 (!tc
.flh
|| flashon
) &&
437 (font
=vo_font
->font
[tc
.unicode
])>=0 && y
+hm
<dys
){
438 tt_draw_alpha_buf(obj
,x
,y
,vo_font
->width
[tc
.unicode
],vo_font
->height
,
439 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[tc
.unicode
]-vo_font
->charspace
*vo_font
->pic_a
[font
]->w
,
440 vo_font
->pic_b
[font
]->w
,
441 buf
[tc
.fg
][0],buf
[tc
.bg
][0],buf
[8][0]);
445 Rendering one graphics character
446 TODO: support for separated graphics symbols (where six rectangles does not touch each other)
450 |10| <= |34| <= 00100110 <= 0x26
454 (0:wm/2) (wm/2:wm-wm/2)
456 ********** *********** (0:hm/3)
458 *** 1 **** **** 2 ****
460 ********** ***********
461 ********** ***********
463 ********** *********** (hm/3:hm-2*hm/3)
464 ********** ***********
466 *** 3 **** **** 4 ****
468 ********** ***********
469 ********** ***********
470 ********** ***********
472 ********** *********** (hm-hm/3:hm/3)
474 *** 5 **** **** 6 ****
476 ********** ***********
477 ********** ***********
480 if(tc
.gfx
>1){ //separated gfx
482 color
=(tc
.unicode
>>b
)&1?tc
.fg
:tc
.bg
;
483 draw_alpha_buf(obj
,x
+ax
[b
]+1,y
+ay
[b
]+1,aw
[b
]-2,ah
[b
]-2,buf
[color
],buf
[8],wm
);
485 //separated gfx (background borders)
487 draw_alpha_buf(obj
,x
,y
,1,hm
,buf
[tc
.bg
],buf
[8],wm
);
488 draw_alpha_buf(obj
,x
+ax
[1]-1,y
,2,hm
,buf
[tc
.bg
],buf
[8],wm
);
489 draw_alpha_buf(obj
,x
+ax
[1]+aw
[1]-1,y
,wm
-ax
[1]-aw
[1]+1,hm
,buf
[tc
.bg
],buf
[8],wm
);
491 draw_alpha_buf(obj
,x
,y
,wm
,1,buf
[tc
.bg
],buf
[8],wm
);
492 draw_alpha_buf(obj
,x
,y
+ay
[0]+ah
[0]-1,wm
,2,buf
[tc
.bg
],buf
[8],wm
);
493 draw_alpha_buf(obj
,x
,y
+ay
[2]+ah
[2]-1,wm
,2,buf
[tc
.bg
],buf
[8],wm
);
494 draw_alpha_buf(obj
,x
,y
+ay
[4]+ah
[4]-1,wm
,hm
-ay
[4]-ah
[4]+1,buf
[tc
.bg
],buf
[8],wm
);
497 color
=(tc
.unicode
>>b
)&1?tc
.fg
:tc
.bg
;
498 draw_alpha_buf(obj
,x
+ax
[b
],y
+ay
[b
],aw
[b
],ah
[b
],buf
[color
],buf
[8],wm
);
511 int vo_osd_progbar_type
=-1;
512 int vo_osd_progbar_value
=100; // 0..256
514 // if we have n=256 bars then OSD progbar looks like below
516 // 0 1 2 3 ... 256 <= vo_osd_progbar_value
518 // [ === === === ... === ]
520 // the above schema is rescalled to n=elems bars
522 inline static void vo_update_text_progbar(mp_osd_obj_t
* obj
,int dxs
,int dys
){
524 obj
->flags
|=OSDFLAG_CHANGED
|OSDFLAG_VISIBLE
;
526 if(vo_osd_progbar_type
<0 || !vo_font
){
527 obj
->flags
&=~OSDFLAG_VISIBLE
;
531 render_one_glyph(vo_font
, OSD_PB_START
);
532 render_one_glyph(vo_font
, OSD_PB_END
);
533 render_one_glyph(vo_font
, OSD_PB_0
);
534 render_one_glyph(vo_font
, OSD_PB_1
);
535 render_one_glyph(vo_font
, vo_osd_progbar_type
);
537 // calculate bbox corners:
539 int y
=(dys
-vo_font
->height
)/2;
540 int delimw
=vo_font
->width
[OSD_PB_START
]
541 +vo_font
->width
[OSD_PB_END
]
543 int width
=(2*dxs
-3*delimw
)/3;
544 int charw
=vo_font
->width
[OSD_PB_0
]+vo_font
->charspace
;
545 int elems
=width
/charw
;
546 int x
=(dxs
-elems
*charw
-delimw
)/2;
548 h
=get_height(OSD_PB_START
,h
);
549 h
=get_height(OSD_PB_END
,h
);
550 h
=get_height(OSD_PB_0
,h
);
551 h
=get_height(OSD_PB_1
,h
);
552 if (vo_osd_progbar_type
>0 && vo_font
->font
[vo_osd_progbar_type
]>=0){
553 delta
= vo_font
->width
[vo_osd_progbar_type
]+vo_font
->spacewidth
;
554 delta
= (x
-delta
> 0) ? delta
: x
;
555 h
=get_height(vo_osd_progbar_type
,h
);
557 obj
->bbox
.x1
=obj
->x
=x
;
558 obj
->bbox
.y1
=obj
->y
=y
;
559 obj
->bbox
.x2
=x
+width
+delimw
;
560 obj
->bbox
.y2
=y
+h
; //vo_font->height;
561 obj
->flags
|=OSDFLAG_BBOX
;
562 obj
->params
.progbar
.elems
=elems
;
563 obj
->bbox
.x1
-=delta
; // space for an icon
569 int minw
= vo_font
->width
[OSD_PB_START
]+vo_font
->width
[OSD_PB_END
]+vo_font
->width
[OSD_PB_0
];
570 if (vo_osd_progbar_type
>0 && vo_font
->font
[vo_osd_progbar_type
]>=0){
571 minw
+= vo_font
->width
[vo_osd_progbar_type
]+vo_font
->charspace
+vo_font
->spacewidth
;
573 if (obj
->bbox
.x2
- obj
->bbox
.x1
< minw
) return; // space too small, don't render anything
583 int charw
=vo_font
->width
[OSD_PB_0
]+vo_font
->charspace
;
584 int elems
=obj
->params
.progbar
.elems
;
586 if (vo_osd_progbar_value
<=0)
589 int ev
=vo_osd_progbar_value
*elems
;
591 if (ev
& 0xFF) mark
++;
592 if (mark
>elems
) mark
=elems
;
596 // printf("osd.progbar width=%d xpos=%d\n",width,x);
598 c
=vo_osd_progbar_type
;
599 if(vo_osd_progbar_type
>0 && (font
=vo_font
->font
[c
])>=0) {
600 int xp
=x
-vo_font
->width
[c
]-vo_font
->spacewidth
;
601 draw_alpha_buf(obj
,(xp
<0?0:xp
),y
,
603 vo_font
->pic_a
[font
]->h
,
604 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
605 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
606 vo_font
->pic_a
[font
]->w
);
610 if ((font
=vo_font
->font
[c
])>=0)
611 draw_alpha_buf(obj
,x
,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
);
617 x
+=vo_font
->width
[c
]+vo_font
->charspace
;
620 if ((font
=vo_font
->font
[c
])>=0){
622 h
=vo_font
->pic_a
[font
]->h
;
623 s
=vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
];
624 sa
=vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
];
625 st
=vo_font
->pic_a
[font
]->w
;
627 draw_alpha_buf(obj
,x
,y
,w
,h
,s
,sa
,st
);
633 if ((font
=vo_font
->font
[c
])>=0){
635 h
=vo_font
->pic_a
[font
]->h
;
636 s
=vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
];
637 sa
=vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
];
638 st
=vo_font
->pic_a
[font
]->w
;
639 if ((i
=elems
-mark
)) do {
640 draw_alpha_buf(obj
,x
,y
,w
,h
,s
,sa
,st
);
646 if ((font
=vo_font
->font
[c
])>=0)
647 draw_alpha_buf(obj
,x
,y
,
649 vo_font
->pic_a
[font
]->h
,
650 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
651 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
652 vo_font
->pic_a
[font
]->w
);
653 // x+=vo_font->width[c]+vo_font->charspace;
656 // vo_osd_progbar_value=(vo_osd_progbar_value+1)&0xFF;
660 subtitle
* vo_sub
=NULL
;
662 inline static void vo_update_text_sub(struct osd_state
*osd
, mp_osd_obj_t
* obj
,int dxs
,int dys
){
664 int c
,i
,j
,l
,x
,y
,font
,prevc
,counter
;
666 int lastStripPosition
;
671 struct font_desc
*sub_font
= osd
->sub_font
;
673 obj
->flags
|=OSDFLAG_CHANGED
|OSDFLAG_VISIBLE
;
675 if(!vo_sub
|| !osd
->sub_font
|| !sub_visibility
|| (sub_font
->font
[40]<0)){
676 obj
->flags
&=~OSDFLAG_VISIBLE
;
680 obj
->bbox
.y2
=obj
->y
=dys
;
681 obj
->params
.subtitle
.lines
=0;
683 // too long lines divide into a smaller ones
686 lastStripPosition
=-1;
690 struct osd_text_t
*osl
, *cp_ott
, *tmp_ott
, *tmp
;
691 struct osd_text_p
*otp_sub
= NULL
, *otp_sub_tmp
, // these are used to store the whole sub text osd
692 *otp
, *tmp_otp
, *pmt
; // these are used to manage sub text osd coming from a single sub line
693 int *char_seq
, char_position
, xlimit
= dxs
* sub_width_p
/ 100, counter
;
696 xsize
= -sub_font
->charspace
;
700 char_seq
= calloc(strlen(t
), sizeof(int));
708 // reading the subtitle words from vo_sub->text[]
711 c
= utf8_get_char(&t
);
712 else if ((c
= *t
++) >= 0x80 && sub_unicode
)
715 t
+= strlen(t
); // end here
716 mp_msg(MSGT_OSD
,MSGL_WARN
,"\nMAX_UCS exceeded!\n");
718 if (!c
) c
++; // avoid UCS 0
719 render_one_glyph(sub_font
, c
);
722 struct osd_text_t
*tmp_ott
= (struct osd_text_t
*) calloc(1, sizeof(struct osd_text_t
));
725 osl
= cp_ott
= tmp_ott
;
727 tmp_ott
->prev
= cp_ott
;
728 cp_ott
->next
= tmp_ott
;
729 tmp_ott
->osd_kerning
=
730 sub_font
->charspace
+ sub_font
->width
[' '];
733 tmp_ott
->osd_length
= xsize
;
734 tmp_ott
->text_length
= char_position
;
735 tmp_ott
->text
= (int *) malloc(char_position
* sizeof(int));
736 for (counter
= 0; counter
< char_position
; ++counter
)
737 tmp_ott
->text
[counter
] = char_seq
[counter
];
742 int delta_xsize
= sub_font
->width
[c
] + sub_font
->charspace
+ kerning(sub_font
, prevc
, c
);
744 if (xsize
+ delta_xsize
<= dxs
) {
747 char_seq
[char_position
++] = c
;
748 xsize
+= delta_xsize
;
749 if ((!suboverlap_enabled
) && ((font
= sub_font
->font
[c
]) >= 0)) {
750 if (sub_font
->pic_a
[font
]->h
> h
) {
751 h
= sub_font
->pic_a
[font
]->h
;
756 mp_msg(MSGT_OSD
, MSGL_WARN
, "\nSubtitle word '%s' too long!\n", t
);
761 }// for len (all words from subtitle line read)
763 // osl holds an ordered (as they appear in the lines) chain of the subtitle words
765 struct osd_text_t
*tmp_ott
= (struct osd_text_t
*) calloc(1, sizeof(struct osd_text_t
));
768 osl
= cp_ott
= tmp_ott
;
770 tmp_ott
->prev
= cp_ott
;
771 cp_ott
->next
= tmp_ott
;
772 tmp_ott
->osd_kerning
=
773 sub_font
->charspace
+ sub_font
->width
[' '];
776 tmp_ott
->osd_length
= xsize
;
777 tmp_ott
->text_length
= char_position
;
778 tmp_ott
->text
= (int *) malloc(char_position
* sizeof(int));
779 for (counter
= 0; counter
< char_position
; ++counter
)
780 tmp_ott
->text
[counter
] = char_seq
[counter
];
782 xsize
= -sub_font
->charspace
;
787 int value
= 0, exit
= 0, minimum
= 0;
789 // otp will contain the chain of the osd subtitle lines coming from the single vo_sub line.
790 otp
= tmp_otp
= (struct osd_text_p
*) calloc(1, sizeof(struct osd_text_p
));
792 for (tmp_ott
= tmp_otp
->ott
; exit
== 0; ) {
794 value
+= tmp_ott
->osd_kerning
+ tmp_ott
->osd_length
;
795 tmp_ott
= tmp_ott
->next
;
796 } while ((tmp_ott
!= NULL
) && (value
+ tmp_ott
->osd_kerning
+ tmp_ott
->osd_length
<= xlimit
));
797 if (tmp_ott
!= NULL
) {
798 struct osd_text_p
*tmp
= (struct osd_text_p
*) calloc(1, sizeof(struct osd_text_p
));
800 tmp_otp
->value
= value
;
804 tmp_otp
->ott
= tmp_ott
;
805 value
= -2 * sub_font
->charspace
- sub_font
->width
[' '];
807 tmp_otp
->value
= value
;
814 // minimum holds the 'sum of the differences in length among the lines',
815 // a measure of the evenness of the lengths of the lines
816 for (tmp_otp
= otp
; tmp_otp
->next
!= NULL
; tmp_otp
= tmp_otp
->next
) {
818 while (pmt
!= NULL
) {
819 minimum
+= abs(tmp_otp
->value
- pmt
->value
);
824 if (otp
->next
!= NULL
) {
826 struct osd_text_p
*mem
, *hold
;
829 // until the last word of a line can be moved to the beginning of following line
830 // reducing the 'sum of the differences in length among the lines', it is done
834 for (tmp_otp
= otp
; tmp_otp
->next
!= NULL
; tmp_otp
= tmp_otp
->next
) {
836 for (tmp
= tmp_otp
->ott
; tmp
->next
!= pmt
->ott
; tmp
= tmp
->next
);
837 if (pmt
->value
+ tmp
->osd_length
+ pmt
->ott
->osd_kerning
<= xlimit
) {
838 mem1
= tmp_otp
->value
;
840 tmp_otp
->value
= mem1
- tmp
->osd_length
- tmp
->osd_kerning
;
841 pmt
->value
= mem2
+ tmp
->osd_length
+ pmt
->ott
->osd_kerning
;
844 for (mem
= otp
; mem
->next
!= NULL
; mem
= mem
->next
) {
846 while (pmt
!= NULL
) {
847 value
+= abs(mem
->value
- pmt
->value
);
851 if (value
< minimum
) {
856 tmp_otp
->value
= mem1
;
857 tmp_otp
->next
->value
= mem2
;
864 for (tmp
= tmp_otp
->ott
; tmp
->next
!= pmt
->ott
; tmp
= tmp
->next
);
865 mem1
= tmp_otp
->value
;
867 tmp_otp
->value
= mem1
- tmp
->osd_length
- tmp
->osd_kerning
;
868 pmt
->value
= mem2
+ tmp
->osd_length
+ pmt
->ott
->osd_kerning
;
872 }//~if(otp->next!=NULL)
875 // adding otp (containing splitted lines) to otp chain
876 if (otp_sub
== NULL
) {
878 for (otp_sub_tmp
= otp_sub
; otp_sub_tmp
->next
!= NULL
; otp_sub_tmp
= otp_sub_tmp
->next
);
882 while (tmp
->next
!= NULL
) tmp
= tmp
->next
;
883 tmp
->next
= otp
->ott
;
884 otp
->ott
->prev
= tmp
;
885 //attaching new subtitle line at the end
886 otp_sub_tmp
->next
= otp
;
887 otp
->prev
= otp_sub_tmp
;
889 otp_sub_tmp
= otp_sub_tmp
->next
;
890 while (otp_sub_tmp
->next
!= NULL
);
895 // write lines into utbl
899 obj
->params
.subtitle
.lines
= 0;
900 for (tmp_otp
= otp_sub
; tmp_otp
!= NULL
; tmp_otp
= tmp_otp
->next
) {
902 if ((obj
->params
.subtitle
.lines
++) >= MAX_UCSLINES
)
905 if (h
> obj
->y
) { // out of the screen so end parsing
906 obj
->y
-= lasth
- sub_font
->height
; // correct the y position
909 xsize
= tmp_otp
->value
;
910 obj
->params
.subtitle
.xtbl
[xtblc
++] = (dxs
- xsize
) / 2;
911 if (xmin
> (dxs
- xsize
) / 2)
912 xmin
= (dxs
- xsize
) / 2;
913 if (xmax
< (dxs
+ xsize
) / 2)
914 xmax
= (dxs
+ xsize
) / 2;
916 tmp
= (tmp_otp
->next
== NULL
) ? NULL
: tmp_otp
->next
->ott
;
917 for (tmp_ott
= tmp_otp
->ott
; tmp_ott
!= tmp
; tmp_ott
= tmp_ott
->next
) {
918 for (counter
= 0; counter
< tmp_ott
->text_length
; ++counter
) {
919 if (utblc
> MAX_UCS
) {
922 c
= tmp_ott
->text
[counter
];
923 render_one_glyph(sub_font
, c
);
924 obj
->params
.subtitle
.utbl
[utblc
++] = c
;
927 obj
->params
.subtitle
.utbl
[utblc
++] = ' ';
929 obj
->params
.subtitle
.utbl
[utblc
- 1] = 0;
930 obj
->y
-= sub_font
->height
;
932 if(obj
->params
.subtitle
.lines
)
933 obj
->y
= dys
- ((obj
->params
.subtitle
.lines
- 1) * sub_font
->height
+ sub_font
->pic_a
[sub_font
->font
[40]]->h
);
936 if (otp_sub
!= NULL
) {
937 for (tmp
= otp_sub
->ott
; tmp
->next
!= NULL
; free(tmp
->prev
)) {
944 for(pmt
= otp_sub
; pmt
->next
!= NULL
; free(pmt
->prev
)) {
951 /// vertical alignment
953 if (sub_alignment
== 2)
954 obj
->y
= dys
* sub_pos
/ 100 - h
;
955 else if (sub_alignment
== 1)
956 obj
->y
= dys
* sub_pos
/ 100 - h
/ 2;
958 obj
->y
= dys
* sub_pos
/ 100;
962 if (obj
->y
> dys
- h
)
965 obj
->bbox
.y2
= obj
->y
+ h
;
968 if (sub_justify
) xmin
= 10;
972 // obj->bbox.y2=obj->y+obj->params.subtitle.lines*sub_font->height;
973 obj
->flags
|=OSDFLAG_BBOX
;
980 switch(vo_sub
->alignment
) {
981 case SUB_ALIGNMENT_BOTTOMLEFT
:
982 case SUB_ALIGNMENT_MIDDLELEFT
:
983 case SUB_ALIGNMENT_TOPLEFT
:
984 obj
->alignment
|= 0x1;
986 case SUB_ALIGNMENT_BOTTOMRIGHT
:
987 case SUB_ALIGNMENT_MIDDLERIGHT
:
988 case SUB_ALIGNMENT_TOPRIGHT
:
989 obj
->alignment
|= 0x2;
991 case SUB_ALIGNMENT_BOTTOMCENTER
:
992 case SUB_ALIGNMENT_MIDDLECENTER
:
993 case SUB_ALIGNMENT_TOPCENTER
:
995 obj
->alignment
|= 0x0;
999 if ((l
= obj
->params
.subtitle
.lines
)) {
1000 for(counter
= dxs
; i
< l
; ++i
)
1001 if (obj
->params
.subtitle
.xtbl
[i
] < counter
) counter
= obj
->params
.subtitle
.xtbl
[i
];
1002 for (i
= 0; i
< l
; ++i
) {
1003 switch (obj
->alignment
&0x3) {
1010 x
= 2 * obj
->params
.subtitle
.xtbl
[i
] - counter
- ((obj
->params
.subtitle
.xtbl
[i
] == counter
) ? 0 : 1);
1014 x
= obj
->params
.subtitle
.xtbl
[i
];
1017 while ((c
=obj
->params
.subtitle
.utbl
[j
++])){
1018 x
+= kerning(sub_font
,prevc
,c
);
1019 if ((font
=sub_font
->font
[c
])>=0)
1020 draw_alpha_buf(obj
,x
,y
,
1022 sub_font
->pic_a
[font
]->h
+y
<obj
->dys
? sub_font
->pic_a
[font
]->h
: obj
->dys
-y
,
1023 sub_font
->pic_b
[font
]->bmp
+sub_font
->start
[c
],
1024 sub_font
->pic_a
[font
]->bmp
+sub_font
->start
[c
],
1025 sub_font
->pic_a
[font
]->w
);
1026 x
+=sub_font
->width
[c
]+sub_font
->charspace
;
1029 y
+=sub_font
->height
;
1035 inline static void vo_update_spudec_sub(mp_osd_obj_t
* obj
, int dxs
, int dys
)
1037 unsigned int bbox
[4];
1038 spudec_calc_bbox(vo_spudec
, dxs
, dys
, bbox
);
1039 obj
->bbox
.x1
= bbox
[0];
1040 obj
->bbox
.x2
= bbox
[1];
1041 obj
->bbox
.y1
= bbox
[2];
1042 obj
->bbox
.y2
= bbox
[3];
1043 obj
->flags
|= OSDFLAG_BBOX
;
1046 inline static void vo_draw_spudec_sub(mp_osd_obj_t
* obj
, void (*draw_alpha
)(void *ctx
, int x0
, int y0
, int w
, int h
, unsigned char* src
, unsigned char* srca
, int stride
), void *ctx
)
1048 spudec_draw_scaled(vo_spudec
, obj
->dxs
, obj
->dys
, draw_alpha
, ctx
);
1051 void *vo_spudec
=NULL
;
1052 void *vo_vobsub
=NULL
;
1054 static int draw_alpha_init_flag
=0;
1056 void vo_draw_alpha_init(void);
1058 mp_osd_obj_t
* vo_osd_list
=NULL
;
1060 static mp_osd_obj_t
* new_osd_obj(int type
){
1061 mp_osd_obj_t
* osd
=malloc(sizeof(mp_osd_obj_t
));
1062 memset(osd
,0,sizeof(mp_osd_obj_t
));
1063 osd
->next
=vo_osd_list
;
1066 osd
->alpha_buffer
= NULL
;
1067 osd
->bitmap_buffer
= NULL
;
1068 osd
->allocated
= -1;
1072 void osd_free(struct osd_state
*osd
)
1074 mp_osd_obj_t
* obj
=vo_osd_list
;
1076 mp_osd_obj_t
* next
=obj
->next
;
1077 if (obj
->alpha_buffer
) free(obj
->alpha_buffer
);
1078 if (obj
->bitmap_buffer
) free(obj
->bitmap_buffer
);
1086 #define FONT_LOAD_DEFER 6
1088 int osd_update_ext(struct osd_state
*osd
, int dxs
, int dys
, int left_border
,
1089 int top_border
, int right_border
, int bottom_border
,
1090 int orig_w
, int orig_h
)
1092 mp_osd_obj_t
* obj
=vo_osd_list
;
1094 #ifdef CONFIG_FREETYPE
1095 static int defer_counter
= 0, prev_dxs
= 0, prev_dys
= 0;
1098 #ifdef CONFIG_FREETYPE
1099 // here is the right place to get screen dimensions
1100 if (((dxs
!= vo_image_width
)
1101 && (subtitle_autoscale
== 2 || subtitle_autoscale
== 3))
1102 || ((dys
!= vo_image_height
)
1103 && (subtitle_autoscale
== 1 || subtitle_autoscale
== 3)))
1105 // screen dimensions changed
1106 // wait a while to avoid useless reloading of the font
1107 if (dxs
== prev_dxs
|| dys
== prev_dys
) {
1114 if (defer_counter
>= FONT_LOAD_DEFER
) force_load_font
= 1;
1117 if (force_load_font
) {
1118 force_load_font
= 0;
1119 load_font_ft(dxs
, dys
, &vo_font
, font_name
, osd_font_scale_factor
);
1121 load_font_ft(dxs
, dys
, &osd
->sub_font
, sub_font_name
, text_font_scale_factor
);
1123 load_font_ft(dxs
, dys
, &osd
->sub_font
, font_name
, text_font_scale_factor
);
1129 load_font_ft(dxs
, dys
, &vo_font
, font_name
, osd_font_scale_factor
);
1130 if (!osd
->sub_font
) {
1132 load_font_ft(dxs
, dys
, &osd
->sub_font
, sub_font_name
, text_font_scale_factor
);
1134 load_font_ft(dxs
, dys
, &osd
->sub_font
, font_name
, text_font_scale_factor
);
1140 if(dxs
!=obj
->dxs
|| dys
!=obj
->dys
|| obj
->flags
&OSDFLAG_FORCE_UPDATE
){
1141 int vis
=obj
->flags
&OSDFLAG_VISIBLE
;
1142 obj
->flags
&=~OSDFLAG_BBOX
;
1144 #ifdef CONFIG_DVDNAV
1145 case OSDTYPE_DVDNAV
:
1146 vo_update_nav(obj
,dxs
,dys
, left_border
, top_border
, right_border
, bottom_border
, orig_w
, orig_h
);
1149 case OSDTYPE_SUBTITLE
:
1150 vo_update_text_sub(osd
, obj
,dxs
,dys
);
1152 #ifdef CONFIG_TV_TELETEXT
1153 case OSDTYPE_TELETEXT
:
1154 vo_update_text_teletext(obj
,dxs
,dys
);
1157 case OSDTYPE_PROGBAR
:
1158 vo_update_text_progbar(obj
,dxs
,dys
);
1161 if(sub_visibility
&& vo_spudec
&& spudec_visible(vo_spudec
)){
1162 vo_update_spudec_sub(obj
, dxs
, dys
);
1163 obj
->flags
|=OSDFLAG_VISIBLE
|OSDFLAG_CHANGED
;
1166 obj
->flags
&=~OSDFLAG_VISIBLE
;
1169 if(vo_font
&& osd
->osd_text
[0]){
1170 vo_update_text_osd(osd
, obj
, dxs
, dys
); // update bbox
1171 obj
->flags
|=OSDFLAG_VISIBLE
|OSDFLAG_CHANGED
;
1173 obj
->flags
&=~OSDFLAG_VISIBLE
;
1177 if(!(obj
->flags
&OSDFLAG_BBOX
)){
1178 // we don't know, so assume the whole screen changed :(
1179 obj
->bbox
.x1
=obj
->bbox
.y1
=0;
1182 obj
->flags
|=OSDFLAG_BBOX
;
1184 // check bbox, reduce it if it's out of bounds (corners):
1185 if(obj
->bbox
.x1
<0) obj
->bbox
.x1
=0;
1186 if(obj
->bbox
.y1
<0) obj
->bbox
.y1
=0;
1187 if(obj
->bbox
.x2
>dxs
) obj
->bbox
.x2
=dxs
;
1188 if(obj
->bbox
.y2
>dys
) obj
->bbox
.y2
=dys
;
1189 if(obj
->flags
&OSDFLAG_VISIBLE
)
1191 mp_msg(MSGT_OSD
,MSGL_DBG2
,"OSD update: %d;%d %dx%d \n",
1192 obj
->bbox
.x1
,obj
->bbox
.y1
,obj
->bbox
.x2
-obj
->bbox
.x1
,
1193 obj
->bbox
.y2
-obj
->bbox
.y1
);
1195 // check if visibility changed:
1196 if(vis
!= (obj
->flags
&OSDFLAG_VISIBLE
) ) obj
->flags
|=OSDFLAG_CHANGED
;
1197 // remove the cause of automatic update:
1198 obj
->dxs
=dxs
; obj
->dys
=dys
;
1199 obj
->flags
&=~OSDFLAG_FORCE_UPDATE
;
1201 if(obj
->flags
&OSDFLAG_CHANGED
){
1203 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
);
1210 int osd_update(struct osd_state
*osd
, int dxs
, int dys
)
1212 return osd_update_ext(osd
, dxs
, dys
, 0, 0, 0, 0, dxs
, dys
);
1215 struct osd_state
*osd_create(void)
1217 struct osd_state
*osd
= talloc_zero(NULL
, struct osd_state
);
1218 *osd
= (struct osd_state
){
1220 if(!draw_alpha_init_flag
){
1221 draw_alpha_init_flag
=1;
1222 vo_draw_alpha_init();
1224 // temp hack, should be moved to mplayer/mencoder later
1225 new_osd_obj(OSDTYPE_OSD
);
1226 new_osd_obj(OSDTYPE_SUBTITLE
);
1227 new_osd_obj(OSDTYPE_PROGBAR
);
1228 new_osd_obj(OSDTYPE_SPU
);
1229 #ifdef CONFIG_DVDNAV
1230 new_osd_obj(OSDTYPE_DVDNAV
);
1232 #if CONFIG_TV_TELETEXT
1233 new_osd_obj(OSDTYPE_TELETEXT
);
1235 #ifdef CONFIG_FREETYPE
1236 force_load_font
= 1;
1241 int vo_osd_changed_flag
=0;
1243 void osd_remove_text(struct osd_state
*osd
, int dxs
, int dys
,
1244 void (*remove
)(int x0
, int y0
, int w
, int h
))
1246 mp_osd_obj_t
* obj
=vo_osd_list
;
1247 osd_update(osd
, dxs
, dys
);
1249 if(((obj
->flags
&OSDFLAG_CHANGED
) || (obj
->flags
&OSDFLAG_VISIBLE
)) &&
1250 (obj
->flags
&OSDFLAG_OLD_BBOX
)){
1251 int w
=obj
->old_bbox
.x2
-obj
->old_bbox
.x1
;
1252 int h
=obj
->old_bbox
.y2
-obj
->old_bbox
.y1
;
1254 vo_osd_changed_flag
=obj
->flags
&OSDFLAG_CHANGED
; // temp hack
1255 remove(obj
->old_bbox
.x1
,obj
->old_bbox
.y1
,w
,h
);
1257 // obj->flags&=~OSDFLAG_OLD_BBOX;
1263 void osd_draw_text_ext(struct osd_state
*osd
, int dxs
, int dys
,
1264 int left_border
, int top_border
, int right_border
,
1265 int bottom_border
, int orig_w
, int orig_h
,
1266 void (*draw_alpha
)(void *ctx
, int x0
, int y0
, int w
,
1267 int h
, unsigned char* src
,
1268 unsigned char *srca
,
1272 mp_osd_obj_t
* obj
=vo_osd_list
;
1273 osd_update_ext(osd
, dxs
, dys
, left_border
, top_border
, right_border
,
1274 bottom_border
, orig_w
, orig_h
);
1276 if(obj
->flags
&OSDFLAG_VISIBLE
){
1277 vo_osd_changed_flag
=obj
->flags
&OSDFLAG_CHANGED
; // temp hack
1280 vo_draw_spudec_sub(obj
, draw_alpha
, ctx
); // FIXME
1282 #ifdef CONFIG_DVDNAV
1283 case OSDTYPE_DVDNAV
:
1285 #ifdef CONFIG_TV_TELETEXT
1286 case OSDTYPE_TELETEXT
:
1289 case OSDTYPE_SUBTITLE
:
1290 case OSDTYPE_PROGBAR
:
1291 vo_draw_text_from_buffer(obj
, draw_alpha
, ctx
);
1294 obj
->old_bbox
=obj
->bbox
;
1295 obj
->flags
|=OSDFLAG_OLD_BBOX
;
1297 obj
->flags
&=~OSDFLAG_CHANGED
;
1302 void osd_draw_text(struct osd_state
*osd
, int dxs
, int dys
,
1303 void (*draw_alpha
)(void *ctx
, int x0
, int y0
, int w
, int h
,
1304 unsigned char* src
, unsigned char *srca
,
1308 osd_draw_text_ext(osd
, dxs
, dys
, 0, 0, 0, 0, dxs
, dys
, draw_alpha
, ctx
);
1311 static int vo_osd_changed_status
= 0;
1313 int vo_osd_changed(int new_value
)
1315 mp_osd_obj_t
* obj
=vo_osd_list
;
1316 int ret
= vo_osd_changed_status
;
1317 vo_osd_changed_status
= new_value
;
1320 if(obj
->type
==new_value
) obj
->flags
|=OSDFLAG_FORCE_UPDATE
;
1327 // BBBBBBBBBBBB AAAAAAAAAAAAA BBBBBBBBBBB
1328 // BBBBBBBBBBBB BBBBBBBBBBBBB
1331 // return TRUE if we have osd in the specified rectangular area:
1332 int vo_osd_check_range_update(int x1
,int y1
,int x2
,int y2
){
1333 mp_osd_obj_t
* obj
=vo_osd_list
;
1335 if(obj
->flags
&OSDFLAG_VISIBLE
){
1336 if( (obj
->bbox
.x1
<=x2
&& obj
->bbox
.x2
>=x1
) &&
1337 (obj
->bbox
.y1
<=y2
&& obj
->bbox
.y2
>=y1
) &&
1338 obj
->bbox
.y2
> obj
->bbox
.y1
&& obj
->bbox
.x2
> obj
->bbox
.x1