12 #include "stream/stream.h"
13 #include "stream/stream_dvdnav.h"
14 #define OSD_NAV_BOX_ALPHA 0x7f
17 #ifdef HAVE_TV_TELETEXT
18 #include "stream/tv.h"
19 #include "osdep/timer.h"
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 HAVE_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
) {
233 obj
->bbox
.x1
= obj
->x
= nav_hl
.sx
;
234 obj
->bbox
.y1
= obj
->y
= nav_hl
.sy
;
235 obj
->bbox
.x2
= nav_hl
.ex
;
236 obj
->bbox
.y2
= nav_hl
.ey
;
239 len
= obj
->stride
* (obj
->bbox
.y2
- obj
->bbox
.y1
);
240 memset (obj
->bitmap_buffer
, OSD_NAV_BOX_ALPHA
, len
);
241 memset (obj
->alpha_buffer
, OSD_NAV_BOX_ALPHA
, len
);
242 obj
->flags
|= OSDFLAG_BBOX
| OSDFLAG_CHANGED
;
243 if (obj
->bbox
.y2
> obj
->bbox
.y1
&& obj
->bbox
.x2
> obj
->bbox
.x1
)
244 obj
->flags
|= OSDFLAG_VISIBLE
;
248 #ifdef HAVE_TV_TELETEXT
249 // renders char to a big per-object buffer where alpha and bitmap are separated
250 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
)
252 int dststride
= obj
->stride
;
253 int dstskip
= obj
->stride
-w
;
254 int srcskip
= stride
-w
;
256 unsigned char *b
= obj
->bitmap_buffer
+ (y0
-obj
->bbox
.y1
)*dststride
+ (x0
-obj
->bbox
.x1
);
257 unsigned char *a
= obj
->alpha_buffer
+ (y0
-obj
->bbox
.y1
)*dststride
+ (x0
-obj
->bbox
.x1
);
258 unsigned char *bs
= src
;
259 if (x0
< obj
->bbox
.x1
|| x0
+w
> obj
->bbox
.x2
|| y0
< obj
->bbox
.y1
|| y0
+h
> obj
->bbox
.y2
) {
260 mp_msg(MSGT_OSD
,MSGL_ERR
,"tt osd text out of range: bbox [%d %d %d %d], txt [%d %d %d %d]\n",
261 obj
->bbox
.x1
, obj
->bbox
.x2
, obj
->bbox
.y1
, obj
->bbox
.y2
,
265 for (i
= 0; i
< h
; i
++) {
266 for (j
= 0; j
< w
; j
++, b
++, a
++, bs
++) {
267 *b
=(fg
-bg
)*(*bs
)/255+bg
;
275 inline static void vo_update_text_teletext(mp_osd_obj_t
*obj
, int dxs
, int dys
)
277 int h
=0,w
=0,i
,j
,font
,flashon
;
285 int start_row
,max_rows
;
286 int b
,ax
[6],ay
[6],aw
[6],ah
[6];
288 tt_char
* tdp
=vo_osd_teletext_page
;
289 unsigned char colors
[8]={1,85,150,226,70,105,179,254};
290 unsigned char* buf
[9];
292 obj
->flags
|=OSDFLAG_CHANGED
|OSDFLAG_VISIBLE
;
293 if (!tdp
|| !vo_osd_teletext_mode
) {
294 obj
->flags
&=~OSDFLAG_VISIBLE
;
297 flashon
=(GetTimer()/1000000)%2;
298 switch(vo_osd_teletext_half
){
299 case TT_ZOOM_TOP_HALF
:
303 case TT_ZOOM_BOTTOM_HALF
:
304 start_row
=VBI_ROWS
/2;
313 for(i
=start_row
;i
<max_rows
;i
++){
314 for(j
=0;j
<VBI_COLUMNS
;j
++){
315 tc
=tdp
[i
*VBI_COLUMNS
+j
];
316 if(!tc
.ctl
&& !tc
.gfx
)
318 render_one_glyph(vo_font
, tc
.unicode
);
319 if (wm
<vo_font
->width
[tc
.unicode
])
320 wm
=vo_font
->width
[tc
.unicode
];
325 hm
=vo_font
->height
+1;
326 wm
=dxs
*hm
*max_rows
/(dys
*VBI_COLUMNS
);
328 //very simple teletext font auto scaling
329 if(!vo_osd_teletext_scale
&& hm
*(max_rows
+1)>dys
){
330 text_font_scale_factor
*=1.0*(dys
)/((max_rows
+1)*hm
);
332 vo_osd_teletext_scale
=text_font_scale_factor
;
333 obj
->flags
&=~OSDFLAG_VISIBLE
;
344 w
=cols
*wm
-vo_font
->charspace
;
345 h
=rows
*hm
-vo_font
->charspace
;
385 obj
->flags
|= OSDFLAG_BBOX
;
389 buf
[i
]=malloc(wm
*hm
);
392 if(vo_osd_teletext_format
==TT_FORMAT_OPAQUE
||vo_osd_teletext_format
==TT_FORMAT_OPAQUE_INV
)
396 memset(buf
[8],color
,wm
*hm
);
398 if(vo_osd_teletext_format
==TT_FORMAT_OPAQUE
||vo_osd_teletext_format
==TT_FORMAT_TRANSPARENT
){
400 memset(buf
[i
],(unsigned char)(1.0*(255-color
)*colors
[i
]/255),wm
*hm
);
404 memset(buf
[i
],(unsigned char)(1.0*(255-color
)*colors
[7-i
]/255),wm
*hm
);
411 tc
=tdp
[(i
+start_row
)*VBI_COLUMNS
+j
];
412 if (tc
.hidden
) { x
+=wm
; continue;}
413 if(!tc
.gfx
|| (tc
.flh
&& !flashon
)){
414 /* Rendering one text character */
415 draw_alpha_buf(obj
,x
,y
,wm
,hm
,buf
[tc
.bg
],buf
[8],wm
);
416 if(tc
.unicode
!=0x20 && tc
.unicode
!=0x00 && !tc
.ctl
&&
417 (!tc
.flh
|| flashon
) &&
418 (font
=vo_font
->font
[tc
.unicode
])>=0 && y
+hm
<dys
){
419 tt_draw_alpha_buf(obj
,x
,y
,vo_font
->width
[tc
.unicode
],vo_font
->height
,
420 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[tc
.unicode
]-vo_font
->charspace
*vo_font
->pic_a
[font
]->w
,
421 vo_font
->pic_b
[font
]->w
,
422 buf
[tc
.fg
][0],buf
[tc
.bg
][0],buf
[8][0]);
426 Rendering one graphics character
427 TODO: support for separated graphics symbols (where six rectangles does not touch each other)
431 |10| <= |34| <= 00100110 <= 0x26
435 (0:wm/2) (wm/2:wm-wm/2)
437 ********** *********** (0:hm/3)
439 *** 1 **** **** 2 ****
441 ********** ***********
442 ********** ***********
444 ********** *********** (hm/3:hm-2*hm/3)
445 ********** ***********
447 *** 3 **** **** 4 ****
449 ********** ***********
450 ********** ***********
451 ********** ***********
453 ********** *********** (hm-hm/3:hm/3)
455 *** 5 **** **** 6 ****
457 ********** ***********
458 ********** ***********
461 if(tc
.gfx
>1){ //separated gfx
463 color
=(tc
.unicode
>>b
)&1?tc
.fg
:tc
.bg
;
464 draw_alpha_buf(obj
,x
+ax
[b
]+1,y
+ay
[b
]+1,aw
[b
]-2,ah
[b
]-2,buf
[color
],buf
[8],wm
);
466 //separated gfx (background borders)
468 draw_alpha_buf(obj
,x
,y
,1,hm
,buf
[tc
.bg
],buf
[8],wm
);
469 draw_alpha_buf(obj
,x
+ax
[1]-1,y
,2,hm
,buf
[tc
.bg
],buf
[8],wm
);
470 draw_alpha_buf(obj
,x
+ax
[1]+aw
[1]-1,y
,wm
-ax
[1]-aw
[1]+1,hm
,buf
[tc
.bg
],buf
[8],wm
);
472 draw_alpha_buf(obj
,x
,y
,wm
,1,buf
[tc
.bg
],buf
[8],wm
);
473 draw_alpha_buf(obj
,x
,y
+ay
[0]+ah
[0]-1,wm
,2,buf
[tc
.bg
],buf
[8],wm
);
474 draw_alpha_buf(obj
,x
,y
+ay
[2]+ah
[2]-1,wm
,2,buf
[tc
.bg
],buf
[8],wm
);
475 draw_alpha_buf(obj
,x
,y
+ay
[4]+ah
[4]-1,wm
,hm
-ay
[4]-ah
[4]+1,buf
[tc
.bg
],buf
[8],wm
);
478 color
=(tc
.unicode
>>b
)&1?tc
.fg
:tc
.bg
;
479 draw_alpha_buf(obj
,x
+ax
[b
],y
+ay
[b
],aw
[b
],ah
[b
],buf
[color
],buf
[8],wm
);
492 int vo_osd_progbar_type
=-1;
493 int vo_osd_progbar_value
=100; // 0..256
495 // if we have n=256 bars then OSD progbar looks like below
497 // 0 1 2 3 ... 256 <= vo_osd_progbar_value
499 // [ === === === ... === ]
501 // the above schema is rescalled to n=elems bars
503 inline static void vo_update_text_progbar(mp_osd_obj_t
* obj
,int dxs
,int dys
){
505 obj
->flags
|=OSDFLAG_CHANGED
|OSDFLAG_VISIBLE
;
507 if(vo_osd_progbar_type
<0 || !vo_font
){
508 obj
->flags
&=~OSDFLAG_VISIBLE
;
512 render_one_glyph(vo_font
, OSD_PB_START
);
513 render_one_glyph(vo_font
, OSD_PB_END
);
514 render_one_glyph(vo_font
, OSD_PB_0
);
515 render_one_glyph(vo_font
, OSD_PB_1
);
516 render_one_glyph(vo_font
, vo_osd_progbar_type
);
518 // calculate bbox corners:
520 int y
=(dys
-vo_font
->height
)/2;
521 int delimw
=vo_font
->width
[OSD_PB_START
]
522 +vo_font
->width
[OSD_PB_END
]
524 int width
=(2*dxs
-3*delimw
)/3;
525 int charw
=vo_font
->width
[OSD_PB_0
]+vo_font
->charspace
;
526 int elems
=width
/charw
;
527 int x
=(dxs
-elems
*charw
-delimw
)/2;
529 h
=get_height(OSD_PB_START
,h
);
530 h
=get_height(OSD_PB_END
,h
);
531 h
=get_height(OSD_PB_0
,h
);
532 h
=get_height(OSD_PB_1
,h
);
533 if (vo_osd_progbar_type
>0 && vo_font
->font
[vo_osd_progbar_type
]>=0){
534 delta
= vo_font
->width
[vo_osd_progbar_type
]+vo_font
->spacewidth
;
535 delta
= (x
-delta
> 0) ? delta
: x
;
536 h
=get_height(vo_osd_progbar_type
,h
);
538 obj
->bbox
.x1
=obj
->x
=x
;
539 obj
->bbox
.y1
=obj
->y
=y
;
540 obj
->bbox
.x2
=x
+width
+delimw
;
541 obj
->bbox
.y2
=y
+h
; //vo_font->height;
542 obj
->flags
|=OSDFLAG_BBOX
;
543 obj
->params
.progbar
.elems
=elems
;
544 obj
->bbox
.x1
-=delta
; // space for an icon
550 int minw
= vo_font
->width
[OSD_PB_START
]+vo_font
->width
[OSD_PB_END
]+vo_font
->width
[OSD_PB_0
];
551 if (vo_osd_progbar_type
>0 && vo_font
->font
[vo_osd_progbar_type
]>=0){
552 minw
+= vo_font
->width
[vo_osd_progbar_type
]+vo_font
->charspace
+vo_font
->spacewidth
;
554 if (obj
->bbox
.x2
- obj
->bbox
.x1
< minw
) return; // space too small, don't render anything
564 int charw
=vo_font
->width
[OSD_PB_0
]+vo_font
->charspace
;
565 int elems
=obj
->params
.progbar
.elems
;
567 if (vo_osd_progbar_value
<=0)
570 int ev
=vo_osd_progbar_value
*elems
;
572 if (ev
& 0xFF) mark
++;
573 if (mark
>elems
) mark
=elems
;
577 // printf("osd.progbar width=%d xpos=%d\n",width,x);
579 c
=vo_osd_progbar_type
;
580 if(vo_osd_progbar_type
>0 && (font
=vo_font
->font
[c
])>=0) {
581 int xp
=x
-vo_font
->width
[c
]-vo_font
->spacewidth
;
582 draw_alpha_buf(obj
,(xp
<0?0:xp
),y
,
584 vo_font
->pic_a
[font
]->h
,
585 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
586 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
587 vo_font
->pic_a
[font
]->w
);
591 if ((font
=vo_font
->font
[c
])>=0)
592 draw_alpha_buf(obj
,x
,y
,
594 vo_font
->pic_a
[font
]->h
,
595 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
596 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
597 vo_font
->pic_a
[font
]->w
);
598 x
+=vo_font
->width
[c
]+vo_font
->charspace
;
601 if ((font
=vo_font
->font
[c
])>=0){
603 h
=vo_font
->pic_a
[font
]->h
;
604 s
=vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
];
605 sa
=vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
];
606 st
=vo_font
->pic_a
[font
]->w
;
608 draw_alpha_buf(obj
,x
,y
,w
,h
,s
,sa
,st
);
614 if ((font
=vo_font
->font
[c
])>=0){
616 h
=vo_font
->pic_a
[font
]->h
;
617 s
=vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
];
618 sa
=vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
];
619 st
=vo_font
->pic_a
[font
]->w
;
620 if ((i
=elems
-mark
)) do {
621 draw_alpha_buf(obj
,x
,y
,w
,h
,s
,sa
,st
);
627 if ((font
=vo_font
->font
[c
])>=0)
628 draw_alpha_buf(obj
,x
,y
,
630 vo_font
->pic_a
[font
]->h
,
631 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
632 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
633 vo_font
->pic_a
[font
]->w
);
634 // x+=vo_font->width[c]+vo_font->charspace;
637 // vo_osd_progbar_value=(vo_osd_progbar_value+1)&0xFF;
641 subtitle
* vo_sub
=NULL
;
643 // vo_draw_text_sub(int dxs,int dys,void (*draw_alpha)(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride))
645 inline static void vo_update_text_sub(struct osd_state
*osd
, mp_osd_obj_t
* obj
,int dxs
,int dys
){
647 int c
,i
,j
,l
,x
,y
,font
,prevc
,counter
;
649 int lastStripPosition
;
654 struct font_desc
*sub_font
= osd
->sub_font
;
656 obj
->flags
|=OSDFLAG_CHANGED
|OSDFLAG_VISIBLE
;
658 if(!vo_sub
|| !osd
->sub_font
|| !sub_visibility
|| (sub_font
->font
[40]<0)){
659 obj
->flags
&=~OSDFLAG_VISIBLE
;
663 obj
->bbox
.y2
=obj
->y
=dys
;
664 obj
->params
.subtitle
.lines
=0;
666 // too long lines divide into a smaller ones
669 lastStripPosition
=-1;
673 struct osd_text_t
*osl
, *cp_ott
, *tmp_ott
, *tmp
;
674 struct osd_text_p
*otp_sub
= NULL
, *otp_sub_tmp
, // these are used to store the whole sub text osd
675 *otp
, *tmp_otp
, *pmt
; // these are used to manage sub text osd coming from a single sub line
676 int *char_seq
, char_position
, xlimit
= dxs
* sub_width_p
/ 100, counter
;
679 xsize
= -sub_font
->charspace
;
683 char_seq
= calloc(strlen(t
), sizeof(int));
691 // reading the subtitle words from vo_sub->text[]
694 c
= utf8_get_char(&t
);
695 else if ((c
= *t
++) >= 0x80 && sub_unicode
)
698 t
+= strlen(t
); // end here
699 mp_msg(MSGT_OSD
,MSGL_WARN
,"\nMAX_UCS exceeded!\n");
701 if (!c
) c
++; // avoid UCS 0
702 render_one_glyph(sub_font
, c
);
705 struct osd_text_t
*tmp_ott
= (struct osd_text_t
*) calloc(1, sizeof(struct osd_text_t
));
708 osl
= cp_ott
= tmp_ott
;
710 tmp_ott
->prev
= cp_ott
;
711 cp_ott
->next
= tmp_ott
;
712 tmp_ott
->osd_kerning
=
713 sub_font
->charspace
+ sub_font
->width
[' '];
716 tmp_ott
->osd_length
= xsize
;
717 tmp_ott
->text_length
= char_position
;
718 tmp_ott
->text
= (int *) malloc(char_position
* sizeof(int));
719 for (counter
= 0; counter
< char_position
; ++counter
)
720 tmp_ott
->text
[counter
] = char_seq
[counter
];
725 int delta_xsize
= sub_font
->width
[c
] + sub_font
->charspace
+ kerning(sub_font
, prevc
, c
);
727 if (xsize
+ delta_xsize
<= dxs
) {
730 char_seq
[char_position
++] = c
;
731 xsize
+= delta_xsize
;
732 if ((!suboverlap_enabled
) && ((font
= sub_font
->font
[c
]) >= 0)) {
733 if (sub_font
->pic_a
[font
]->h
> h
) {
734 h
= sub_font
->pic_a
[font
]->h
;
739 mp_msg(MSGT_OSD
, MSGL_WARN
, "\nSubtitle word '%s' too long!\n", t
);
744 }// for len (all words from subtitle line read)
746 // osl holds an ordered (as they appear in the lines) chain of the subtitle words
748 struct osd_text_t
*tmp_ott
= (struct osd_text_t
*) calloc(1, sizeof(struct osd_text_t
));
751 osl
= cp_ott
= tmp_ott
;
753 tmp_ott
->prev
= cp_ott
;
754 cp_ott
->next
= tmp_ott
;
755 tmp_ott
->osd_kerning
=
756 sub_font
->charspace
+ sub_font
->width
[' '];
759 tmp_ott
->osd_length
= xsize
;
760 tmp_ott
->text_length
= char_position
;
761 tmp_ott
->text
= (int *) malloc(char_position
* sizeof(int));
762 for (counter
= 0; counter
< char_position
; ++counter
)
763 tmp_ott
->text
[counter
] = char_seq
[counter
];
765 xsize
= -sub_font
->charspace
;
770 int value
= 0, exit
= 0, minimum
= 0;
772 // otp will contain the chain of the osd subtitle lines coming from the single vo_sub line.
773 otp
= tmp_otp
= (struct osd_text_p
*) calloc(1, sizeof(struct osd_text_p
));
775 for (tmp_ott
= tmp_otp
->ott
; exit
== 0; ) {
777 value
+= tmp_ott
->osd_kerning
+ tmp_ott
->osd_length
;
778 tmp_ott
= tmp_ott
->next
;
779 } while ((tmp_ott
!= NULL
) && (value
+ tmp_ott
->osd_kerning
+ tmp_ott
->osd_length
<= xlimit
));
780 if (tmp_ott
!= NULL
) {
781 struct osd_text_p
*tmp
= (struct osd_text_p
*) calloc(1, sizeof(struct osd_text_p
));
783 tmp_otp
->value
= value
;
787 tmp_otp
->ott
= tmp_ott
;
788 value
= -2 * sub_font
->charspace
- sub_font
->width
[' '];
790 tmp_otp
->value
= value
;
797 // minimum holds the 'sum of the differences in length among the lines',
798 // a measure of the evenness of the lengths of the lines
799 for (tmp_otp
= otp
; tmp_otp
->next
!= NULL
; tmp_otp
= tmp_otp
->next
) {
801 while (pmt
!= NULL
) {
802 minimum
+= abs(tmp_otp
->value
- pmt
->value
);
807 if (otp
->next
!= NULL
) {
809 struct osd_text_p
*mem
, *hold
;
812 // until the last word of a line can be moved to the beginning of following line
813 // reducing the 'sum of the differences in length among the lines', it is done
817 for (tmp_otp
= otp
; tmp_otp
->next
!= NULL
; tmp_otp
= tmp_otp
->next
) {
819 for (tmp
= tmp_otp
->ott
; tmp
->next
!= pmt
->ott
; tmp
= tmp
->next
);
820 if (pmt
->value
+ tmp
->osd_length
+ pmt
->ott
->osd_kerning
<= xlimit
) {
821 mem1
= tmp_otp
->value
;
823 tmp_otp
->value
= mem1
- tmp
->osd_length
- tmp
->osd_kerning
;
824 pmt
->value
= mem2
+ tmp
->osd_length
+ pmt
->ott
->osd_kerning
;
827 for (mem
= otp
; mem
->next
!= NULL
; mem
= mem
->next
) {
829 while (pmt
!= NULL
) {
830 value
+= abs(mem
->value
- pmt
->value
);
834 if (value
< minimum
) {
839 tmp_otp
->value
= mem1
;
840 tmp_otp
->next
->value
= mem2
;
847 for (tmp
= tmp_otp
->ott
; tmp
->next
!= pmt
->ott
; tmp
= tmp
->next
);
848 mem1
= tmp_otp
->value
;
850 tmp_otp
->value
= mem1
- tmp
->osd_length
- tmp
->osd_kerning
;
851 pmt
->value
= mem2
+ tmp
->osd_length
+ pmt
->ott
->osd_kerning
;
855 }//~if(otp->next!=NULL)
858 // adding otp (containing splitted lines) to otp chain
859 if (otp_sub
== NULL
) {
861 for (otp_sub_tmp
= otp_sub
; otp_sub_tmp
->next
!= NULL
; otp_sub_tmp
= otp_sub_tmp
->next
);
865 while (tmp
->next
!= NULL
) tmp
= tmp
->next
;
866 tmp
->next
= otp
->ott
;
867 otp
->ott
->prev
= tmp
;
868 //attaching new subtitle line at the end
869 otp_sub_tmp
->next
= otp
;
870 otp
->prev
= otp_sub_tmp
;
872 otp_sub_tmp
= otp_sub_tmp
->next
;
873 while (otp_sub_tmp
->next
!= NULL
);
878 // write lines into utbl
882 obj
->params
.subtitle
.lines
= 0;
883 for (tmp_otp
= otp_sub
; tmp_otp
!= NULL
; tmp_otp
= tmp_otp
->next
) {
885 if ((obj
->params
.subtitle
.lines
++) >= MAX_UCSLINES
)
888 if (h
> obj
->y
) { // out of the screen so end parsing
889 obj
->y
-= lasth
- sub_font
->height
; // correct the y position
892 xsize
= tmp_otp
->value
;
893 obj
->params
.subtitle
.xtbl
[xtblc
++] = (dxs
- xsize
) / 2;
894 if (xmin
> (dxs
- xsize
) / 2)
895 xmin
= (dxs
- xsize
) / 2;
896 if (xmax
< (dxs
+ xsize
) / 2)
897 xmax
= (dxs
+ xsize
) / 2;
899 tmp
= (tmp_otp
->next
== NULL
) ? NULL
: tmp_otp
->next
->ott
;
900 for (tmp_ott
= tmp_otp
->ott
; tmp_ott
!= tmp
; tmp_ott
= tmp_ott
->next
) {
901 for (counter
= 0; counter
< tmp_ott
->text_length
; ++counter
) {
902 if (utblc
> MAX_UCS
) {
905 c
= tmp_ott
->text
[counter
];
906 render_one_glyph(sub_font
, c
);
907 obj
->params
.subtitle
.utbl
[utblc
++] = c
;
910 obj
->params
.subtitle
.utbl
[utblc
++] = ' ';
912 obj
->params
.subtitle
.utbl
[utblc
- 1] = 0;
913 obj
->y
-= sub_font
->height
;
915 if(obj
->params
.subtitle
.lines
)
916 obj
->y
= dys
- ((obj
->params
.subtitle
.lines
- 1) * sub_font
->height
+ sub_font
->pic_a
[sub_font
->font
[40]]->h
);
919 if (otp_sub
!= NULL
) {
920 for (tmp
= otp_sub
->ott
; tmp
->next
!= NULL
; free(tmp
->prev
)) {
927 for(pmt
= otp_sub
; pmt
->next
!= NULL
; free(pmt
->prev
)) {
934 /// vertical alignment
936 if (sub_alignment
== 2)
937 obj
->y
= dys
* sub_pos
/ 100 - h
;
938 else if (sub_alignment
== 1)
939 obj
->y
= dys
* sub_pos
/ 100 - h
/ 2;
941 obj
->y
= dys
* sub_pos
/ 100;
945 if (obj
->y
> dys
- h
)
948 obj
->bbox
.y2
= obj
->y
+ h
;
951 if (sub_justify
) xmin
= 10;
955 // obj->bbox.y2=obj->y+obj->params.subtitle.lines*sub_font->height;
956 obj
->flags
|=OSDFLAG_BBOX
;
963 switch(vo_sub
->alignment
) {
964 case SUB_ALIGNMENT_BOTTOMLEFT
:
965 case SUB_ALIGNMENT_MIDDLELEFT
:
966 case SUB_ALIGNMENT_TOPLEFT
:
967 obj
->alignment
|= 0x1;
969 case SUB_ALIGNMENT_BOTTOMRIGHT
:
970 case SUB_ALIGNMENT_MIDDLERIGHT
:
971 case SUB_ALIGNMENT_TOPRIGHT
:
972 obj
->alignment
|= 0x2;
974 case SUB_ALIGNMENT_BOTTOMCENTER
:
975 case SUB_ALIGNMENT_MIDDLECENTER
:
976 case SUB_ALIGNMENT_TOPCENTER
:
978 obj
->alignment
|= 0x0;
982 if ((l
= obj
->params
.subtitle
.lines
)) {
983 for(counter
= dxs
; i
< l
; ++i
)
984 if (obj
->params
.subtitle
.xtbl
[i
] < counter
) counter
= obj
->params
.subtitle
.xtbl
[i
];
985 for (i
= 0; i
< l
; ++i
) {
986 switch (obj
->alignment
&0x3) {
993 x
= 2 * obj
->params
.subtitle
.xtbl
[i
] - counter
- ((obj
->params
.subtitle
.xtbl
[i
] == counter
) ? 0 : 1);
997 x
= obj
->params
.subtitle
.xtbl
[i
];
1000 while ((c
=obj
->params
.subtitle
.utbl
[j
++])){
1001 x
+= kerning(sub_font
,prevc
,c
);
1002 if ((font
=sub_font
->font
[c
])>=0)
1003 draw_alpha_buf(obj
,x
,y
,
1005 sub_font
->pic_a
[font
]->h
+y
<obj
->dys
? sub_font
->pic_a
[font
]->h
: obj
->dys
-y
,
1006 sub_font
->pic_b
[font
]->bmp
+sub_font
->start
[c
],
1007 sub_font
->pic_a
[font
]->bmp
+sub_font
->start
[c
],
1008 sub_font
->pic_a
[font
]->w
);
1009 x
+=sub_font
->width
[c
]+sub_font
->charspace
;
1012 y
+=sub_font
->height
;
1018 inline static void vo_update_spudec_sub(mp_osd_obj_t
* obj
, int dxs
, int dys
)
1020 unsigned int bbox
[4];
1021 spudec_calc_bbox(vo_spudec
, dxs
, dys
, bbox
);
1022 obj
->bbox
.x1
= bbox
[0];
1023 obj
->bbox
.x2
= bbox
[1];
1024 obj
->bbox
.y1
= bbox
[2];
1025 obj
->bbox
.y2
= bbox
[3];
1026 obj
->flags
|= OSDFLAG_BBOX
;
1029 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
)
1031 spudec_draw_scaled(vo_spudec
, obj
->dxs
, obj
->dys
, draw_alpha
, ctx
);
1034 void *vo_spudec
=NULL
;
1035 void *vo_vobsub
=NULL
;
1037 static int draw_alpha_init_flag
=0;
1039 extern void vo_draw_alpha_init(void);
1041 mp_osd_obj_t
* vo_osd_list
=NULL
;
1043 static mp_osd_obj_t
* new_osd_obj(int type
){
1044 mp_osd_obj_t
* osd
=malloc(sizeof(mp_osd_obj_t
));
1045 memset(osd
,0,sizeof(mp_osd_obj_t
));
1046 osd
->next
=vo_osd_list
;
1049 osd
->alpha_buffer
= NULL
;
1050 osd
->bitmap_buffer
= NULL
;
1051 osd
->allocated
= -1;
1055 void osd_free(struct osd_state
*osd
)
1057 mp_osd_obj_t
* obj
=vo_osd_list
;
1059 mp_osd_obj_t
* next
=obj
->next
;
1060 if (obj
->alpha_buffer
) free(obj
->alpha_buffer
);
1061 if (obj
->bitmap_buffer
) free(obj
->bitmap_buffer
);
1069 #define FONT_LOAD_DEFER 6
1071 int osd_update(struct osd_state
*osd
, int dxs
, int dys
)
1073 mp_osd_obj_t
* obj
=vo_osd_list
;
1075 #ifdef HAVE_FREETYPE
1076 static int defer_counter
= 0, prev_dxs
= 0, prev_dys
= 0;
1079 #ifdef HAVE_FREETYPE
1080 // here is the right place to get screen dimensions
1081 if (((dxs
!= vo_image_width
)
1082 && (subtitle_autoscale
== 2 || subtitle_autoscale
== 3))
1083 || ((dys
!= vo_image_height
)
1084 && (subtitle_autoscale
== 1 || subtitle_autoscale
== 3)))
1086 // screen dimensions changed
1087 // wait a while to avoid useless reloading of the font
1088 if (dxs
== prev_dxs
|| dys
== prev_dys
) {
1095 if (defer_counter
>= FONT_LOAD_DEFER
) force_load_font
= 1;
1098 if (force_load_font
) {
1099 force_load_font
= 0;
1100 load_font_ft(dxs
, dys
, &vo_font
, font_name
, osd_font_scale_factor
);
1102 load_font_ft(dxs
, dys
, &osd
->sub_font
, sub_font_name
, text_font_scale_factor
);
1104 load_font_ft(dxs
, dys
, &osd
->sub_font
, font_name
, text_font_scale_factor
);
1110 load_font_ft(dxs
, dys
, &vo_font
, font_name
, osd_font_scale_factor
);
1111 if (!osd
->sub_font
) {
1113 load_font_ft(dxs
, dys
, &osd
->sub_font
, sub_font_name
, text_font_scale_factor
);
1115 load_font_ft(dxs
, dys
, &osd
->sub_font
, font_name
, text_font_scale_factor
);
1121 if(dxs
!=obj
->dxs
|| dys
!=obj
->dys
|| obj
->flags
&OSDFLAG_FORCE_UPDATE
){
1122 int vis
=obj
->flags
&OSDFLAG_VISIBLE
;
1123 obj
->flags
&=~OSDFLAG_BBOX
;
1126 case OSDTYPE_DVDNAV
:
1127 vo_update_nav(obj
,dxs
,dys
);
1130 case OSDTYPE_SUBTITLE
:
1131 vo_update_text_sub(osd
, obj
,dxs
,dys
);
1133 #ifdef HAVE_TV_TELETEXT
1134 case OSDTYPE_TELETEXT
:
1135 vo_update_text_teletext(obj
,dxs
,dys
);
1138 case OSDTYPE_PROGBAR
:
1139 vo_update_text_progbar(obj
,dxs
,dys
);
1142 if(sub_visibility
&& vo_spudec
&& spudec_visible(vo_spudec
)){
1143 vo_update_spudec_sub(obj
, dxs
, dys
);
1144 obj
->flags
|=OSDFLAG_VISIBLE
|OSDFLAG_CHANGED
;
1147 obj
->flags
&=~OSDFLAG_VISIBLE
;
1150 if(vo_font
&& osd
->osd_text
[0]){
1151 vo_update_text_osd(osd
, obj
, dxs
, dys
); // update bbox
1152 obj
->flags
|=OSDFLAG_VISIBLE
|OSDFLAG_CHANGED
;
1154 obj
->flags
&=~OSDFLAG_VISIBLE
;
1158 if(!(obj
->flags
&OSDFLAG_BBOX
)){
1159 // we don't know, so assume the whole screen changed :(
1160 obj
->bbox
.x1
=obj
->bbox
.y1
=0;
1163 obj
->flags
|=OSDFLAG_BBOX
;
1165 // check bbox, reduce it if it's out of bounds (corners):
1166 if(obj
->bbox
.x1
<0) obj
->bbox
.x1
=0;
1167 if(obj
->bbox
.y1
<0) obj
->bbox
.y1
=0;
1168 if(obj
->bbox
.x2
>dxs
) obj
->bbox
.x2
=dxs
;
1169 if(obj
->bbox
.y2
>dys
) obj
->bbox
.y2
=dys
;
1170 if(obj
->flags
&OSDFLAG_VISIBLE
)
1172 mp_msg(MSGT_OSD
,MSGL_DBG2
,"OSD update: %d;%d %dx%d \n",
1173 obj
->bbox
.x1
,obj
->bbox
.y1
,obj
->bbox
.x2
-obj
->bbox
.x1
,
1174 obj
->bbox
.y2
-obj
->bbox
.y1
);
1176 // check if visibility changed:
1177 if(vis
!= (obj
->flags
&OSDFLAG_VISIBLE
) ) obj
->flags
|=OSDFLAG_CHANGED
;
1178 // remove the cause of automatic update:
1179 obj
->dxs
=dxs
; obj
->dys
=dys
;
1180 obj
->flags
&=~OSDFLAG_FORCE_UPDATE
;
1182 if(obj
->flags
&OSDFLAG_CHANGED
){
1184 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
);
1191 struct osd_state
*osd_create(void)
1193 struct osd_state
*osd
= talloc_zero(NULL
, struct osd_state
);
1194 *osd
= (struct osd_state
){
1196 if(!draw_alpha_init_flag
){
1197 draw_alpha_init_flag
=1;
1198 vo_draw_alpha_init();
1200 // temp hack, should be moved to mplayer/mencoder later
1201 new_osd_obj(OSDTYPE_OSD
);
1202 new_osd_obj(OSDTYPE_SUBTITLE
);
1203 new_osd_obj(OSDTYPE_PROGBAR
);
1204 new_osd_obj(OSDTYPE_SPU
);
1206 new_osd_obj(OSDTYPE_DVDNAV
);
1208 #if HAVE_TV_TELETEXT
1209 new_osd_obj(OSDTYPE_TELETEXT
);
1211 #ifdef HAVE_FREETYPE
1212 force_load_font
= 1;
1217 int vo_osd_changed_flag
=0;
1219 void osd_remove_text(struct osd_state
*osd
, int dxs
, int dys
,
1220 void (*remove
)(int x0
, int y0
, int w
, int h
))
1222 mp_osd_obj_t
* obj
=vo_osd_list
;
1223 osd_update(osd
, dxs
, dys
);
1225 if(((obj
->flags
&OSDFLAG_CHANGED
) || (obj
->flags
&OSDFLAG_VISIBLE
)) &&
1226 (obj
->flags
&OSDFLAG_OLD_BBOX
)){
1227 int w
=obj
->old_bbox
.x2
-obj
->old_bbox
.x1
;
1228 int h
=obj
->old_bbox
.y2
-obj
->old_bbox
.y1
;
1230 vo_osd_changed_flag
=obj
->flags
&OSDFLAG_CHANGED
; // temp hack
1231 remove(obj
->old_bbox
.x1
,obj
->old_bbox
.y1
,w
,h
);
1233 // obj->flags&=~OSDFLAG_OLD_BBOX;
1239 void osd_draw_text(struct osd_state
*osd
, int dxs
, int dys
,
1240 void (*draw_alpha
)(void *ctx
, int x0
, int y0
, int w
, int h
,
1241 unsigned char* src
, unsigned char *srca
,
1245 mp_osd_obj_t
* obj
=vo_osd_list
;
1246 osd_update(osd
, dxs
, dys
);
1248 if(obj
->flags
&OSDFLAG_VISIBLE
){
1249 vo_osd_changed_flag
=obj
->flags
&OSDFLAG_CHANGED
; // temp hack
1252 vo_draw_spudec_sub(obj
, draw_alpha
, ctx
); // FIXME
1255 case OSDTYPE_DVDNAV
:
1257 #ifdef HAVE_TV_TELETEXT
1258 case OSDTYPE_TELETEXT
:
1261 case OSDTYPE_SUBTITLE
:
1262 case OSDTYPE_PROGBAR
:
1263 vo_draw_text_from_buffer(obj
, draw_alpha
, ctx
);
1266 obj
->old_bbox
=obj
->bbox
;
1267 obj
->flags
|=OSDFLAG_OLD_BBOX
;
1269 obj
->flags
&=~OSDFLAG_CHANGED
;
1274 static int vo_osd_changed_status
= 0;
1276 int vo_osd_changed(int new_value
)
1278 mp_osd_obj_t
* obj
=vo_osd_list
;
1279 int ret
= vo_osd_changed_status
;
1280 vo_osd_changed_status
= new_value
;
1283 if(obj
->type
==new_value
) obj
->flags
|=OSDFLAG_FORCE_UPDATE
;
1290 // BBBBBBBBBBBB AAAAAAAAAAAAA BBBBBBBBBBB
1291 // BBBBBBBBBBBB BBBBBBBBBBBBB
1294 // return TRUE if we have osd in the specified rectangular area:
1295 int vo_osd_check_range_update(int x1
,int y1
,int x2
,int y2
){
1296 mp_osd_obj_t
* obj
=vo_osd_list
;
1298 if(obj
->flags
&OSDFLAG_VISIBLE
){
1299 if( (obj
->bbox
.x1
<=x2
&& obj
->bbox
.x2
>=x1
) &&
1300 (obj
->bbox
.y1
<=y2
&& obj
->bbox
.y2
>=y1
) &&
1301 obj
->bbox
.y2
> obj
->bbox
.y1
&& obj
->bbox
.x2
> obj
->bbox
.x1