2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "stream/stream.h"
29 #include "stream/stream_dvdnav.h"
30 #define OSD_NAV_BOX_ALPHA 0x7f
32 #include "libmpcodecs/dec_teletext.h"
33 #include "osdep/timer.h"
38 #include "video_out.h"
39 #include "font_load.h"
42 #include "libavutil/common.h"
47 // Structures needed for the new splitting algorithm.
48 // osd_text_t contains the single subtitle word.
49 // osd_text_p is used to mark the lines of subtitles
51 int osd_kerning
, //kerning with the previous word
52 osd_length
, //orizontal length inside the bbox
53 text_length
, //number of characters
55 struct osd_text_t
*prev
,
61 struct osd_text_t
*ott
;
62 struct osd_text_p
*prev
,
67 char * const sub_osd_names
[]={
82 char * const sub_osd_names_short
[] ={ "", "|>", "||", "[]", "<<" , ">>", "", "", "", "", "", "", "" };
84 //static int vo_font_loaded=-1;
85 font_desc_t
* vo_font
=NULL
;
87 void* vo_osd_teletext_page
=NULL
;
88 int vo_osd_teletext_half
= 0;
89 int vo_osd_teletext_mode
=0;
90 int vo_osd_teletext_format
=0;
91 int vo_osd_teletext_scale
=0;
96 int sub_alignment
=2; /* 0=top, 1=center, 2=bottom */
98 int sub_bg_color
=0; /* subtitles background color */
102 static nav_highlight_t nav_hl
;
105 // return the real height of a char:
106 static inline int get_height(int c
,int h
){
108 if ((font
=vo_font
->font
[c
])>=0)
109 if(h
<vo_font
->pic_a
[font
]->h
) h
=vo_font
->pic_a
[font
]->h
;
113 // renders char to a big per-object buffer where alpha and bitmap are separated
114 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
)
116 int dststride
= obj
->stride
;
117 int dstskip
= obj
->stride
-w
;
118 int srcskip
= stride
-w
;
120 unsigned char *b
= obj
->bitmap_buffer
+ (y0
-obj
->bbox
.y1
)*dststride
+ (x0
-obj
->bbox
.x1
);
121 unsigned char *a
= obj
->alpha_buffer
+ (y0
-obj
->bbox
.y1
)*dststride
+ (x0
-obj
->bbox
.x1
);
122 unsigned char *bs
= src
;
123 unsigned char *as
= srca
;
125 if (x0
< obj
->bbox
.x1
|| x0
+w
> obj
->bbox
.x2
|| y0
< obj
->bbox
.y1
|| y0
+h
> obj
->bbox
.y2
) {
126 fprintf(stderr
, "osd text out of range: bbox [%d %d %d %d], txt [%d %d %d %d]\n",
127 obj
->bbox
.x1
, obj
->bbox
.x2
, obj
->bbox
.y1
, obj
->bbox
.y2
,
132 for (i
= 0; i
< h
; i
++) {
133 for (j
= 0; j
< w
; j
++, b
++, a
++, bs
++, as
++) {
134 if (*b
< *bs
) *b
= *bs
;
136 if (*a
== 0 || *a
> *as
) *a
= *as
;
146 // allocates/enlarges the alpha/bitmap buffer
147 static void alloc_buf(mp_osd_obj_t
* obj
)
150 if (obj
->bbox
.x2
< obj
->bbox
.x1
) obj
->bbox
.x2
= obj
->bbox
.x1
;
151 if (obj
->bbox
.y2
< obj
->bbox
.y1
) obj
->bbox
.y2
= obj
->bbox
.y1
;
152 obj
->stride
= ((obj
->bbox
.x2
-obj
->bbox
.x1
)+7)&(~7);
153 len
= obj
->stride
*(obj
->bbox
.y2
-obj
->bbox
.y1
);
154 if (obj
->allocated
<len
) {
155 obj
->allocated
= len
;
156 free(obj
->bitmap_buffer
);
157 free(obj
->alpha_buffer
);
158 obj
->bitmap_buffer
= (unsigned char *)memalign(16, len
);
159 obj
->alpha_buffer
= (unsigned char *)memalign(16, len
);
161 memset(obj
->bitmap_buffer
, sub_bg_color
, len
);
162 memset(obj
->alpha_buffer
, sub_bg_alpha
, len
);
165 // renders the buffer
166 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
)
168 if (obj
->allocated
> 0) {
170 obj
->bbox
.x1
,obj
->bbox
.y1
,
171 obj
->bbox
.x2
-obj
->bbox
.x1
,
172 obj
->bbox
.y2
-obj
->bbox
.y1
,
179 unsigned utf8_get_char(const char **str
) {
180 const uint8_t *strp
= (const uint8_t *)*str
;
182 GET_UTF8(c
, *strp
++, goto no_utf8
;);
183 *str
= (const char *)strp
;
187 strp
= (const uint8_t *)*str
;
189 *str
= (const char *)strp
;
193 inline static void vo_update_text_osd(struct osd_state
*osd
, mp_osd_obj_t
* obj
,
196 const char *cp
= osd
->osd_text
;
201 obj
->bbox
.x1
=obj
->x
=x
;
202 obj
->bbox
.y1
=obj
->y
=10;
205 uint16_t c
=utf8_get_char(&cp
);
206 render_one_glyph(vo_font
, c
);
207 x
+=vo_font
->width
[c
]+vo_font
->charspace
;
211 obj
->bbox
.x2
=x
-vo_font
->charspace
;
212 obj
->bbox
.y2
=obj
->bbox
.y1
+h
;
213 obj
->flags
|=OSDFLAG_BBOX
;
220 uint16_t c
=utf8_get_char(&cp
);
221 if ((font
=vo_font
->font
[c
])>=0)
222 draw_alpha_buf(obj
,x
,obj
->y
,
224 vo_font
->pic_a
[font
]->h
,
225 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
226 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
227 vo_font
->pic_a
[font
]->w
);
228 x
+=vo_font
->width
[c
]+vo_font
->charspace
;
233 void osd_set_nav_box (uint16_t sx
, uint16_t sy
, uint16_t ex
, uint16_t ey
) {
240 inline static void vo_update_nav (mp_osd_obj_t
*obj
, int dxs
, int dys
, int left_border
, int top_border
,
241 int right_border
, int bottom_border
, int orig_w
, int orig_h
) {
243 int sx
= nav_hl
.sx
, sy
= nav_hl
.sy
;
244 int ex
= nav_hl
.ex
, ey
= nav_hl
.ey
;
245 int scaled_w
= dxs
- left_border
- right_border
;
246 int scaled_h
= dys
- top_border
- bottom_border
;
247 if (scaled_w
!= orig_w
) {
248 sx
= sx
* scaled_w
/ orig_w
;
249 ex
= ex
* scaled_w
/ orig_w
;
251 if (scaled_h
!= orig_h
) {
252 sy
= sy
* scaled_h
/ orig_h
;
253 ey
= ey
* scaled_h
/ orig_h
;
255 sx
+= left_border
; ex
+= left_border
;
256 sy
+= top_border
; ey
+= top_border
;
257 sx
= FFMIN(FFMAX(sx
, 0), dxs
);
258 ex
= FFMIN(FFMAX(ex
, 0), dxs
);
259 sy
= FFMIN(FFMAX(sy
, 0), dys
);
260 ey
= FFMIN(FFMAX(ey
, 0), dys
);
262 obj
->bbox
.x1
= obj
->x
= sx
;
263 obj
->bbox
.y1
= obj
->y
= sy
;
268 len
= obj
->stride
* (obj
->bbox
.y2
- obj
->bbox
.y1
);
269 memset (obj
->bitmap_buffer
, OSD_NAV_BOX_ALPHA
, len
);
270 memset (obj
->alpha_buffer
, OSD_NAV_BOX_ALPHA
, len
);
271 obj
->flags
|= OSDFLAG_BBOX
| OSDFLAG_CHANGED
;
272 if (obj
->bbox
.y2
> obj
->bbox
.y1
&& obj
->bbox
.x2
> obj
->bbox
.x1
)
273 obj
->flags
|= OSDFLAG_VISIBLE
;
277 // renders char to a big per-object buffer where alpha and bitmap are separated
278 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
)
280 int dststride
= obj
->stride
;
281 int dstskip
= obj
->stride
-w
;
282 int srcskip
= stride
-w
;
284 unsigned char *b
= obj
->bitmap_buffer
+ (y0
-obj
->bbox
.y1
)*dststride
+ (x0
-obj
->bbox
.x1
);
285 unsigned char *a
= obj
->alpha_buffer
+ (y0
-obj
->bbox
.y1
)*dststride
+ (x0
-obj
->bbox
.x1
);
286 unsigned char *bs
= src
;
287 if (x0
< obj
->bbox
.x1
|| x0
+w
> obj
->bbox
.x2
|| y0
< obj
->bbox
.y1
|| y0
+h
> obj
->bbox
.y2
) {
288 mp_msg(MSGT_OSD
,MSGL_ERR
,"tt osd text out of range: bbox [%d %d %d %d], txt [%d %d %d %d]\n",
289 obj
->bbox
.x1
, obj
->bbox
.x2
, obj
->bbox
.y1
, obj
->bbox
.y2
,
293 for (i
= 0; i
< h
; i
++) {
294 for (j
= 0; j
< w
; j
++, b
++, a
++, bs
++) {
295 *b
=(fg
-bg
)*(*bs
)/255+bg
;
303 inline static void vo_update_text_teletext(mp_osd_obj_t
*obj
, int dxs
, int dys
)
305 int h
=0,w
=0,i
,j
,font
,flashon
;
313 int start_row
,max_rows
;
314 int b
,ax
[6],ay
[6],aw
[6],ah
[6];
316 tt_char
* tdp
=vo_osd_teletext_page
;
317 static const uint8_t colors
[8]={1,85,150,226,70,105,179,254};
318 unsigned char* buf
[9];
320 obj
->flags
|=OSDFLAG_CHANGED
|OSDFLAG_VISIBLE
;
321 if (!tdp
|| !vo_osd_teletext_mode
) {
322 obj
->flags
&=~OSDFLAG_VISIBLE
;
325 flashon
=(GetTimer()/1000000)%2;
326 switch(vo_osd_teletext_half
){
327 case TT_ZOOM_TOP_HALF
:
331 case TT_ZOOM_BOTTOM_HALF
:
332 start_row
=VBI_ROWS
/2;
341 for(i
=start_row
;i
<max_rows
;i
++){
342 for(j
=0;j
<VBI_COLUMNS
;j
++){
343 tc
=tdp
[i
*VBI_COLUMNS
+j
];
344 if(!tc
.ctl
&& !tc
.gfx
)
346 render_one_glyph(vo_font
, tc
.unicode
);
347 if (wm
<vo_font
->width
[tc
.unicode
])
348 wm
=vo_font
->width
[tc
.unicode
];
353 hm
=vo_font
->height
+1;
354 wm
=dxs
*hm
*max_rows
/(dys
*VBI_COLUMNS
);
356 #ifdef CONFIG_FREETYPE
357 //very simple teletext font auto scaling
358 if(!vo_osd_teletext_scale
&& hm
*(max_rows
+1)>dys
){
359 osd_font_scale_factor
*=1.0*(dys
)/((max_rows
+1)*hm
);
361 vo_osd_teletext_scale
=osd_font_scale_factor
;
362 obj
->flags
&=~OSDFLAG_VISIBLE
;
374 w
=cols
*wm
-vo_font
->charspace
;
375 h
=rows
*hm
-vo_font
->charspace
;
415 obj
->flags
|= OSDFLAG_BBOX
;
419 buf
[i
]=malloc(wm
*hm
);
422 if(vo_osd_teletext_format
==TT_FORMAT_OPAQUE
||vo_osd_teletext_format
==TT_FORMAT_OPAQUE_INV
)
426 memset(buf
[8],color
,wm
*hm
);
428 if(vo_osd_teletext_format
==TT_FORMAT_OPAQUE
||vo_osd_teletext_format
==TT_FORMAT_TRANSPARENT
){
430 memset(buf
[i
],(unsigned char)(1.0*(255-color
)*colors
[i
]/255),wm
*hm
);
434 memset(buf
[i
],(unsigned char)(1.0*(255-color
)*colors
[7-i
]/255),wm
*hm
);
441 tc
=tdp
[(i
+start_row
)*VBI_COLUMNS
+j
];
442 if (tc
.hidden
) { x
+=wm
; continue;}
443 if(!tc
.gfx
|| (tc
.flh
&& !flashon
)){
444 /* Rendering one text character */
445 draw_alpha_buf(obj
,x
,y
,wm
,hm
,buf
[tc
.bg
],buf
[8],wm
);
446 if(tc
.unicode
!=0x20 && tc
.unicode
!=0x00 && !tc
.ctl
&&
447 (!tc
.flh
|| flashon
) &&
448 (font
=vo_font
->font
[tc
.unicode
])>=0 && y
+hm
<dys
){
449 tt_draw_alpha_buf(obj
,x
,y
,vo_font
->width
[tc
.unicode
],vo_font
->height
,
450 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[tc
.unicode
]-vo_font
->charspace
*vo_font
->pic_a
[font
]->w
,
451 vo_font
->pic_b
[font
]->w
,
452 buf
[tc
.fg
][0],buf
[tc
.bg
][0],buf
[8][0]);
456 Rendering one graphics character
457 TODO: support for separated graphics symbols (where six rectangles does not touch each other)
461 |10| <= |34| <= 00100110 <= 0x26
465 (0:wm/2) (wm/2:wm-wm/2)
467 ********** *********** (0:hm/3)
469 *** 1 **** **** 2 ****
471 ********** ***********
472 ********** ***********
474 ********** *********** (hm/3:hm-2*hm/3)
475 ********** ***********
477 *** 3 **** **** 4 ****
479 ********** ***********
480 ********** ***********
481 ********** ***********
483 ********** *********** (hm-hm/3:hm/3)
485 *** 5 **** **** 6 ****
487 ********** ***********
488 ********** ***********
491 if(tc
.gfx
>1){ //separated gfx
493 color
=(tc
.unicode
>>b
)&1?tc
.fg
:tc
.bg
;
494 draw_alpha_buf(obj
,x
+ax
[b
]+1,y
+ay
[b
]+1,aw
[b
]-2,ah
[b
]-2,buf
[color
],buf
[8],wm
);
496 //separated gfx (background borders)
498 draw_alpha_buf(obj
,x
,y
,1,hm
,buf
[tc
.bg
],buf
[8],wm
);
499 draw_alpha_buf(obj
,x
+ax
[1]-1,y
,2,hm
,buf
[tc
.bg
],buf
[8],wm
);
500 draw_alpha_buf(obj
,x
+ax
[1]+aw
[1]-1,y
,wm
-ax
[1]-aw
[1]+1,hm
,buf
[tc
.bg
],buf
[8],wm
);
502 draw_alpha_buf(obj
,x
,y
,wm
,1,buf
[tc
.bg
],buf
[8],wm
);
503 draw_alpha_buf(obj
,x
,y
+ay
[0]+ah
[0]-1,wm
,2,buf
[tc
.bg
],buf
[8],wm
);
504 draw_alpha_buf(obj
,x
,y
+ay
[2]+ah
[2]-1,wm
,2,buf
[tc
.bg
],buf
[8],wm
);
505 draw_alpha_buf(obj
,x
,y
+ay
[4]+ah
[4]-1,wm
,hm
-ay
[4]-ah
[4]+1,buf
[tc
.bg
],buf
[8],wm
);
508 color
=(tc
.unicode
>>b
)&1?tc
.fg
:tc
.bg
;
509 draw_alpha_buf(obj
,x
+ax
[b
],y
+ay
[b
],aw
[b
],ah
[b
],buf
[color
],buf
[8],wm
);
521 int vo_osd_progbar_type
=-1;
522 int vo_osd_progbar_value
=100; // 0..256
524 // if we have n=256 bars then OSD progbar looks like below
526 // 0 1 2 3 ... 256 <= vo_osd_progbar_value
528 // [ === === === ... === ]
530 // the above schema is rescalled to n=elems bars
532 inline static void vo_update_text_progbar(mp_osd_obj_t
* obj
,int dxs
,int dys
){
534 obj
->flags
|=OSDFLAG_CHANGED
|OSDFLAG_VISIBLE
;
536 if(vo_osd_progbar_type
<0 || !vo_font
){
537 obj
->flags
&=~OSDFLAG_VISIBLE
;
541 render_one_glyph(vo_font
, OSD_PB_START
);
542 render_one_glyph(vo_font
, OSD_PB_END
);
543 render_one_glyph(vo_font
, OSD_PB_0
);
544 render_one_glyph(vo_font
, OSD_PB_1
);
545 render_one_glyph(vo_font
, vo_osd_progbar_type
);
547 // calculate bbox corners:
549 int y
=(dys
-vo_font
->height
)/2;
550 int delimw
=vo_font
->width
[OSD_PB_START
]
551 +vo_font
->width
[OSD_PB_END
]
553 int width
=(2*dxs
-3*delimw
)/3;
554 int charw
=vo_font
->width
[OSD_PB_0
]+vo_font
->charspace
;
555 int elems
=width
/charw
;
556 int x
=(dxs
-elems
*charw
-delimw
)/2;
558 h
=get_height(OSD_PB_START
,h
);
559 h
=get_height(OSD_PB_END
,h
);
560 h
=get_height(OSD_PB_0
,h
);
561 h
=get_height(OSD_PB_1
,h
);
562 if (vo_osd_progbar_type
>0 && vo_font
->font
[vo_osd_progbar_type
]>=0){
563 delta
= vo_font
->width
[vo_osd_progbar_type
]+vo_font
->spacewidth
;
564 delta
= (x
-delta
> 0) ? delta
: x
;
565 h
=get_height(vo_osd_progbar_type
,h
);
567 obj
->bbox
.x1
=obj
->x
=x
;
568 obj
->bbox
.y1
=obj
->y
=y
;
569 obj
->bbox
.x2
=x
+width
+delimw
;
570 obj
->bbox
.y2
=y
+h
; //vo_font->height;
571 obj
->flags
|=OSDFLAG_BBOX
;
572 obj
->params
.progbar
.elems
=elems
;
573 obj
->bbox
.x1
-=delta
; // space for an icon
579 int minw
= vo_font
->width
[OSD_PB_START
]+vo_font
->width
[OSD_PB_END
]+vo_font
->width
[OSD_PB_0
];
580 if (vo_osd_progbar_type
>0 && vo_font
->font
[vo_osd_progbar_type
]>=0){
581 minw
+= vo_font
->width
[vo_osd_progbar_type
]+vo_font
->charspace
+vo_font
->spacewidth
;
583 if (obj
->bbox
.x2
- obj
->bbox
.x1
< minw
) return; // space too small, don't render anything
593 int charw
=vo_font
->width
[OSD_PB_0
]+vo_font
->charspace
;
594 int elems
=obj
->params
.progbar
.elems
;
596 if (vo_osd_progbar_value
<=0)
599 int ev
=vo_osd_progbar_value
*elems
;
601 if (ev
& 0xFF) mark
++;
602 if (mark
>elems
) mark
=elems
;
606 // printf("osd.progbar width=%d xpos=%d\n",width,x);
608 c
=vo_osd_progbar_type
;
609 if(vo_osd_progbar_type
>0 && (font
=vo_font
->font
[c
])>=0) {
610 int xp
=x
-vo_font
->width
[c
]-vo_font
->spacewidth
;
611 draw_alpha_buf(obj
,(xp
<0?0:xp
),y
,
613 vo_font
->pic_a
[font
]->h
,
614 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
615 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
616 vo_font
->pic_a
[font
]->w
);
620 if ((font
=vo_font
->font
[c
])>=0)
621 draw_alpha_buf(obj
,x
,y
,
623 vo_font
->pic_a
[font
]->h
,
624 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
625 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
626 vo_font
->pic_a
[font
]->w
);
627 x
+=vo_font
->width
[c
]+vo_font
->charspace
;
630 if ((font
=vo_font
->font
[c
])>=0){
632 h
=vo_font
->pic_a
[font
]->h
;
633 s
=vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
];
634 sa
=vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
];
635 st
=vo_font
->pic_a
[font
]->w
;
637 draw_alpha_buf(obj
,x
,y
,w
,h
,s
,sa
,st
);
643 if ((font
=vo_font
->font
[c
])>=0){
645 h
=vo_font
->pic_a
[font
]->h
;
646 s
=vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
];
647 sa
=vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
];
648 st
=vo_font
->pic_a
[font
]->w
;
649 if ((i
=elems
-mark
)) do {
650 draw_alpha_buf(obj
,x
,y
,w
,h
,s
,sa
,st
);
656 if ((font
=vo_font
->font
[c
])>=0)
657 draw_alpha_buf(obj
,x
,y
,
659 vo_font
->pic_a
[font
]->h
,
660 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
661 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
662 vo_font
->pic_a
[font
]->w
);
663 // x+=vo_font->width[c]+vo_font->charspace;
666 // vo_osd_progbar_value=(vo_osd_progbar_value+1)&0xFF;
670 subtitle
* vo_sub
=NULL
;
672 inline static void vo_update_text_sub(struct osd_state
*osd
, mp_osd_obj_t
* obj
,int dxs
,int dys
){
674 int c
,i
,j
,l
,x
,y
,font
,prevc
,counter
;
676 int lastStripPosition
;
681 struct font_desc
*sub_font
= osd
->sub_font
;
683 obj
->flags
|=OSDFLAG_CHANGED
|OSDFLAG_VISIBLE
;
685 if(!vo_sub
|| !osd
->sub_font
|| !sub_visibility
|| (sub_font
->font
[40]<0)){
686 obj
->flags
&=~OSDFLAG_VISIBLE
;
690 obj
->bbox
.y2
=obj
->y
=dys
;
691 obj
->params
.subtitle
.lines
=0;
693 // too long lines divide into a smaller ones
696 lastStripPosition
=-1;
700 struct osd_text_t
*osl
, *cp_ott
, *tmp_ott
, *tmp
;
701 struct osd_text_p
*otp_sub
= NULL
, *otp_sub_tmp
, // these are used to store the whole sub text osd
702 *otp
, *tmp_otp
, *pmt
; // these are used to manage sub text osd coming from a single sub line
703 int *char_seq
, char_position
, xlimit
= dxs
* sub_width_p
/ 100, counter
;
706 xsize
= -sub_font
->charspace
;
710 char_seq
= calloc(strlen(t
), sizeof(int));
718 // reading the subtitle words from vo_sub->text[]
721 c
= utf8_get_char(&t
);
722 else if ((c
= *t
++) >= 0x80 && sub_unicode
)
725 t
+= strlen(t
); // end here
726 mp_msg(MSGT_OSD
,MSGL_WARN
,"\nMAX_UCS exceeded!\n");
728 if (!c
) c
++; // avoid UCS 0
729 render_one_glyph(sub_font
, c
);
732 struct osd_text_t
*tmp_ott
= calloc(1, sizeof(struct osd_text_t
));
735 osl
= cp_ott
= tmp_ott
;
737 tmp_ott
->prev
= cp_ott
;
738 cp_ott
->next
= tmp_ott
;
739 tmp_ott
->osd_kerning
=
740 sub_font
->charspace
+ sub_font
->width
[' '];
743 tmp_ott
->osd_length
= xsize
;
744 tmp_ott
->text_length
= char_position
;
745 tmp_ott
->text
= malloc(char_position
* sizeof(int));
746 for (counter
= 0; counter
< char_position
; ++counter
)
747 tmp_ott
->text
[counter
] = char_seq
[counter
];
752 int delta_xsize
= sub_font
->width
[c
] + sub_font
->charspace
+ kerning(sub_font
, prevc
, c
);
754 if (xsize
+ delta_xsize
<= dxs
) {
757 char_seq
[char_position
++] = c
;
758 xsize
+= delta_xsize
;
759 if ((!suboverlap_enabled
) && ((font
= sub_font
->font
[c
]) >= 0)) {
760 if (sub_font
->pic_a
[font
]->h
> h
) {
761 h
= sub_font
->pic_a
[font
]->h
;
766 mp_msg(MSGT_OSD
, MSGL_WARN
, "\nSubtitle word '%s' too long!\n", t
);
771 }// for len (all words from subtitle line read)
773 // osl holds an ordered (as they appear in the lines) chain of the subtitle words
775 struct osd_text_t
*tmp_ott
= calloc(1, sizeof(struct osd_text_t
));
778 osl
= cp_ott
= tmp_ott
;
780 tmp_ott
->prev
= cp_ott
;
781 cp_ott
->next
= tmp_ott
;
782 tmp_ott
->osd_kerning
=
783 sub_font
->charspace
+ sub_font
->width
[' '];
786 tmp_ott
->osd_length
= xsize
;
787 tmp_ott
->text_length
= char_position
;
788 tmp_ott
->text
= malloc(char_position
* sizeof(int));
789 for (counter
= 0; counter
< char_position
; ++counter
)
790 tmp_ott
->text
[counter
] = char_seq
[counter
];
792 xsize
= -sub_font
->charspace
;
797 int value
= 0, exit
= 0, minimum
= 0;
799 // otp will contain the chain of the osd subtitle lines coming from the single vo_sub line.
800 otp
= tmp_otp
= calloc(1, sizeof(struct osd_text_p
));
802 for (tmp_ott
= tmp_otp
->ott
; exit
== 0; ) {
804 value
+= tmp_ott
->osd_kerning
+ tmp_ott
->osd_length
;
805 tmp_ott
= tmp_ott
->next
;
806 } while ((tmp_ott
!= NULL
) && (value
+ tmp_ott
->osd_kerning
+ tmp_ott
->osd_length
<= xlimit
));
807 if (tmp_ott
!= NULL
) {
808 struct osd_text_p
*tmp
= calloc(1, sizeof(struct osd_text_p
));
810 tmp_otp
->value
= value
;
814 tmp_otp
->ott
= tmp_ott
;
815 value
= -2 * sub_font
->charspace
- sub_font
->width
[' '];
817 tmp_otp
->value
= value
;
824 // minimum holds the 'sum of the differences in length among the lines',
825 // a measure of the evenness of the lengths of the lines
826 for (tmp_otp
= otp
; tmp_otp
->next
!= NULL
; tmp_otp
= tmp_otp
->next
) {
828 while (pmt
!= NULL
) {
829 minimum
+= abs(tmp_otp
->value
- pmt
->value
);
834 if (otp
->next
!= NULL
) {
836 struct osd_text_p
*mem
, *hold
;
839 // until the last word of a line can be moved to the beginning of following line
840 // reducing the 'sum of the differences in length among the lines', it is done
844 for (tmp_otp
= otp
; tmp_otp
->next
!= NULL
; tmp_otp
= tmp_otp
->next
) {
846 for (tmp
= tmp_otp
->ott
; tmp
->next
!= pmt
->ott
; tmp
= tmp
->next
);
847 if (pmt
->value
+ tmp
->osd_length
+ pmt
->ott
->osd_kerning
<= xlimit
) {
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
;
854 for (mem
= otp
; mem
->next
!= NULL
; mem
= mem
->next
) {
856 while (pmt
!= NULL
) {
857 value
+= abs(mem
->value
- pmt
->value
);
861 if (value
< minimum
) {
866 tmp_otp
->value
= mem1
;
867 tmp_otp
->next
->value
= mem2
;
874 for (tmp
= tmp_otp
->ott
; tmp
->next
!= pmt
->ott
; tmp
= tmp
->next
);
875 mem1
= tmp_otp
->value
;
877 tmp_otp
->value
= mem1
- tmp
->osd_length
- tmp
->osd_kerning
;
878 pmt
->value
= mem2
+ tmp
->osd_length
+ pmt
->ott
->osd_kerning
;
882 }//~if(otp->next!=NULL)
885 // adding otp (containing splitted lines) to otp chain
886 if (otp_sub
== NULL
) {
888 for (otp_sub_tmp
= otp_sub
; otp_sub_tmp
->next
!= NULL
; otp_sub_tmp
= otp_sub_tmp
->next
);
892 while (tmp
->next
!= NULL
) tmp
= tmp
->next
;
893 tmp
->next
= otp
->ott
;
894 otp
->ott
->prev
= tmp
;
895 //attaching new subtitle line at the end
896 otp_sub_tmp
->next
= otp
;
897 otp
->prev
= otp_sub_tmp
;
899 otp_sub_tmp
= otp_sub_tmp
->next
;
900 while (otp_sub_tmp
->next
!= NULL
);
905 // write lines into utbl
909 obj
->params
.subtitle
.lines
= 0;
910 for (tmp_otp
= otp_sub
; tmp_otp
!= NULL
; tmp_otp
= tmp_otp
->next
) {
912 if ((obj
->params
.subtitle
.lines
++) >= MAX_UCSLINES
)
915 if (h
> obj
->y
) { // out of the screen so end parsing
916 obj
->y
-= lasth
- sub_font
->height
; // correct the y position
919 xsize
= tmp_otp
->value
;
920 obj
->params
.subtitle
.xtbl
[xtblc
++] = (dxs
- xsize
) / 2;
921 if (xmin
> (dxs
- xsize
) / 2)
922 xmin
= (dxs
- xsize
) / 2;
923 if (xmax
< (dxs
+ xsize
) / 2)
924 xmax
= (dxs
+ xsize
) / 2;
926 tmp
= (tmp_otp
->next
== NULL
) ? NULL
: tmp_otp
->next
->ott
;
927 for (tmp_ott
= tmp_otp
->ott
; tmp_ott
!= tmp
; tmp_ott
= tmp_ott
->next
) {
928 for (counter
= 0; counter
< tmp_ott
->text_length
; ++counter
) {
929 if (utblc
> MAX_UCS
) {
932 c
= tmp_ott
->text
[counter
];
933 render_one_glyph(sub_font
, c
);
934 obj
->params
.subtitle
.utbl
[utblc
++] = c
;
937 obj
->params
.subtitle
.utbl
[utblc
++] = ' ';
939 obj
->params
.subtitle
.utbl
[utblc
- 1] = 0;
940 obj
->y
-= sub_font
->height
;
942 if(obj
->params
.subtitle
.lines
)
943 obj
->y
= dys
- ((obj
->params
.subtitle
.lines
- 1) * sub_font
->height
+ sub_font
->pic_a
[sub_font
->font
[40]]->h
);
946 if (otp_sub
!= NULL
) {
947 for (tmp
= otp_sub
->ott
; tmp
->next
!= NULL
; free(tmp
->prev
)) {
954 for(pmt
= otp_sub
; pmt
->next
!= NULL
; free(pmt
->prev
)) {
961 /// vertical alignment
963 if (sub_alignment
== 2)
964 obj
->y
= dys
* sub_pos
/ 100 - h
;
965 else if (sub_alignment
== 1)
966 obj
->y
= dys
* sub_pos
/ 100 - h
/ 2;
968 obj
->y
= dys
* sub_pos
/ 100;
972 if (obj
->y
> dys
- h
)
975 obj
->bbox
.y2
= obj
->y
+ h
;
978 if (sub_justify
) xmin
= 10;
982 // obj->bbox.y2=obj->y+obj->params.subtitle.lines*sub_font->height;
983 obj
->flags
|=OSDFLAG_BBOX
;
990 switch(vo_sub
->alignment
) {
991 case SUB_ALIGNMENT_BOTTOMLEFT
:
992 case SUB_ALIGNMENT_MIDDLELEFT
:
993 case SUB_ALIGNMENT_TOPLEFT
:
994 obj
->alignment
|= 0x1;
996 case SUB_ALIGNMENT_BOTTOMRIGHT
:
997 case SUB_ALIGNMENT_MIDDLERIGHT
:
998 case SUB_ALIGNMENT_TOPRIGHT
:
999 obj
->alignment
|= 0x2;
1001 case SUB_ALIGNMENT_BOTTOMCENTER
:
1002 case SUB_ALIGNMENT_MIDDLECENTER
:
1003 case SUB_ALIGNMENT_TOPCENTER
:
1005 obj
->alignment
|= 0x0;
1009 if ((l
= obj
->params
.subtitle
.lines
)) {
1010 for(counter
= dxs
; i
< l
; ++i
)
1011 if (obj
->params
.subtitle
.xtbl
[i
] < counter
) counter
= obj
->params
.subtitle
.xtbl
[i
];
1012 for (i
= 0; i
< l
; ++i
) {
1013 switch (obj
->alignment
&0x3) {
1020 x
= 2 * obj
->params
.subtitle
.xtbl
[i
] - counter
- ((obj
->params
.subtitle
.xtbl
[i
] == counter
) ? 0 : 1);
1024 x
= obj
->params
.subtitle
.xtbl
[i
];
1027 while ((c
=obj
->params
.subtitle
.utbl
[j
++])){
1028 x
+= kerning(sub_font
,prevc
,c
);
1029 if ((font
=sub_font
->font
[c
])>=0)
1030 draw_alpha_buf(obj
,x
,y
,
1032 sub_font
->pic_a
[font
]->h
+y
<obj
->dys
? sub_font
->pic_a
[font
]->h
: obj
->dys
-y
,
1033 sub_font
->pic_b
[font
]->bmp
+sub_font
->start
[c
],
1034 sub_font
->pic_a
[font
]->bmp
+sub_font
->start
[c
],
1035 sub_font
->pic_a
[font
]->w
);
1036 x
+=sub_font
->width
[c
]+sub_font
->charspace
;
1039 y
+=sub_font
->height
;
1045 inline static void vo_update_spudec_sub(mp_osd_obj_t
* obj
, int dxs
, int dys
)
1047 unsigned int bbox
[4];
1048 spudec_calc_bbox(vo_spudec
, dxs
, dys
, bbox
);
1049 obj
->bbox
.x1
= bbox
[0];
1050 obj
->bbox
.x2
= bbox
[1];
1051 obj
->bbox
.y1
= bbox
[2];
1052 obj
->bbox
.y2
= bbox
[3];
1053 obj
->flags
|= OSDFLAG_BBOX
;
1056 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
)
1058 spudec_draw_scaled(vo_spudec
, obj
->dxs
, obj
->dys
, draw_alpha
, ctx
);
1061 void *vo_spudec
=NULL
;
1062 void *vo_vobsub
=NULL
;
1064 static int draw_alpha_init_flag
=0;
1066 void vo_draw_alpha_init(void);
1068 mp_osd_obj_t
* vo_osd_list
=NULL
;
1070 static mp_osd_obj_t
* new_osd_obj(int type
){
1071 mp_osd_obj_t
* osd
=malloc(sizeof(mp_osd_obj_t
));
1072 memset(osd
,0,sizeof(mp_osd_obj_t
));
1073 osd
->next
=vo_osd_list
;
1076 osd
->alpha_buffer
= NULL
;
1077 osd
->bitmap_buffer
= NULL
;
1078 osd
->allocated
= -1;
1082 void osd_free(struct osd_state
*osd
)
1084 mp_osd_obj_t
* obj
=vo_osd_list
;
1086 mp_osd_obj_t
* next
=obj
->next
;
1087 free(obj
->alpha_buffer
);
1088 free(obj
->bitmap_buffer
);
1096 #define FONT_LOAD_DEFER 6
1098 static int osd_update_ext(struct osd_state
*osd
, int dxs
, int dys
,
1099 int left_border
, int top_border
, int right_border
,
1100 int bottom_border
, int orig_w
, int orig_h
)
1102 mp_osd_obj_t
* obj
=vo_osd_list
;
1104 #ifdef CONFIG_FREETYPE
1105 static int defer_counter
= 0, prev_dxs
= 0, prev_dys
= 0;
1108 #ifdef CONFIG_FREETYPE
1109 // here is the right place to get screen dimensions
1110 if (((dxs
!= vo_image_width
)
1111 && (subtitle_autoscale
== 2 || subtitle_autoscale
== 3))
1112 || ((dys
!= vo_image_height
)
1113 && (subtitle_autoscale
== 1 || subtitle_autoscale
== 3)))
1115 // screen dimensions changed
1116 // wait a while to avoid useless reloading of the font
1117 if (dxs
== prev_dxs
|| dys
== prev_dys
) {
1124 if (defer_counter
>= FONT_LOAD_DEFER
) force_load_font
= 1;
1127 if (force_load_font
) {
1128 force_load_font
= 0;
1129 load_font_ft(dxs
, dys
, &vo_font
, font_name
, osd_font_scale_factor
);
1131 load_font_ft(dxs
, dys
, &osd
->sub_font
, sub_font_name
, text_font_scale_factor
);
1133 load_font_ft(dxs
, dys
, &osd
->sub_font
, font_name
, text_font_scale_factor
);
1139 load_font_ft(dxs
, dys
, &vo_font
, font_name
, osd_font_scale_factor
);
1140 if (!osd
->sub_font
) {
1142 load_font_ft(dxs
, dys
, &osd
->sub_font
, sub_font_name
, text_font_scale_factor
);
1144 load_font_ft(dxs
, dys
, &osd
->sub_font
, font_name
, text_font_scale_factor
);
1150 if(dxs
!=obj
->dxs
|| dys
!=obj
->dys
|| obj
->flags
&OSDFLAG_FORCE_UPDATE
){
1151 int vis
=obj
->flags
&OSDFLAG_VISIBLE
;
1152 obj
->flags
&=~OSDFLAG_BBOX
;
1154 #ifdef CONFIG_DVDNAV
1155 case OSDTYPE_DVDNAV
:
1156 vo_update_nav(obj
,dxs
,dys
, left_border
, top_border
, right_border
, bottom_border
, orig_w
, orig_h
);
1159 case OSDTYPE_SUBTITLE
:
1160 vo_update_text_sub(osd
, obj
,dxs
,dys
);
1162 case OSDTYPE_TELETEXT
:
1163 vo_update_text_teletext(obj
,dxs
,dys
);
1165 case OSDTYPE_PROGBAR
:
1166 vo_update_text_progbar(obj
,dxs
,dys
);
1169 if(sub_visibility
&& vo_spudec
&& spudec_visible(vo_spudec
)){
1170 vo_update_spudec_sub(obj
, dxs
, dys
);
1171 obj
->flags
|=OSDFLAG_VISIBLE
|OSDFLAG_CHANGED
;
1174 obj
->flags
&=~OSDFLAG_VISIBLE
;
1177 if(vo_font
&& osd
->osd_text
[0]){
1178 vo_update_text_osd(osd
, obj
, dxs
, dys
); // update bbox
1179 obj
->flags
|=OSDFLAG_VISIBLE
|OSDFLAG_CHANGED
;
1181 obj
->flags
&=~OSDFLAG_VISIBLE
;
1185 if(!(obj
->flags
&OSDFLAG_BBOX
)){
1186 // we don't know, so assume the whole screen changed :(
1187 obj
->bbox
.x1
=obj
->bbox
.y1
=0;
1190 obj
->flags
|=OSDFLAG_BBOX
;
1192 // check bbox, reduce it if it's out of bounds (corners):
1193 if(obj
->bbox
.x1
<0) obj
->bbox
.x1
=0;
1194 if(obj
->bbox
.y1
<0) obj
->bbox
.y1
=0;
1195 if(obj
->bbox
.x2
>dxs
) obj
->bbox
.x2
=dxs
;
1196 if(obj
->bbox
.y2
>dys
) obj
->bbox
.y2
=dys
;
1197 if(obj
->flags
&OSDFLAG_VISIBLE
)
1199 mp_msg(MSGT_OSD
,MSGL_DBG2
,"OSD update: %d;%d %dx%d \n",
1200 obj
->bbox
.x1
,obj
->bbox
.y1
,obj
->bbox
.x2
-obj
->bbox
.x1
,
1201 obj
->bbox
.y2
-obj
->bbox
.y1
);
1203 // check if visibility changed:
1204 if(vis
!= (obj
->flags
&OSDFLAG_VISIBLE
) ) obj
->flags
|=OSDFLAG_CHANGED
;
1205 // remove the cause of automatic update:
1206 obj
->dxs
=dxs
; obj
->dys
=dys
;
1207 obj
->flags
&=~OSDFLAG_FORCE_UPDATE
;
1209 if(obj
->flags
&OSDFLAG_CHANGED
){
1211 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
);
1218 int osd_update(struct osd_state
*osd
, int dxs
, int dys
)
1220 return osd_update_ext(osd
, dxs
, dys
, 0, 0, 0, 0, dxs
, dys
);
1223 struct osd_state
*osd_create(void)
1225 struct osd_state
*osd
= talloc_zero(NULL
, struct osd_state
);
1226 *osd
= (struct osd_state
){
1228 if(!draw_alpha_init_flag
){
1229 draw_alpha_init_flag
=1;
1230 vo_draw_alpha_init();
1232 // temp hack, should be moved to mplayer/mencoder later
1233 new_osd_obj(OSDTYPE_OSD
);
1234 new_osd_obj(OSDTYPE_SUBTITLE
);
1235 new_osd_obj(OSDTYPE_PROGBAR
);
1236 new_osd_obj(OSDTYPE_SPU
);
1237 #ifdef CONFIG_DVDNAV
1238 new_osd_obj(OSDTYPE_DVDNAV
);
1240 new_osd_obj(OSDTYPE_TELETEXT
);
1241 #ifdef CONFIG_FREETYPE
1242 force_load_font
= 1;
1247 int vo_osd_changed_flag
=0;
1249 void osd_remove_text(struct osd_state
*osd
, int dxs
, int dys
,
1250 void (*remove
)(int x0
, int y0
, int w
, int h
))
1252 mp_osd_obj_t
* obj
=vo_osd_list
;
1253 osd_update(osd
, dxs
, dys
);
1255 if(((obj
->flags
&OSDFLAG_CHANGED
) || (obj
->flags
&OSDFLAG_VISIBLE
)) &&
1256 (obj
->flags
&OSDFLAG_OLD_BBOX
)){
1257 int w
=obj
->old_bbox
.x2
-obj
->old_bbox
.x1
;
1258 int h
=obj
->old_bbox
.y2
-obj
->old_bbox
.y1
;
1260 vo_osd_changed_flag
=obj
->flags
&OSDFLAG_CHANGED
; // temp hack
1261 remove(obj
->old_bbox
.x1
,obj
->old_bbox
.y1
,w
,h
);
1263 // obj->flags&=~OSDFLAG_OLD_BBOX;
1269 void osd_draw_text_ext(struct osd_state
*osd
, int dxs
, int dys
,
1270 int left_border
, int top_border
, int right_border
,
1271 int bottom_border
, int orig_w
, int orig_h
,
1272 void (*draw_alpha
)(void *ctx
, int x0
, int y0
, int w
,
1273 int h
, unsigned char* src
,
1274 unsigned char *srca
,
1278 mp_osd_obj_t
* obj
=vo_osd_list
;
1279 osd_update_ext(osd
, dxs
, dys
, left_border
, top_border
, right_border
,
1280 bottom_border
, orig_w
, orig_h
);
1282 if(obj
->flags
&OSDFLAG_VISIBLE
){
1283 vo_osd_changed_flag
=obj
->flags
&OSDFLAG_CHANGED
; // temp hack
1286 vo_draw_spudec_sub(obj
, draw_alpha
, ctx
); // FIXME
1288 #ifdef CONFIG_DVDNAV
1289 case OSDTYPE_DVDNAV
:
1291 case OSDTYPE_TELETEXT
:
1293 case OSDTYPE_SUBTITLE
:
1294 case OSDTYPE_PROGBAR
:
1295 vo_draw_text_from_buffer(obj
, draw_alpha
, ctx
);
1298 obj
->old_bbox
=obj
->bbox
;
1299 obj
->flags
|=OSDFLAG_OLD_BBOX
;
1301 obj
->flags
&=~OSDFLAG_CHANGED
;
1306 void osd_draw_text(struct osd_state
*osd
, int dxs
, int dys
,
1307 void (*draw_alpha
)(void *ctx
, int x0
, int y0
, int w
, int h
,
1308 unsigned char* src
, unsigned char *srca
,
1312 osd_draw_text_ext(osd
, dxs
, dys
, 0, 0, 0, 0, dxs
, dys
, draw_alpha
, ctx
);
1315 static int vo_osd_changed_status
= 0;
1317 int vo_osd_changed(int new_value
)
1319 mp_osd_obj_t
* obj
=vo_osd_list
;
1320 int ret
= vo_osd_changed_status
;
1321 vo_osd_changed_status
= new_value
;
1324 if(obj
->type
==new_value
) obj
->flags
|=OSDFLAG_FORCE_UPDATE
;
1331 // BBBBBBBBBBBB AAAAAAAAAAAAA BBBBBBBBBBB
1332 // BBBBBBBBBBBB BBBBBBBBBBBBB
1335 // return TRUE if we have osd in the specified rectangular area:
1336 int vo_osd_check_range_update(int x1
,int y1
,int x2
,int y2
){
1337 mp_osd_obj_t
* obj
=vo_osd_list
;
1339 if(obj
->flags
&OSDFLAG_VISIBLE
){
1340 if( (obj
->bbox
.x1
<=x2
&& obj
->bbox
.x2
>=x1
) &&
1341 (obj
->bbox
.y1
<=y2
&& obj
->bbox
.y2
>=y1
) &&
1342 obj
->bbox
.y2
> obj
->bbox
.y1
&& obj
->bbox
.x2
> obj
->bbox
.x1