2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "stream/stream.h"
29 #include "stream/stream_dvdnav.h"
30 #define OSD_NAV_BOX_ALPHA 0x7f
32 #include "libmpcodecs/dec_teletext.h"
33 #include "osdep/timer.h"
39 #include "video_out.h"
40 #include "font_load.h"
43 #include "libavutil/common.h"
48 // Structures needed for the new splitting algorithm.
49 // osd_text_t contains the single subtitle word.
50 // osd_text_p is used to mark the lines of subtitles
52 int osd_kerning
, //kerning with the previous word
53 osd_length
, //orizontal length inside the bbox
54 text_length
, //number of characters
56 struct osd_text_t
*prev
,
62 struct osd_text_t
*ott
;
63 struct osd_text_p
*prev
,
68 char * const sub_osd_names
[]={
83 char * const sub_osd_names_short
[] ={ "", "|>", "||", "[]", "<<" , ">>", "", "", "", "", "", "", "" };
85 //static int vo_font_loaded=-1;
86 font_desc_t
* vo_font
=NULL
;
88 void* vo_osd_teletext_page
=NULL
;
89 int vo_osd_teletext_half
= 0;
90 int vo_osd_teletext_mode
=0;
91 int vo_osd_teletext_format
=0;
92 int vo_osd_teletext_scale
=0;
97 int sub_alignment
=2; /* 0=top, 1=center, 2=bottom */
99 int sub_bg_color
=0; /* subtitles background color */
103 static nav_highlight_t nav_hl
;
106 // return the real height of a char:
107 static inline int get_height(int c
,int h
){
109 if ((font
=vo_font
->font
[c
])>=0)
110 if(h
<vo_font
->pic_a
[font
]->h
) h
=vo_font
->pic_a
[font
]->h
;
114 // renders char to a big per-object buffer where alpha and bitmap are separated
115 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
)
117 int dststride
= obj
->stride
;
118 int dstskip
= obj
->stride
-w
;
119 int srcskip
= stride
-w
;
121 unsigned char *b
= obj
->bitmap_buffer
+ (y0
-obj
->bbox
.y1
)*dststride
+ (x0
-obj
->bbox
.x1
);
122 unsigned char *a
= obj
->alpha_buffer
+ (y0
-obj
->bbox
.y1
)*dststride
+ (x0
-obj
->bbox
.x1
);
123 unsigned char *bs
= src
;
124 unsigned char *as
= srca
;
126 if (x0
< obj
->bbox
.x1
|| x0
+w
> obj
->bbox
.x2
|| y0
< obj
->bbox
.y1
|| y0
+h
> obj
->bbox
.y2
) {
127 fprintf(stderr
, "osd text out of range: bbox [%d %d %d %d], txt [%d %d %d %d]\n",
128 obj
->bbox
.x1
, obj
->bbox
.x2
, obj
->bbox
.y1
, obj
->bbox
.y2
,
133 for (i
= 0; i
< h
; i
++) {
134 for (j
= 0; j
< w
; j
++, b
++, a
++, bs
++, as
++) {
135 if (*b
< *bs
) *b
= *bs
;
137 if (*a
== 0 || *a
> *as
) *a
= *as
;
147 // allocates/enlarges the alpha/bitmap buffer
148 static void alloc_buf(mp_osd_obj_t
* obj
)
151 if (obj
->bbox
.x2
< obj
->bbox
.x1
) obj
->bbox
.x2
= obj
->bbox
.x1
;
152 if (obj
->bbox
.y2
< obj
->bbox
.y1
) obj
->bbox
.y2
= obj
->bbox
.y1
;
153 obj
->stride
= ((obj
->bbox
.x2
-obj
->bbox
.x1
)+7)&(~7);
154 len
= obj
->stride
*(obj
->bbox
.y2
-obj
->bbox
.y1
);
155 if (obj
->allocated
<len
) {
156 obj
->allocated
= len
;
157 free(obj
->bitmap_buffer
);
158 free(obj
->alpha_buffer
);
159 obj
->bitmap_buffer
= (unsigned char *)memalign(16, len
);
160 obj
->alpha_buffer
= (unsigned char *)memalign(16, len
);
162 memset(obj
->bitmap_buffer
, sub_bg_color
, len
);
163 memset(obj
->alpha_buffer
, sub_bg_alpha
, len
);
166 // renders the buffer
167 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
)
169 if (obj
->allocated
> 0) {
171 obj
->bbox
.x1
,obj
->bbox
.y1
,
172 obj
->bbox
.x2
-obj
->bbox
.x1
,
173 obj
->bbox
.y2
-obj
->bbox
.y1
,
180 unsigned utf8_get_char(const char **str
) {
181 const uint8_t *strp
= (const uint8_t *)*str
;
183 GET_UTF8(c
, *strp
++, goto no_utf8
;);
184 *str
= (const char *)strp
;
188 strp
= (const uint8_t *)*str
;
190 *str
= (const char *)strp
;
194 inline static void vo_update_text_osd(struct osd_state
*osd
, mp_osd_obj_t
* obj
,
197 const char *cp
= osd
->osd_text
;
202 obj
->bbox
.x1
=obj
->x
=x
;
203 obj
->bbox
.y1
=obj
->y
=10;
206 uint16_t c
=utf8_get_char(&cp
);
207 render_one_glyph(vo_font
, c
);
208 x
+=vo_font
->width
[c
]+vo_font
->charspace
;
212 obj
->bbox
.x2
=x
-vo_font
->charspace
;
213 obj
->bbox
.y2
=obj
->bbox
.y1
+h
;
214 obj
->flags
|=OSDFLAG_BBOX
;
221 uint16_t c
=utf8_get_char(&cp
);
222 if ((font
=vo_font
->font
[c
])>=0)
223 draw_alpha_buf(obj
,x
,obj
->y
,
225 vo_font
->pic_a
[font
]->h
,
226 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
227 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
228 vo_font
->pic_a
[font
]->w
);
229 x
+=vo_font
->width
[c
]+vo_font
->charspace
;
234 void osd_set_nav_box (uint16_t sx
, uint16_t sy
, uint16_t ex
, uint16_t ey
) {
241 inline static void vo_update_nav (mp_osd_obj_t
*obj
, int dxs
, int dys
, int left_border
, int top_border
,
242 int right_border
, int bottom_border
, int orig_w
, int orig_h
) {
244 int sx
= nav_hl
.sx
, sy
= nav_hl
.sy
;
245 int ex
= nav_hl
.ex
, ey
= nav_hl
.ey
;
246 int scaled_w
= dxs
- left_border
- right_border
;
247 int scaled_h
= dys
- top_border
- bottom_border
;
248 if (scaled_w
!= orig_w
) {
249 sx
= sx
* scaled_w
/ orig_w
;
250 ex
= ex
* scaled_w
/ orig_w
;
252 if (scaled_h
!= orig_h
) {
253 sy
= sy
* scaled_h
/ orig_h
;
254 ey
= ey
* scaled_h
/ orig_h
;
256 sx
+= left_border
; ex
+= left_border
;
257 sy
+= top_border
; ey
+= top_border
;
258 sx
= FFMIN(FFMAX(sx
, 0), dxs
);
259 ex
= FFMIN(FFMAX(ex
, 0), dxs
);
260 sy
= FFMIN(FFMAX(sy
, 0), dys
);
261 ey
= FFMIN(FFMAX(ey
, 0), dys
);
263 obj
->bbox
.x1
= obj
->x
= sx
;
264 obj
->bbox
.y1
= obj
->y
= sy
;
269 len
= obj
->stride
* (obj
->bbox
.y2
- obj
->bbox
.y1
);
270 memset (obj
->bitmap_buffer
, OSD_NAV_BOX_ALPHA
, len
);
271 memset (obj
->alpha_buffer
, OSD_NAV_BOX_ALPHA
, len
);
272 obj
->flags
|= OSDFLAG_BBOX
| OSDFLAG_CHANGED
;
273 if (obj
->bbox
.y2
> obj
->bbox
.y1
&& obj
->bbox
.x2
> obj
->bbox
.x1
)
274 obj
->flags
|= OSDFLAG_VISIBLE
;
278 // renders char to a big per-object buffer where alpha and bitmap are separated
279 static void tt_draw_alpha_buf(mp_osd_obj_t
* obj
, int x0
,int y0
, int w
,int h
, unsigned char* src
, int stride
,int fg
,int bg
,int alpha
)
281 int dststride
= obj
->stride
;
282 int dstskip
= obj
->stride
-w
;
283 int srcskip
= stride
-w
;
285 unsigned char *b
= obj
->bitmap_buffer
+ (y0
-obj
->bbox
.y1
)*dststride
+ (x0
-obj
->bbox
.x1
);
286 unsigned char *a
= obj
->alpha_buffer
+ (y0
-obj
->bbox
.y1
)*dststride
+ (x0
-obj
->bbox
.x1
);
287 unsigned char *bs
= src
;
288 if (x0
< obj
->bbox
.x1
|| x0
+w
> obj
->bbox
.x2
|| y0
< obj
->bbox
.y1
|| y0
+h
> obj
->bbox
.y2
) {
289 mp_msg(MSGT_OSD
,MSGL_ERR
,"tt osd text out of range: bbox [%d %d %d %d], txt [%d %d %d %d]\n",
290 obj
->bbox
.x1
, obj
->bbox
.x2
, obj
->bbox
.y1
, obj
->bbox
.y2
,
294 for (i
= 0; i
< h
; i
++) {
295 for (j
= 0; j
< w
; j
++, b
++, a
++, bs
++) {
296 *b
=(fg
-bg
)*(*bs
)/255+bg
;
304 inline static void vo_update_text_teletext(mp_osd_obj_t
*obj
, int dxs
, int dys
)
306 int h
=0,w
=0,i
,j
,font
,flashon
;
314 int start_row
,max_rows
;
315 int b
,ax
[6],ay
[6],aw
[6],ah
[6];
317 tt_char
* tdp
=vo_osd_teletext_page
;
318 static const uint8_t colors
[8]={1,85,150,226,70,105,179,254};
319 unsigned char* buf
[9];
321 obj
->flags
|=OSDFLAG_CHANGED
|OSDFLAG_VISIBLE
;
322 if (!tdp
|| !vo_osd_teletext_mode
) {
323 obj
->flags
&=~OSDFLAG_VISIBLE
;
326 flashon
=(GetTimer()/1000000)%2;
327 switch(vo_osd_teletext_half
){
328 case TT_ZOOM_TOP_HALF
:
332 case TT_ZOOM_BOTTOM_HALF
:
333 start_row
=VBI_ROWS
/2;
342 for(i
=start_row
;i
<max_rows
;i
++){
343 for(j
=0;j
<VBI_COLUMNS
;j
++){
344 tc
=tdp
[i
*VBI_COLUMNS
+j
];
345 if(!tc
.ctl
&& !tc
.gfx
)
347 render_one_glyph(vo_font
, tc
.unicode
);
348 if (wm
<vo_font
->width
[tc
.unicode
])
349 wm
=vo_font
->width
[tc
.unicode
];
354 hm
=vo_font
->height
+1;
355 wm
=dxs
*hm
*max_rows
/(dys
*VBI_COLUMNS
);
357 #ifdef CONFIG_FREETYPE
358 //very simple teletext font auto scaling
359 if(!vo_osd_teletext_scale
&& hm
*(max_rows
+1)>dys
){
360 osd_font_scale_factor
*=1.0*(dys
)/((max_rows
+1)*hm
);
362 vo_osd_teletext_scale
=osd_font_scale_factor
;
363 obj
->flags
&=~OSDFLAG_VISIBLE
;
375 w
=cols
*wm
-vo_font
->charspace
;
376 h
=rows
*hm
-vo_font
->charspace
;
416 obj
->flags
|= OSDFLAG_BBOX
;
420 buf
[i
]=malloc(wm
*hm
);
423 if(vo_osd_teletext_format
==TT_FORMAT_OPAQUE
||vo_osd_teletext_format
==TT_FORMAT_OPAQUE_INV
)
427 memset(buf
[8],color
,wm
*hm
);
429 if(vo_osd_teletext_format
==TT_FORMAT_OPAQUE
||vo_osd_teletext_format
==TT_FORMAT_TRANSPARENT
){
431 memset(buf
[i
],(unsigned char)(1.0*(255-color
)*colors
[i
]/255),wm
*hm
);
435 memset(buf
[i
],(unsigned char)(1.0*(255-color
)*colors
[7-i
]/255),wm
*hm
);
442 tc
=tdp
[(i
+start_row
)*VBI_COLUMNS
+j
];
443 if (tc
.hidden
) { x
+=wm
; continue;}
444 if(!tc
.gfx
|| (tc
.flh
&& !flashon
)){
445 /* Rendering one text character */
446 draw_alpha_buf(obj
,x
,y
,wm
,hm
,buf
[tc
.bg
],buf
[8],wm
);
447 if(tc
.unicode
!=0x20 && tc
.unicode
!=0x00 && !tc
.ctl
&&
448 (!tc
.flh
|| flashon
) &&
449 (font
=vo_font
->font
[tc
.unicode
])>=0 && y
+hm
<dys
){
450 tt_draw_alpha_buf(obj
,x
,y
,vo_font
->width
[tc
.unicode
],vo_font
->height
,
451 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[tc
.unicode
]-vo_font
->charspace
*vo_font
->pic_a
[font
]->w
,
452 vo_font
->pic_b
[font
]->w
,
453 buf
[tc
.fg
][0],buf
[tc
.bg
][0],buf
[8][0]);
457 Rendering one graphics character
458 TODO: support for separated graphics symbols (where six rectangles does not touch each other)
462 |10| <= |34| <= 00100110 <= 0x26
466 (0:wm/2) (wm/2:wm-wm/2)
468 ********** *********** (0:hm/3)
470 *** 1 **** **** 2 ****
472 ********** ***********
473 ********** ***********
475 ********** *********** (hm/3:hm-2*hm/3)
476 ********** ***********
478 *** 3 **** **** 4 ****
480 ********** ***********
481 ********** ***********
482 ********** ***********
484 ********** *********** (hm-hm/3:hm/3)
486 *** 5 **** **** 6 ****
488 ********** ***********
489 ********** ***********
492 if(tc
.gfx
>1){ //separated gfx
494 color
=(tc
.unicode
>>b
)&1?tc
.fg
:tc
.bg
;
495 draw_alpha_buf(obj
,x
+ax
[b
]+1,y
+ay
[b
]+1,aw
[b
]-2,ah
[b
]-2,buf
[color
],buf
[8],wm
);
497 //separated gfx (background borders)
499 draw_alpha_buf(obj
,x
,y
,1,hm
,buf
[tc
.bg
],buf
[8],wm
);
500 draw_alpha_buf(obj
,x
+ax
[1]-1,y
,2,hm
,buf
[tc
.bg
],buf
[8],wm
);
501 draw_alpha_buf(obj
,x
+ax
[1]+aw
[1]-1,y
,wm
-ax
[1]-aw
[1]+1,hm
,buf
[tc
.bg
],buf
[8],wm
);
503 draw_alpha_buf(obj
,x
,y
,wm
,1,buf
[tc
.bg
],buf
[8],wm
);
504 draw_alpha_buf(obj
,x
,y
+ay
[0]+ah
[0]-1,wm
,2,buf
[tc
.bg
],buf
[8],wm
);
505 draw_alpha_buf(obj
,x
,y
+ay
[2]+ah
[2]-1,wm
,2,buf
[tc
.bg
],buf
[8],wm
);
506 draw_alpha_buf(obj
,x
,y
+ay
[4]+ah
[4]-1,wm
,hm
-ay
[4]-ah
[4]+1,buf
[tc
.bg
],buf
[8],wm
);
509 color
=(tc
.unicode
>>b
)&1?tc
.fg
:tc
.bg
;
510 draw_alpha_buf(obj
,x
+ax
[b
],y
+ay
[b
],aw
[b
],ah
[b
],buf
[color
],buf
[8],wm
);
522 int vo_osd_progbar_type
=-1;
523 int vo_osd_progbar_value
=100; // 0..256
525 // if we have n=256 bars then OSD progbar looks like below
527 // 0 1 2 3 ... 256 <= vo_osd_progbar_value
529 // [ === === === ... === ]
531 // the above schema is rescalled to n=elems bars
533 inline static void vo_update_text_progbar(mp_osd_obj_t
* obj
,int dxs
,int dys
){
535 obj
->flags
|=OSDFLAG_CHANGED
|OSDFLAG_VISIBLE
;
537 if(vo_osd_progbar_type
<0 || !vo_font
){
538 obj
->flags
&=~OSDFLAG_VISIBLE
;
542 render_one_glyph(vo_font
, OSD_PB_START
);
543 render_one_glyph(vo_font
, OSD_PB_END
);
544 render_one_glyph(vo_font
, OSD_PB_0
);
545 render_one_glyph(vo_font
, OSD_PB_1
);
546 render_one_glyph(vo_font
, vo_osd_progbar_type
);
548 // calculate bbox corners:
550 int y
=(dys
-vo_font
->height
)/2;
551 int delimw
=vo_font
->width
[OSD_PB_START
]
552 +vo_font
->width
[OSD_PB_END
]
554 int width
=(2*dxs
-3*delimw
)/3;
555 int charw
=vo_font
->width
[OSD_PB_0
]+vo_font
->charspace
;
556 int elems
=width
/charw
;
557 int x
=(dxs
-elems
*charw
-delimw
)/2;
559 h
=get_height(OSD_PB_START
,h
);
560 h
=get_height(OSD_PB_END
,h
);
561 h
=get_height(OSD_PB_0
,h
);
562 h
=get_height(OSD_PB_1
,h
);
563 if (vo_osd_progbar_type
>0 && vo_font
->font
[vo_osd_progbar_type
]>=0){
564 delta
= vo_font
->width
[vo_osd_progbar_type
]+vo_font
->spacewidth
;
565 delta
= (x
-delta
> 0) ? delta
: x
;
566 h
=get_height(vo_osd_progbar_type
,h
);
568 obj
->bbox
.x1
=obj
->x
=x
;
569 obj
->bbox
.y1
=obj
->y
=y
;
570 obj
->bbox
.x2
=x
+width
+delimw
;
571 obj
->bbox
.y2
=y
+h
; //vo_font->height;
572 obj
->flags
|=OSDFLAG_BBOX
;
573 obj
->params
.progbar
.elems
=elems
;
574 obj
->bbox
.x1
-=delta
; // space for an icon
580 int minw
= vo_font
->width
[OSD_PB_START
]+vo_font
->width
[OSD_PB_END
]+vo_font
->width
[OSD_PB_0
];
581 if (vo_osd_progbar_type
>0 && vo_font
->font
[vo_osd_progbar_type
]>=0){
582 minw
+= vo_font
->width
[vo_osd_progbar_type
]+vo_font
->charspace
+vo_font
->spacewidth
;
584 if (obj
->bbox
.x2
- obj
->bbox
.x1
< minw
) return; // space too small, don't render anything
594 int charw
=vo_font
->width
[OSD_PB_0
]+vo_font
->charspace
;
595 int elems
=obj
->params
.progbar
.elems
;
597 if (vo_osd_progbar_value
<=0)
600 int ev
=vo_osd_progbar_value
*elems
;
602 if (ev
& 0xFF) mark
++;
603 if (mark
>elems
) mark
=elems
;
607 // printf("osd.progbar width=%d xpos=%d\n",width,x);
609 c
=vo_osd_progbar_type
;
610 if(vo_osd_progbar_type
>0 && (font
=vo_font
->font
[c
])>=0) {
611 int xp
=x
-vo_font
->width
[c
]-vo_font
->spacewidth
;
612 draw_alpha_buf(obj
,(xp
<0?0:xp
),y
,
614 vo_font
->pic_a
[font
]->h
,
615 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
616 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
617 vo_font
->pic_a
[font
]->w
);
621 if ((font
=vo_font
->font
[c
])>=0)
622 draw_alpha_buf(obj
,x
,y
,
624 vo_font
->pic_a
[font
]->h
,
625 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
626 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
627 vo_font
->pic_a
[font
]->w
);
628 x
+=vo_font
->width
[c
]+vo_font
->charspace
;
631 if ((font
=vo_font
->font
[c
])>=0){
633 h
=vo_font
->pic_a
[font
]->h
;
634 s
=vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
];
635 sa
=vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
];
636 st
=vo_font
->pic_a
[font
]->w
;
638 draw_alpha_buf(obj
,x
,y
,w
,h
,s
,sa
,st
);
644 if ((font
=vo_font
->font
[c
])>=0){
646 h
=vo_font
->pic_a
[font
]->h
;
647 s
=vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
];
648 sa
=vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
];
649 st
=vo_font
->pic_a
[font
]->w
;
650 if ((i
=elems
-mark
)) do {
651 draw_alpha_buf(obj
,x
,y
,w
,h
,s
,sa
,st
);
657 if ((font
=vo_font
->font
[c
])>=0)
658 draw_alpha_buf(obj
,x
,y
,
660 vo_font
->pic_a
[font
]->h
,
661 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
662 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
663 vo_font
->pic_a
[font
]->w
);
664 // x+=vo_font->width[c]+vo_font->charspace;
667 // vo_osd_progbar_value=(vo_osd_progbar_value+1)&0xFF;
671 subtitle
* vo_sub
=NULL
;
673 inline static void vo_update_text_sub(struct osd_state
*osd
, mp_osd_obj_t
* obj
,int dxs
,int dys
){
675 int c
,i
,j
,l
,x
,y
,font
,prevc
,counter
;
677 int lastStripPosition
;
682 struct font_desc
*sub_font
= osd
->sub_font
;
684 obj
->flags
|=OSDFLAG_CHANGED
|OSDFLAG_VISIBLE
;
686 if(!vo_sub
|| !osd
->sub_font
|| !sub_visibility
|| (sub_font
->font
[40]<0)){
687 obj
->flags
&=~OSDFLAG_VISIBLE
;
691 obj
->bbox
.y2
=obj
->y
=dys
;
692 obj
->params
.subtitle
.lines
=0;
694 // too long lines divide into a smaller ones
697 lastStripPosition
=-1;
701 struct osd_text_t
*osl
, *cp_ott
, *tmp_ott
, *tmp
;
702 struct osd_text_p
*otp_sub
= NULL
, *otp_sub_tmp
, // these are used to store the whole sub text osd
703 *otp
, *tmp_otp
, *pmt
; // these are used to manage sub text osd coming from a single sub line
704 int *char_seq
, char_position
, xlimit
= dxs
* sub_width_p
/ 100, counter
;
707 xsize
= -sub_font
->charspace
;
711 char_seq
= calloc(strlen(t
), sizeof(int));
719 // reading the subtitle words from vo_sub->text[]
722 c
= utf8_get_char(&t
);
723 else if ((c
= *t
++) >= 0x80 && sub_unicode
)
726 t
+= strlen(t
); // end here
727 mp_msg(MSGT_OSD
,MSGL_WARN
,"\nMAX_UCS exceeded!\n");
729 if (!c
) c
++; // avoid UCS 0
730 render_one_glyph(sub_font
, c
);
733 struct osd_text_t
*tmp_ott
= (struct osd_text_t
*) calloc(1, sizeof(struct osd_text_t
));
736 osl
= cp_ott
= tmp_ott
;
738 tmp_ott
->prev
= cp_ott
;
739 cp_ott
->next
= tmp_ott
;
740 tmp_ott
->osd_kerning
=
741 sub_font
->charspace
+ sub_font
->width
[' '];
744 tmp_ott
->osd_length
= xsize
;
745 tmp_ott
->text_length
= char_position
;
746 tmp_ott
->text
= (int *) malloc(char_position
* sizeof(int));
747 for (counter
= 0; counter
< char_position
; ++counter
)
748 tmp_ott
->text
[counter
] = char_seq
[counter
];
753 int delta_xsize
= sub_font
->width
[c
] + sub_font
->charspace
+ kerning(sub_font
, prevc
, c
);
755 if (xsize
+ delta_xsize
<= dxs
) {
758 char_seq
[char_position
++] = c
;
759 xsize
+= delta_xsize
;
760 if ((!suboverlap_enabled
) && ((font
= sub_font
->font
[c
]) >= 0)) {
761 if (sub_font
->pic_a
[font
]->h
> h
) {
762 h
= sub_font
->pic_a
[font
]->h
;
767 mp_msg(MSGT_OSD
, MSGL_WARN
, "\nSubtitle word '%s' too long!\n", t
);
772 }// for len (all words from subtitle line read)
774 // osl holds an ordered (as they appear in the lines) chain of the subtitle words
776 struct osd_text_t
*tmp_ott
= (struct osd_text_t
*) calloc(1, sizeof(struct osd_text_t
));
779 osl
= cp_ott
= tmp_ott
;
781 tmp_ott
->prev
= cp_ott
;
782 cp_ott
->next
= tmp_ott
;
783 tmp_ott
->osd_kerning
=
784 sub_font
->charspace
+ sub_font
->width
[' '];
787 tmp_ott
->osd_length
= xsize
;
788 tmp_ott
->text_length
= char_position
;
789 tmp_ott
->text
= (int *) malloc(char_position
* sizeof(int));
790 for (counter
= 0; counter
< char_position
; ++counter
)
791 tmp_ott
->text
[counter
] = char_seq
[counter
];
793 xsize
= -sub_font
->charspace
;
798 int value
= 0, exit
= 0, minimum
= 0;
800 // otp will contain the chain of the osd subtitle lines coming from the single vo_sub line.
801 otp
= tmp_otp
= (struct osd_text_p
*) calloc(1, sizeof(struct osd_text_p
));
803 for (tmp_ott
= tmp_otp
->ott
; exit
== 0; ) {
805 value
+= tmp_ott
->osd_kerning
+ tmp_ott
->osd_length
;
806 tmp_ott
= tmp_ott
->next
;
807 } while ((tmp_ott
!= NULL
) && (value
+ tmp_ott
->osd_kerning
+ tmp_ott
->osd_length
<= xlimit
));
808 if (tmp_ott
!= NULL
) {
809 struct osd_text_p
*tmp
= (struct osd_text_p
*) calloc(1, sizeof(struct osd_text_p
));
811 tmp_otp
->value
= value
;
815 tmp_otp
->ott
= tmp_ott
;
816 value
= -2 * sub_font
->charspace
- sub_font
->width
[' '];
818 tmp_otp
->value
= value
;
825 // minimum holds the 'sum of the differences in length among the lines',
826 // a measure of the evenness of the lengths of the lines
827 for (tmp_otp
= otp
; tmp_otp
->next
!= NULL
; tmp_otp
= tmp_otp
->next
) {
829 while (pmt
!= NULL
) {
830 minimum
+= abs(tmp_otp
->value
- pmt
->value
);
835 if (otp
->next
!= NULL
) {
837 struct osd_text_p
*mem
, *hold
;
840 // until the last word of a line can be moved to the beginning of following line
841 // reducing the 'sum of the differences in length among the lines', it is done
845 for (tmp_otp
= otp
; tmp_otp
->next
!= NULL
; tmp_otp
= tmp_otp
->next
) {
847 for (tmp
= tmp_otp
->ott
; tmp
->next
!= pmt
->ott
; tmp
= tmp
->next
);
848 if (pmt
->value
+ tmp
->osd_length
+ pmt
->ott
->osd_kerning
<= xlimit
) {
849 mem1
= tmp_otp
->value
;
851 tmp_otp
->value
= mem1
- tmp
->osd_length
- tmp
->osd_kerning
;
852 pmt
->value
= mem2
+ tmp
->osd_length
+ pmt
->ott
->osd_kerning
;
855 for (mem
= otp
; mem
->next
!= NULL
; mem
= mem
->next
) {
857 while (pmt
!= NULL
) {
858 value
+= abs(mem
->value
- pmt
->value
);
862 if (value
< minimum
) {
867 tmp_otp
->value
= mem1
;
868 tmp_otp
->next
->value
= mem2
;
875 for (tmp
= tmp_otp
->ott
; tmp
->next
!= pmt
->ott
; tmp
= tmp
->next
);
876 mem1
= tmp_otp
->value
;
878 tmp_otp
->value
= mem1
- tmp
->osd_length
- tmp
->osd_kerning
;
879 pmt
->value
= mem2
+ tmp
->osd_length
+ pmt
->ott
->osd_kerning
;
883 }//~if(otp->next!=NULL)
886 // adding otp (containing splitted lines) to otp chain
887 if (otp_sub
== NULL
) {
889 for (otp_sub_tmp
= otp_sub
; otp_sub_tmp
->next
!= NULL
; otp_sub_tmp
= otp_sub_tmp
->next
);
893 while (tmp
->next
!= NULL
) tmp
= tmp
->next
;
894 tmp
->next
= otp
->ott
;
895 otp
->ott
->prev
= tmp
;
896 //attaching new subtitle line at the end
897 otp_sub_tmp
->next
= otp
;
898 otp
->prev
= otp_sub_tmp
;
900 otp_sub_tmp
= otp_sub_tmp
->next
;
901 while (otp_sub_tmp
->next
!= NULL
);
906 // write lines into utbl
910 obj
->params
.subtitle
.lines
= 0;
911 for (tmp_otp
= otp_sub
; tmp_otp
!= NULL
; tmp_otp
= tmp_otp
->next
) {
913 if ((obj
->params
.subtitle
.lines
++) >= MAX_UCSLINES
)
916 if (h
> obj
->y
) { // out of the screen so end parsing
917 obj
->y
-= lasth
- sub_font
->height
; // correct the y position
920 xsize
= tmp_otp
->value
;
921 obj
->params
.subtitle
.xtbl
[xtblc
++] = (dxs
- xsize
) / 2;
922 if (xmin
> (dxs
- xsize
) / 2)
923 xmin
= (dxs
- xsize
) / 2;
924 if (xmax
< (dxs
+ xsize
) / 2)
925 xmax
= (dxs
+ xsize
) / 2;
927 tmp
= (tmp_otp
->next
== NULL
) ? NULL
: tmp_otp
->next
->ott
;
928 for (tmp_ott
= tmp_otp
->ott
; tmp_ott
!= tmp
; tmp_ott
= tmp_ott
->next
) {
929 for (counter
= 0; counter
< tmp_ott
->text_length
; ++counter
) {
930 if (utblc
> MAX_UCS
) {
933 c
= tmp_ott
->text
[counter
];
934 render_one_glyph(sub_font
, c
);
935 obj
->params
.subtitle
.utbl
[utblc
++] = c
;
938 obj
->params
.subtitle
.utbl
[utblc
++] = ' ';
940 obj
->params
.subtitle
.utbl
[utblc
- 1] = 0;
941 obj
->y
-= sub_font
->height
;
943 if(obj
->params
.subtitle
.lines
)
944 obj
->y
= dys
- ((obj
->params
.subtitle
.lines
- 1) * sub_font
->height
+ sub_font
->pic_a
[sub_font
->font
[40]]->h
);
947 if (otp_sub
!= NULL
) {
948 for (tmp
= otp_sub
->ott
; tmp
->next
!= NULL
; free(tmp
->prev
)) {
955 for(pmt
= otp_sub
; pmt
->next
!= NULL
; free(pmt
->prev
)) {
962 /// vertical alignment
964 if (sub_alignment
== 2)
965 obj
->y
= dys
* sub_pos
/ 100 - h
;
966 else if (sub_alignment
== 1)
967 obj
->y
= dys
* sub_pos
/ 100 - h
/ 2;
969 obj
->y
= dys
* sub_pos
/ 100;
973 if (obj
->y
> dys
- h
)
976 obj
->bbox
.y2
= obj
->y
+ h
;
979 if (sub_justify
) xmin
= 10;
983 // obj->bbox.y2=obj->y+obj->params.subtitle.lines*sub_font->height;
984 obj
->flags
|=OSDFLAG_BBOX
;
991 switch(vo_sub
->alignment
) {
992 case SUB_ALIGNMENT_BOTTOMLEFT
:
993 case SUB_ALIGNMENT_MIDDLELEFT
:
994 case SUB_ALIGNMENT_TOPLEFT
:
995 obj
->alignment
|= 0x1;
997 case SUB_ALIGNMENT_BOTTOMRIGHT
:
998 case SUB_ALIGNMENT_MIDDLERIGHT
:
999 case SUB_ALIGNMENT_TOPRIGHT
:
1000 obj
->alignment
|= 0x2;
1002 case SUB_ALIGNMENT_BOTTOMCENTER
:
1003 case SUB_ALIGNMENT_MIDDLECENTER
:
1004 case SUB_ALIGNMENT_TOPCENTER
:
1006 obj
->alignment
|= 0x0;
1010 if ((l
= obj
->params
.subtitle
.lines
)) {
1011 for(counter
= dxs
; i
< l
; ++i
)
1012 if (obj
->params
.subtitle
.xtbl
[i
] < counter
) counter
= obj
->params
.subtitle
.xtbl
[i
];
1013 for (i
= 0; i
< l
; ++i
) {
1014 switch (obj
->alignment
&0x3) {
1021 x
= 2 * obj
->params
.subtitle
.xtbl
[i
] - counter
- ((obj
->params
.subtitle
.xtbl
[i
] == counter
) ? 0 : 1);
1025 x
= obj
->params
.subtitle
.xtbl
[i
];
1028 while ((c
=obj
->params
.subtitle
.utbl
[j
++])){
1029 x
+= kerning(sub_font
,prevc
,c
);
1030 if ((font
=sub_font
->font
[c
])>=0)
1031 draw_alpha_buf(obj
,x
,y
,
1033 sub_font
->pic_a
[font
]->h
+y
<obj
->dys
? sub_font
->pic_a
[font
]->h
: obj
->dys
-y
,
1034 sub_font
->pic_b
[font
]->bmp
+sub_font
->start
[c
],
1035 sub_font
->pic_a
[font
]->bmp
+sub_font
->start
[c
],
1036 sub_font
->pic_a
[font
]->w
);
1037 x
+=sub_font
->width
[c
]+sub_font
->charspace
;
1040 y
+=sub_font
->height
;
1046 inline static void vo_update_spudec_sub(mp_osd_obj_t
* obj
, int dxs
, int dys
)
1048 unsigned int bbox
[4];
1049 spudec_calc_bbox(vo_spudec
, dxs
, dys
, bbox
);
1050 obj
->bbox
.x1
= bbox
[0];
1051 obj
->bbox
.x2
= bbox
[1];
1052 obj
->bbox
.y1
= bbox
[2];
1053 obj
->bbox
.y2
= bbox
[3];
1054 obj
->flags
|= OSDFLAG_BBOX
;
1057 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
)
1059 spudec_draw_scaled(vo_spudec
, obj
->dxs
, obj
->dys
, draw_alpha
, ctx
);
1062 void *vo_spudec
=NULL
;
1063 void *vo_vobsub
=NULL
;
1065 static int draw_alpha_init_flag
=0;
1067 void vo_draw_alpha_init(void);
1069 mp_osd_obj_t
* vo_osd_list
=NULL
;
1071 static mp_osd_obj_t
* new_osd_obj(int type
){
1072 mp_osd_obj_t
* osd
=malloc(sizeof(mp_osd_obj_t
));
1073 memset(osd
,0,sizeof(mp_osd_obj_t
));
1074 osd
->next
=vo_osd_list
;
1077 osd
->alpha_buffer
= NULL
;
1078 osd
->bitmap_buffer
= NULL
;
1079 osd
->allocated
= -1;
1083 void osd_free(struct osd_state
*osd
)
1085 mp_osd_obj_t
* obj
=vo_osd_list
;
1087 mp_osd_obj_t
* next
=obj
->next
;
1088 if (obj
->alpha_buffer
) free(obj
->alpha_buffer
);
1089 if (obj
->bitmap_buffer
) free(obj
->bitmap_buffer
);
1097 #define FONT_LOAD_DEFER 6
1099 int osd_update_ext(struct osd_state
*osd
, int dxs
, int dys
, int left_border
,
1100 int top_border
, int right_border
, int bottom_border
,
1101 int orig_w
, int orig_h
)
1103 mp_osd_obj_t
* obj
=vo_osd_list
;
1105 #ifdef CONFIG_FREETYPE
1106 static int defer_counter
= 0, prev_dxs
= 0, prev_dys
= 0;
1109 #ifdef CONFIG_FREETYPE
1110 // here is the right place to get screen dimensions
1111 if (((dxs
!= vo_image_width
)
1112 && (subtitle_autoscale
== 2 || subtitle_autoscale
== 3))
1113 || ((dys
!= vo_image_height
)
1114 && (subtitle_autoscale
== 1 || subtitle_autoscale
== 3)))
1116 // screen dimensions changed
1117 // wait a while to avoid useless reloading of the font
1118 if (dxs
== prev_dxs
|| dys
== prev_dys
) {
1125 if (defer_counter
>= FONT_LOAD_DEFER
) force_load_font
= 1;
1128 if (force_load_font
) {
1129 force_load_font
= 0;
1130 load_font_ft(dxs
, dys
, &vo_font
, font_name
, osd_font_scale_factor
);
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 load_font_ft(dxs
, dys
, &vo_font
, font_name
, osd_font_scale_factor
);
1141 if (!osd
->sub_font
) {
1143 load_font_ft(dxs
, dys
, &osd
->sub_font
, sub_font_name
, text_font_scale_factor
);
1145 load_font_ft(dxs
, dys
, &osd
->sub_font
, font_name
, text_font_scale_factor
);
1151 if(dxs
!=obj
->dxs
|| dys
!=obj
->dys
|| obj
->flags
&OSDFLAG_FORCE_UPDATE
){
1152 int vis
=obj
->flags
&OSDFLAG_VISIBLE
;
1153 obj
->flags
&=~OSDFLAG_BBOX
;
1155 #ifdef CONFIG_DVDNAV
1156 case OSDTYPE_DVDNAV
:
1157 vo_update_nav(obj
,dxs
,dys
, left_border
, top_border
, right_border
, bottom_border
, orig_w
, orig_h
);
1160 case OSDTYPE_SUBTITLE
:
1161 vo_update_text_sub(osd
, obj
,dxs
,dys
);
1163 case OSDTYPE_TELETEXT
:
1164 vo_update_text_teletext(obj
,dxs
,dys
);
1166 case OSDTYPE_PROGBAR
:
1167 vo_update_text_progbar(obj
,dxs
,dys
);
1170 if(sub_visibility
&& vo_spudec
&& spudec_visible(vo_spudec
)){
1171 vo_update_spudec_sub(obj
, dxs
, dys
);
1172 obj
->flags
|=OSDFLAG_VISIBLE
|OSDFLAG_CHANGED
;
1175 obj
->flags
&=~OSDFLAG_VISIBLE
;
1178 if(vo_font
&& osd
->osd_text
[0]){
1179 vo_update_text_osd(osd
, obj
, dxs
, dys
); // update bbox
1180 obj
->flags
|=OSDFLAG_VISIBLE
|OSDFLAG_CHANGED
;
1182 obj
->flags
&=~OSDFLAG_VISIBLE
;
1186 if(!(obj
->flags
&OSDFLAG_BBOX
)){
1187 // we don't know, so assume the whole screen changed :(
1188 obj
->bbox
.x1
=obj
->bbox
.y1
=0;
1191 obj
->flags
|=OSDFLAG_BBOX
;
1193 // check bbox, reduce it if it's out of bounds (corners):
1194 if(obj
->bbox
.x1
<0) obj
->bbox
.x1
=0;
1195 if(obj
->bbox
.y1
<0) obj
->bbox
.y1
=0;
1196 if(obj
->bbox
.x2
>dxs
) obj
->bbox
.x2
=dxs
;
1197 if(obj
->bbox
.y2
>dys
) obj
->bbox
.y2
=dys
;
1198 if(obj
->flags
&OSDFLAG_VISIBLE
)
1200 mp_msg(MSGT_OSD
,MSGL_DBG2
,"OSD update: %d;%d %dx%d \n",
1201 obj
->bbox
.x1
,obj
->bbox
.y1
,obj
->bbox
.x2
-obj
->bbox
.x1
,
1202 obj
->bbox
.y2
-obj
->bbox
.y1
);
1204 // check if visibility changed:
1205 if(vis
!= (obj
->flags
&OSDFLAG_VISIBLE
) ) obj
->flags
|=OSDFLAG_CHANGED
;
1206 // remove the cause of automatic update:
1207 obj
->dxs
=dxs
; obj
->dys
=dys
;
1208 obj
->flags
&=~OSDFLAG_FORCE_UPDATE
;
1210 if(obj
->flags
&OSDFLAG_CHANGED
){
1212 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
);
1219 int osd_update(struct osd_state
*osd
, int dxs
, int dys
)
1221 return osd_update_ext(osd
, dxs
, dys
, 0, 0, 0, 0, dxs
, dys
);
1224 struct osd_state
*osd_create(void)
1226 struct osd_state
*osd
= talloc_zero(NULL
, struct osd_state
);
1227 *osd
= (struct osd_state
){
1229 if(!draw_alpha_init_flag
){
1230 draw_alpha_init_flag
=1;
1231 vo_draw_alpha_init();
1233 // temp hack, should be moved to mplayer/mencoder later
1234 new_osd_obj(OSDTYPE_OSD
);
1235 new_osd_obj(OSDTYPE_SUBTITLE
);
1236 new_osd_obj(OSDTYPE_PROGBAR
);
1237 new_osd_obj(OSDTYPE_SPU
);
1238 #ifdef CONFIG_DVDNAV
1239 new_osd_obj(OSDTYPE_DVDNAV
);
1241 new_osd_obj(OSDTYPE_TELETEXT
);
1242 #ifdef CONFIG_FREETYPE
1243 force_load_font
= 1;
1248 int vo_osd_changed_flag
=0;
1250 void osd_remove_text(struct osd_state
*osd
, int dxs
, int dys
,
1251 void (*remove
)(int x0
, int y0
, int w
, int h
))
1253 mp_osd_obj_t
* obj
=vo_osd_list
;
1254 osd_update(osd
, dxs
, dys
);
1256 if(((obj
->flags
&OSDFLAG_CHANGED
) || (obj
->flags
&OSDFLAG_VISIBLE
)) &&
1257 (obj
->flags
&OSDFLAG_OLD_BBOX
)){
1258 int w
=obj
->old_bbox
.x2
-obj
->old_bbox
.x1
;
1259 int h
=obj
->old_bbox
.y2
-obj
->old_bbox
.y1
;
1261 vo_osd_changed_flag
=obj
->flags
&OSDFLAG_CHANGED
; // temp hack
1262 remove(obj
->old_bbox
.x1
,obj
->old_bbox
.y1
,w
,h
);
1264 // obj->flags&=~OSDFLAG_OLD_BBOX;
1270 void osd_draw_text_ext(struct osd_state
*osd
, int dxs
, int dys
,
1271 int left_border
, int top_border
, int right_border
,
1272 int bottom_border
, int orig_w
, int orig_h
,
1273 void (*draw_alpha
)(void *ctx
, int x0
, int y0
, int w
,
1274 int h
, unsigned char* src
,
1275 unsigned char *srca
,
1279 mp_osd_obj_t
* obj
=vo_osd_list
;
1280 osd_update_ext(osd
, dxs
, dys
, left_border
, top_border
, right_border
,
1281 bottom_border
, orig_w
, orig_h
);
1283 if(obj
->flags
&OSDFLAG_VISIBLE
){
1284 vo_osd_changed_flag
=obj
->flags
&OSDFLAG_CHANGED
; // temp hack
1287 vo_draw_spudec_sub(obj
, draw_alpha
, ctx
); // FIXME
1289 #ifdef CONFIG_DVDNAV
1290 case OSDTYPE_DVDNAV
:
1292 case OSDTYPE_TELETEXT
:
1294 case OSDTYPE_SUBTITLE
:
1295 case OSDTYPE_PROGBAR
:
1296 vo_draw_text_from_buffer(obj
, draw_alpha
, ctx
);
1299 obj
->old_bbox
=obj
->bbox
;
1300 obj
->flags
|=OSDFLAG_OLD_BBOX
;
1302 obj
->flags
&=~OSDFLAG_CHANGED
;
1307 void osd_draw_text(struct osd_state
*osd
, int dxs
, int dys
,
1308 void (*draw_alpha
)(void *ctx
, int x0
, int y0
, int w
, int h
,
1309 unsigned char* src
, unsigned char *srca
,
1313 osd_draw_text_ext(osd
, dxs
, dys
, 0, 0, 0, 0, dxs
, dys
, draw_alpha
, ctx
);
1316 static int vo_osd_changed_status
= 0;
1318 int vo_osd_changed(int new_value
)
1320 mp_osd_obj_t
* obj
=vo_osd_list
;
1321 int ret
= vo_osd_changed_status
;
1322 vo_osd_changed_status
= new_value
;
1325 if(obj
->type
==new_value
) obj
->flags
|=OSDFLAG_FORCE_UPDATE
;
1332 // BBBBBBBBBBBB AAAAAAAAAAAAA BBBBBBBBBBB
1333 // BBBBBBBBBBBB BBBBBBBBBBBBB
1336 // return TRUE if we have osd in the specified rectangular area:
1337 int vo_osd_check_range_update(int x1
,int y1
,int x2
,int y2
){
1338 mp_osd_obj_t
* obj
=vo_osd_list
;
1340 if(obj
->flags
&OSDFLAG_VISIBLE
){
1341 if( (obj
->bbox
.x1
<=x2
&& obj
->bbox
.x2
>=x1
) &&
1342 (obj
->bbox
.y1
<=y2
&& obj
->bbox
.y2
>=y1
) &&
1343 obj
->bbox
.y2
> obj
->bbox
.y1
&& obj
->bbox
.x2
> obj
->bbox
.x1