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
) {
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 CONFIG_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 inline static void vo_update_text_sub(struct osd_state
*osd
, mp_osd_obj_t
* obj
,int dxs
,int dys
){
645 int c
,i
,j
,l
,x
,y
,font
,prevc
,counter
;
647 int lastStripPosition
;
652 struct font_desc
*sub_font
= osd
->sub_font
;
654 obj
->flags
|=OSDFLAG_CHANGED
|OSDFLAG_VISIBLE
;
656 if(!vo_sub
|| !osd
->sub_font
|| !sub_visibility
|| (sub_font
->font
[40]<0)){
657 obj
->flags
&=~OSDFLAG_VISIBLE
;
661 obj
->bbox
.y2
=obj
->y
=dys
;
662 obj
->params
.subtitle
.lines
=0;
664 // too long lines divide into a smaller ones
667 lastStripPosition
=-1;
671 struct osd_text_t
*osl
, *cp_ott
, *tmp_ott
, *tmp
;
672 struct osd_text_p
*otp_sub
= NULL
, *otp_sub_tmp
, // these are used to store the whole sub text osd
673 *otp
, *tmp_otp
, *pmt
; // these are used to manage sub text osd coming from a single sub line
674 int *char_seq
, char_position
, xlimit
= dxs
* sub_width_p
/ 100, counter
;
677 xsize
= -sub_font
->charspace
;
681 char_seq
= calloc(strlen(t
), sizeof(int));
689 // reading the subtitle words from vo_sub->text[]
692 c
= utf8_get_char(&t
);
693 else if ((c
= *t
++) >= 0x80 && sub_unicode
)
696 t
+= strlen(t
); // end here
697 mp_msg(MSGT_OSD
,MSGL_WARN
,"\nMAX_UCS exceeded!\n");
699 if (!c
) c
++; // avoid UCS 0
700 render_one_glyph(sub_font
, c
);
703 struct osd_text_t
*tmp_ott
= (struct osd_text_t
*) calloc(1, sizeof(struct osd_text_t
));
706 osl
= cp_ott
= tmp_ott
;
708 tmp_ott
->prev
= cp_ott
;
709 cp_ott
->next
= tmp_ott
;
710 tmp_ott
->osd_kerning
=
711 sub_font
->charspace
+ sub_font
->width
[' '];
714 tmp_ott
->osd_length
= xsize
;
715 tmp_ott
->text_length
= char_position
;
716 tmp_ott
->text
= (int *) malloc(char_position
* sizeof(int));
717 for (counter
= 0; counter
< char_position
; ++counter
)
718 tmp_ott
->text
[counter
] = char_seq
[counter
];
723 int delta_xsize
= sub_font
->width
[c
] + sub_font
->charspace
+ kerning(sub_font
, prevc
, c
);
725 if (xsize
+ delta_xsize
<= dxs
) {
728 char_seq
[char_position
++] = c
;
729 xsize
+= delta_xsize
;
730 if ((!suboverlap_enabled
) && ((font
= sub_font
->font
[c
]) >= 0)) {
731 if (sub_font
->pic_a
[font
]->h
> h
) {
732 h
= sub_font
->pic_a
[font
]->h
;
737 mp_msg(MSGT_OSD
, MSGL_WARN
, "\nSubtitle word '%s' too long!\n", t
);
742 }// for len (all words from subtitle line read)
744 // osl holds an ordered (as they appear in the lines) chain of the subtitle words
746 struct osd_text_t
*tmp_ott
= (struct osd_text_t
*) calloc(1, sizeof(struct osd_text_t
));
749 osl
= cp_ott
= tmp_ott
;
751 tmp_ott
->prev
= cp_ott
;
752 cp_ott
->next
= tmp_ott
;
753 tmp_ott
->osd_kerning
=
754 sub_font
->charspace
+ sub_font
->width
[' '];
757 tmp_ott
->osd_length
= xsize
;
758 tmp_ott
->text_length
= char_position
;
759 tmp_ott
->text
= (int *) malloc(char_position
* sizeof(int));
760 for (counter
= 0; counter
< char_position
; ++counter
)
761 tmp_ott
->text
[counter
] = char_seq
[counter
];
763 xsize
= -sub_font
->charspace
;
768 int value
= 0, exit
= 0, minimum
= 0;
770 // otp will contain the chain of the osd subtitle lines coming from the single vo_sub line.
771 otp
= tmp_otp
= (struct osd_text_p
*) calloc(1, sizeof(struct osd_text_p
));
773 for (tmp_ott
= tmp_otp
->ott
; exit
== 0; ) {
775 value
+= tmp_ott
->osd_kerning
+ tmp_ott
->osd_length
;
776 tmp_ott
= tmp_ott
->next
;
777 } while ((tmp_ott
!= NULL
) && (value
+ tmp_ott
->osd_kerning
+ tmp_ott
->osd_length
<= xlimit
));
778 if (tmp_ott
!= NULL
) {
779 struct osd_text_p
*tmp
= (struct osd_text_p
*) calloc(1, sizeof(struct osd_text_p
));
781 tmp_otp
->value
= value
;
785 tmp_otp
->ott
= tmp_ott
;
786 value
= -2 * sub_font
->charspace
- sub_font
->width
[' '];
788 tmp_otp
->value
= value
;
795 // minimum holds the 'sum of the differences in length among the lines',
796 // a measure of the evenness of the lengths of the lines
797 for (tmp_otp
= otp
; tmp_otp
->next
!= NULL
; tmp_otp
= tmp_otp
->next
) {
799 while (pmt
!= NULL
) {
800 minimum
+= abs(tmp_otp
->value
- pmt
->value
);
805 if (otp
->next
!= NULL
) {
807 struct osd_text_p
*mem
, *hold
;
810 // until the last word of a line can be moved to the beginning of following line
811 // reducing the 'sum of the differences in length among the lines', it is done
815 for (tmp_otp
= otp
; tmp_otp
->next
!= NULL
; tmp_otp
= tmp_otp
->next
) {
817 for (tmp
= tmp_otp
->ott
; tmp
->next
!= pmt
->ott
; tmp
= tmp
->next
);
818 if (pmt
->value
+ tmp
->osd_length
+ pmt
->ott
->osd_kerning
<= xlimit
) {
819 mem1
= tmp_otp
->value
;
821 tmp_otp
->value
= mem1
- tmp
->osd_length
- tmp
->osd_kerning
;
822 pmt
->value
= mem2
+ tmp
->osd_length
+ pmt
->ott
->osd_kerning
;
825 for (mem
= otp
; mem
->next
!= NULL
; mem
= mem
->next
) {
827 while (pmt
!= NULL
) {
828 value
+= abs(mem
->value
- pmt
->value
);
832 if (value
< minimum
) {
837 tmp_otp
->value
= mem1
;
838 tmp_otp
->next
->value
= mem2
;
845 for (tmp
= tmp_otp
->ott
; tmp
->next
!= pmt
->ott
; tmp
= tmp
->next
);
846 mem1
= tmp_otp
->value
;
848 tmp_otp
->value
= mem1
- tmp
->osd_length
- tmp
->osd_kerning
;
849 pmt
->value
= mem2
+ tmp
->osd_length
+ pmt
->ott
->osd_kerning
;
853 }//~if(otp->next!=NULL)
856 // adding otp (containing splitted lines) to otp chain
857 if (otp_sub
== NULL
) {
859 for (otp_sub_tmp
= otp_sub
; otp_sub_tmp
->next
!= NULL
; otp_sub_tmp
= otp_sub_tmp
->next
);
863 while (tmp
->next
!= NULL
) tmp
= tmp
->next
;
864 tmp
->next
= otp
->ott
;
865 otp
->ott
->prev
= tmp
;
866 //attaching new subtitle line at the end
867 otp_sub_tmp
->next
= otp
;
868 otp
->prev
= otp_sub_tmp
;
870 otp_sub_tmp
= otp_sub_tmp
->next
;
871 while (otp_sub_tmp
->next
!= NULL
);
876 // write lines into utbl
880 obj
->params
.subtitle
.lines
= 0;
881 for (tmp_otp
= otp_sub
; tmp_otp
!= NULL
; tmp_otp
= tmp_otp
->next
) {
883 if ((obj
->params
.subtitle
.lines
++) >= MAX_UCSLINES
)
886 if (h
> obj
->y
) { // out of the screen so end parsing
887 obj
->y
-= lasth
- sub_font
->height
; // correct the y position
890 xsize
= tmp_otp
->value
;
891 obj
->params
.subtitle
.xtbl
[xtblc
++] = (dxs
- xsize
) / 2;
892 if (xmin
> (dxs
- xsize
) / 2)
893 xmin
= (dxs
- xsize
) / 2;
894 if (xmax
< (dxs
+ xsize
) / 2)
895 xmax
= (dxs
+ xsize
) / 2;
897 tmp
= (tmp_otp
->next
== NULL
) ? NULL
: tmp_otp
->next
->ott
;
898 for (tmp_ott
= tmp_otp
->ott
; tmp_ott
!= tmp
; tmp_ott
= tmp_ott
->next
) {
899 for (counter
= 0; counter
< tmp_ott
->text_length
; ++counter
) {
900 if (utblc
> MAX_UCS
) {
903 c
= tmp_ott
->text
[counter
];
904 render_one_glyph(sub_font
, c
);
905 obj
->params
.subtitle
.utbl
[utblc
++] = c
;
908 obj
->params
.subtitle
.utbl
[utblc
++] = ' ';
910 obj
->params
.subtitle
.utbl
[utblc
- 1] = 0;
911 obj
->y
-= sub_font
->height
;
913 if(obj
->params
.subtitle
.lines
)
914 obj
->y
= dys
- ((obj
->params
.subtitle
.lines
- 1) * sub_font
->height
+ sub_font
->pic_a
[sub_font
->font
[40]]->h
);
917 if (otp_sub
!= NULL
) {
918 for (tmp
= otp_sub
->ott
; tmp
->next
!= NULL
; free(tmp
->prev
)) {
925 for(pmt
= otp_sub
; pmt
->next
!= NULL
; free(pmt
->prev
)) {
932 /// vertical alignment
934 if (sub_alignment
== 2)
935 obj
->y
= dys
* sub_pos
/ 100 - h
;
936 else if (sub_alignment
== 1)
937 obj
->y
= dys
* sub_pos
/ 100 - h
/ 2;
939 obj
->y
= dys
* sub_pos
/ 100;
943 if (obj
->y
> dys
- h
)
946 obj
->bbox
.y2
= obj
->y
+ h
;
949 if (sub_justify
) xmin
= 10;
953 // obj->bbox.y2=obj->y+obj->params.subtitle.lines*sub_font->height;
954 obj
->flags
|=OSDFLAG_BBOX
;
961 switch(vo_sub
->alignment
) {
962 case SUB_ALIGNMENT_BOTTOMLEFT
:
963 case SUB_ALIGNMENT_MIDDLELEFT
:
964 case SUB_ALIGNMENT_TOPLEFT
:
965 obj
->alignment
|= 0x1;
967 case SUB_ALIGNMENT_BOTTOMRIGHT
:
968 case SUB_ALIGNMENT_MIDDLERIGHT
:
969 case SUB_ALIGNMENT_TOPRIGHT
:
970 obj
->alignment
|= 0x2;
972 case SUB_ALIGNMENT_BOTTOMCENTER
:
973 case SUB_ALIGNMENT_MIDDLECENTER
:
974 case SUB_ALIGNMENT_TOPCENTER
:
976 obj
->alignment
|= 0x0;
980 if ((l
= obj
->params
.subtitle
.lines
)) {
981 for(counter
= dxs
; i
< l
; ++i
)
982 if (obj
->params
.subtitle
.xtbl
[i
] < counter
) counter
= obj
->params
.subtitle
.xtbl
[i
];
983 for (i
= 0; i
< l
; ++i
) {
984 switch (obj
->alignment
&0x3) {
991 x
= 2 * obj
->params
.subtitle
.xtbl
[i
] - counter
- ((obj
->params
.subtitle
.xtbl
[i
] == counter
) ? 0 : 1);
995 x
= obj
->params
.subtitle
.xtbl
[i
];
998 while ((c
=obj
->params
.subtitle
.utbl
[j
++])){
999 x
+= kerning(sub_font
,prevc
,c
);
1000 if ((font
=sub_font
->font
[c
])>=0)
1001 draw_alpha_buf(obj
,x
,y
,
1003 sub_font
->pic_a
[font
]->h
+y
<obj
->dys
? sub_font
->pic_a
[font
]->h
: obj
->dys
-y
,
1004 sub_font
->pic_b
[font
]->bmp
+sub_font
->start
[c
],
1005 sub_font
->pic_a
[font
]->bmp
+sub_font
->start
[c
],
1006 sub_font
->pic_a
[font
]->w
);
1007 x
+=sub_font
->width
[c
]+sub_font
->charspace
;
1010 y
+=sub_font
->height
;
1016 inline static void vo_update_spudec_sub(mp_osd_obj_t
* obj
, int dxs
, int dys
)
1018 unsigned int bbox
[4];
1019 spudec_calc_bbox(vo_spudec
, dxs
, dys
, bbox
);
1020 obj
->bbox
.x1
= bbox
[0];
1021 obj
->bbox
.x2
= bbox
[1];
1022 obj
->bbox
.y1
= bbox
[2];
1023 obj
->bbox
.y2
= bbox
[3];
1024 obj
->flags
|= OSDFLAG_BBOX
;
1027 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
)
1029 spudec_draw_scaled(vo_spudec
, obj
->dxs
, obj
->dys
, draw_alpha
, ctx
);
1032 void *vo_spudec
=NULL
;
1033 void *vo_vobsub
=NULL
;
1035 static int draw_alpha_init_flag
=0;
1037 void vo_draw_alpha_init(void);
1039 mp_osd_obj_t
* vo_osd_list
=NULL
;
1041 static mp_osd_obj_t
* new_osd_obj(int type
){
1042 mp_osd_obj_t
* osd
=malloc(sizeof(mp_osd_obj_t
));
1043 memset(osd
,0,sizeof(mp_osd_obj_t
));
1044 osd
->next
=vo_osd_list
;
1047 osd
->alpha_buffer
= NULL
;
1048 osd
->bitmap_buffer
= NULL
;
1049 osd
->allocated
= -1;
1053 void osd_free(struct osd_state
*osd
)
1055 mp_osd_obj_t
* obj
=vo_osd_list
;
1057 mp_osd_obj_t
* next
=obj
->next
;
1058 if (obj
->alpha_buffer
) free(obj
->alpha_buffer
);
1059 if (obj
->bitmap_buffer
) free(obj
->bitmap_buffer
);
1067 #define FONT_LOAD_DEFER 6
1069 int osd_update(struct osd_state
*osd
, int dxs
, int dys
)
1071 mp_osd_obj_t
* obj
=vo_osd_list
;
1073 #ifdef CONFIG_FREETYPE
1074 static int defer_counter
= 0, prev_dxs
= 0, prev_dys
= 0;
1077 #ifdef CONFIG_FREETYPE
1078 // here is the right place to get screen dimensions
1079 if (((dxs
!= vo_image_width
)
1080 && (subtitle_autoscale
== 2 || subtitle_autoscale
== 3))
1081 || ((dys
!= vo_image_height
)
1082 && (subtitle_autoscale
== 1 || subtitle_autoscale
== 3)))
1084 // screen dimensions changed
1085 // wait a while to avoid useless reloading of the font
1086 if (dxs
== prev_dxs
|| dys
== prev_dys
) {
1093 if (defer_counter
>= FONT_LOAD_DEFER
) force_load_font
= 1;
1096 if (force_load_font
) {
1097 force_load_font
= 0;
1098 load_font_ft(dxs
, dys
, &vo_font
, font_name
, osd_font_scale_factor
);
1100 load_font_ft(dxs
, dys
, &osd
->sub_font
, sub_font_name
, text_font_scale_factor
);
1102 load_font_ft(dxs
, dys
, &osd
->sub_font
, font_name
, text_font_scale_factor
);
1108 load_font_ft(dxs
, dys
, &vo_font
, font_name
, osd_font_scale_factor
);
1109 if (!osd
->sub_font
) {
1111 load_font_ft(dxs
, dys
, &osd
->sub_font
, sub_font_name
, text_font_scale_factor
);
1113 load_font_ft(dxs
, dys
, &osd
->sub_font
, font_name
, text_font_scale_factor
);
1119 if(dxs
!=obj
->dxs
|| dys
!=obj
->dys
|| obj
->flags
&OSDFLAG_FORCE_UPDATE
){
1120 int vis
=obj
->flags
&OSDFLAG_VISIBLE
;
1121 obj
->flags
&=~OSDFLAG_BBOX
;
1123 #ifdef CONFIG_DVDNAV
1124 case OSDTYPE_DVDNAV
:
1125 vo_update_nav(obj
,dxs
,dys
);
1128 case OSDTYPE_SUBTITLE
:
1129 vo_update_text_sub(osd
, obj
,dxs
,dys
);
1131 #ifdef CONFIG_TV_TELETEXT
1132 case OSDTYPE_TELETEXT
:
1133 vo_update_text_teletext(obj
,dxs
,dys
);
1136 case OSDTYPE_PROGBAR
:
1137 vo_update_text_progbar(obj
,dxs
,dys
);
1140 if(sub_visibility
&& vo_spudec
&& spudec_visible(vo_spudec
)){
1141 vo_update_spudec_sub(obj
, dxs
, dys
);
1142 obj
->flags
|=OSDFLAG_VISIBLE
|OSDFLAG_CHANGED
;
1145 obj
->flags
&=~OSDFLAG_VISIBLE
;
1148 if(vo_font
&& osd
->osd_text
[0]){
1149 vo_update_text_osd(osd
, obj
, dxs
, dys
); // update bbox
1150 obj
->flags
|=OSDFLAG_VISIBLE
|OSDFLAG_CHANGED
;
1152 obj
->flags
&=~OSDFLAG_VISIBLE
;
1156 if(!(obj
->flags
&OSDFLAG_BBOX
)){
1157 // we don't know, so assume the whole screen changed :(
1158 obj
->bbox
.x1
=obj
->bbox
.y1
=0;
1161 obj
->flags
|=OSDFLAG_BBOX
;
1163 // check bbox, reduce it if it's out of bounds (corners):
1164 if(obj
->bbox
.x1
<0) obj
->bbox
.x1
=0;
1165 if(obj
->bbox
.y1
<0) obj
->bbox
.y1
=0;
1166 if(obj
->bbox
.x2
>dxs
) obj
->bbox
.x2
=dxs
;
1167 if(obj
->bbox
.y2
>dys
) obj
->bbox
.y2
=dys
;
1168 if(obj
->flags
&OSDFLAG_VISIBLE
)
1170 mp_msg(MSGT_OSD
,MSGL_DBG2
,"OSD update: %d;%d %dx%d \n",
1171 obj
->bbox
.x1
,obj
->bbox
.y1
,obj
->bbox
.x2
-obj
->bbox
.x1
,
1172 obj
->bbox
.y2
-obj
->bbox
.y1
);
1174 // check if visibility changed:
1175 if(vis
!= (obj
->flags
&OSDFLAG_VISIBLE
) ) obj
->flags
|=OSDFLAG_CHANGED
;
1176 // remove the cause of automatic update:
1177 obj
->dxs
=dxs
; obj
->dys
=dys
;
1178 obj
->flags
&=~OSDFLAG_FORCE_UPDATE
;
1180 if(obj
->flags
&OSDFLAG_CHANGED
){
1182 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
);
1189 struct osd_state
*osd_create(void)
1191 struct osd_state
*osd
= talloc_zero(NULL
, struct osd_state
);
1192 *osd
= (struct osd_state
){
1194 if(!draw_alpha_init_flag
){
1195 draw_alpha_init_flag
=1;
1196 vo_draw_alpha_init();
1198 // temp hack, should be moved to mplayer/mencoder later
1199 new_osd_obj(OSDTYPE_OSD
);
1200 new_osd_obj(OSDTYPE_SUBTITLE
);
1201 new_osd_obj(OSDTYPE_PROGBAR
);
1202 new_osd_obj(OSDTYPE_SPU
);
1203 #ifdef CONFIG_DVDNAV
1204 new_osd_obj(OSDTYPE_DVDNAV
);
1206 #if CONFIG_TV_TELETEXT
1207 new_osd_obj(OSDTYPE_TELETEXT
);
1209 #ifdef CONFIG_FREETYPE
1210 force_load_font
= 1;
1215 int vo_osd_changed_flag
=0;
1217 void osd_remove_text(struct osd_state
*osd
, int dxs
, int dys
,
1218 void (*remove
)(int x0
, int y0
, int w
, int h
))
1220 mp_osd_obj_t
* obj
=vo_osd_list
;
1221 osd_update(osd
, dxs
, dys
);
1223 if(((obj
->flags
&OSDFLAG_CHANGED
) || (obj
->flags
&OSDFLAG_VISIBLE
)) &&
1224 (obj
->flags
&OSDFLAG_OLD_BBOX
)){
1225 int w
=obj
->old_bbox
.x2
-obj
->old_bbox
.x1
;
1226 int h
=obj
->old_bbox
.y2
-obj
->old_bbox
.y1
;
1228 vo_osd_changed_flag
=obj
->flags
&OSDFLAG_CHANGED
; // temp hack
1229 remove(obj
->old_bbox
.x1
,obj
->old_bbox
.y1
,w
,h
);
1231 // obj->flags&=~OSDFLAG_OLD_BBOX;
1237 void osd_draw_text(struct osd_state
*osd
, int dxs
, int dys
,
1238 void (*draw_alpha
)(void *ctx
, int x0
, int y0
, int w
, int h
,
1239 unsigned char* src
, unsigned char *srca
,
1243 mp_osd_obj_t
* obj
=vo_osd_list
;
1244 osd_update(osd
, dxs
, dys
);
1246 if(obj
->flags
&OSDFLAG_VISIBLE
){
1247 vo_osd_changed_flag
=obj
->flags
&OSDFLAG_CHANGED
; // temp hack
1250 vo_draw_spudec_sub(obj
, draw_alpha
, ctx
); // FIXME
1252 #ifdef CONFIG_DVDNAV
1253 case OSDTYPE_DVDNAV
:
1255 #ifdef CONFIG_TV_TELETEXT
1256 case OSDTYPE_TELETEXT
:
1259 case OSDTYPE_SUBTITLE
:
1260 case OSDTYPE_PROGBAR
:
1261 vo_draw_text_from_buffer(obj
, draw_alpha
, ctx
);
1264 obj
->old_bbox
=obj
->bbox
;
1265 obj
->flags
|=OSDFLAG_OLD_BBOX
;
1267 obj
->flags
&=~OSDFLAG_CHANGED
;
1272 static int vo_osd_changed_status
= 0;
1274 int vo_osd_changed(int new_value
)
1276 mp_osd_obj_t
* obj
=vo_osd_list
;
1277 int ret
= vo_osd_changed_status
;
1278 vo_osd_changed_status
= new_value
;
1281 if(obj
->type
==new_value
) obj
->flags
|=OSDFLAG_FORCE_UPDATE
;
1288 // BBBBBBBBBBBB AAAAAAAAAAAAA BBBBBBBBBBB
1289 // BBBBBBBBBBBB BBBBBBBBBBBBB
1292 // return TRUE if we have osd in the specified rectangular area:
1293 int vo_osd_check_range_update(int x1
,int y1
,int x2
,int y2
){
1294 mp_osd_obj_t
* obj
=vo_osd_list
;
1296 if(obj
->flags
&OSDFLAG_VISIBLE
){
1297 if( (obj
->bbox
.x1
<=x2
&& obj
->bbox
.x2
>=x1
) &&
1298 (obj
->bbox
.y1
<=y2
&& obj
->bbox
.y2
>=y1
) &&
1299 obj
->bbox
.y2
> obj
->bbox
.y1
&& obj
->bbox
.x2
> obj
->bbox
.x1