Make some functions static.
[mplayer.git] / libvo / sub.c
blobcbf3e5bedd04d0fa3de50c00bd884c21d944fbda
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
6 #include "config.h"
7 #ifdef HAVE_MALLOC_H
8 #include <malloc.h>
9 #endif
11 #include "mp_msg.h"
12 #include "help_mp.h"
13 #include "video_out.h"
14 #include "font_load.h"
15 #include "sub.h"
16 #include "spudec.h"
18 #define NEW_SPLITTING
21 // Structures needed for the new splitting algorithm.
22 // osd_text_t contains the single subtitle word.
23 // osd_text_p is used to mark the lines of subtitles
24 struct osd_text_t {
25 int osd_kerning, //kerning with the previous word
26 osd_length, //orizontal length inside the bbox
27 text_length, //number of characters
28 *text; //characters
29 struct osd_text_t *prev,
30 *next;
33 struct osd_text_p {
34 int value;
35 struct osd_text_t *ott;
36 struct osd_text_p *prev,
37 *next;
39 //^
41 char * __sub_osd_names[]={
42 MSGTR_VO_SUB_Seekbar,
43 MSGTR_VO_SUB_Play,
44 MSGTR_VO_SUB_Pause,
45 MSGTR_VO_SUB_Stop,
46 MSGTR_VO_SUB_Rewind,
47 MSGTR_VO_SUB_Forward,
48 MSGTR_VO_SUB_Clock,
49 MSGTR_VO_SUB_Contrast,
50 MSGTR_VO_SUB_Saturation,
51 MSGTR_VO_SUB_Volume,
52 MSGTR_VO_SUB_Brightness,
53 MSGTR_VO_SUB_Hue
55 char * __sub_osd_names_short[] ={ "", "|>", "||", "[]", "<<" , ">>", "", "", "", "", "", ""};
57 //static int vo_font_loaded=-1;
58 font_desc_t* vo_font=NULL;
60 unsigned char* vo_osd_text=NULL;
61 int sub_unicode=0;
62 int sub_utf8=0;
63 int sub_pos=100;
64 int sub_width_p=100;
65 int sub_alignment=2; /* 0=top, 1=center, 2=bottom */
66 int sub_visibility=1;
67 int sub_bg_color=0; /* subtitles background color */
68 int sub_bg_alpha=0;
69 int sub_justify=0;
71 // return the real height of a char:
72 static inline int get_height(int c,int h){
73 int font;
74 if ((font=vo_font->font[c])>=0)
75 if(h<vo_font->pic_a[font]->h) h=vo_font->pic_a[font]->h;
76 return h;
79 // renders char to a big per-object buffer where alpha and bitmap are separated
80 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)
82 int dststride = obj->stride;
83 int dstskip = obj->stride-w;
84 int srcskip = stride-w;
85 int i, j;
86 unsigned char *b = obj->bitmap_buffer + (y0-obj->bbox.y1)*dststride + (x0-obj->bbox.x1);
87 unsigned char *a = obj->alpha_buffer + (y0-obj->bbox.y1)*dststride + (x0-obj->bbox.x1);
88 unsigned char *bs = src;
89 unsigned char *as = srca;
91 if (x0 < obj->bbox.x1 || x0+w > obj->bbox.x2 || y0 < obj->bbox.y1 || y0+h > obj->bbox.y2) {
92 fprintf(stderr, "osd text out of range: bbox [%d %d %d %d], txt [%d %d %d %d]\n",
93 obj->bbox.x1, obj->bbox.x2, obj->bbox.y1, obj->bbox.y2,
94 x0, x0+w, y0, y0+h);
95 return;
98 for (i = 0; i < h; i++) {
99 for (j = 0; j < w; j++, b++, a++, bs++, as++) {
100 if (*b < *bs) *b = *bs;
101 if (*as) {
102 if (*a == 0 || *a > *as) *a = *as;
105 b+= dstskip;
106 a+= dstskip;
107 bs+= srcskip;
108 as+= srcskip;
112 // allocates/enlarges the alpha/bitmap buffer
113 static void alloc_buf(mp_osd_obj_t* obj)
115 int len;
116 if (obj->bbox.x2 < obj->bbox.x1) obj->bbox.x2 = obj->bbox.x1;
117 if (obj->bbox.y2 < obj->bbox.y1) obj->bbox.y2 = obj->bbox.y1;
118 obj->stride = ((obj->bbox.x2-obj->bbox.x1)+7)&(~7);
119 len = obj->stride*(obj->bbox.y2-obj->bbox.y1);
120 if (obj->allocated<len) {
121 obj->allocated = len;
122 free(obj->bitmap_buffer);
123 free(obj->alpha_buffer);
124 obj->bitmap_buffer = (unsigned char *)memalign(16, len);
125 obj->alpha_buffer = (unsigned char *)memalign(16, len);
127 memset(obj->bitmap_buffer, sub_bg_color, len);
128 memset(obj->alpha_buffer, sub_bg_alpha, len);
131 // renders the buffer
132 inline static void vo_draw_text_from_buffer(mp_osd_obj_t* obj,void (*draw_alpha)(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride)){
133 if (obj->allocated > 0) {
134 draw_alpha(obj->bbox.x1,obj->bbox.y1,
135 obj->bbox.x2-obj->bbox.x1,
136 obj->bbox.y2-obj->bbox.y1,
137 obj->bitmap_buffer,
138 obj->alpha_buffer,
139 obj->stride);
143 unsigned utf8_get_char(char **str) {
144 uint8_t *strp = (uint8_t *)*str;
145 unsigned c = *strp++;
146 unsigned mask = 0x80;
147 int len = -1;
148 while (c & mask) {
149 mask >>= 1;
150 len++;
152 if (len <= 0 || len > 4)
153 goto no_utf8;
154 c &= mask - 1;
155 while ((*strp & 0xc0) == 0x80) {
156 if (len-- <= 0)
157 goto no_utf8;
158 c = (c << 6) | (*strp++ & 0x3f);
160 if (len)
161 goto no_utf8;
162 *str = (char *)strp;
163 return c;
165 no_utf8:
166 strp = (uint8_t *)*str;
167 c = *strp++;
168 *str = (char *)strp;
169 return c;
172 inline static void vo_update_text_osd(mp_osd_obj_t* obj,int dxs,int dys){
173 unsigned char *cp=vo_osd_text;
174 int x=20;
175 int h=0;
176 int font;
178 obj->bbox.x1=obj->x=x;
179 obj->bbox.y1=obj->y=10;
181 while (*cp){
182 uint16_t c=utf8_get_char(&cp);
183 render_one_glyph(vo_font, c);
184 x+=vo_font->width[c]+vo_font->charspace;
185 h=get_height(c,h);
188 obj->bbox.x2=x-vo_font->charspace;
189 obj->bbox.y2=obj->bbox.y1+h;
190 obj->flags|=OSDFLAG_BBOX;
192 alloc_buf(obj);
194 cp=vo_osd_text;
195 x = obj->x;
196 while (*cp){
197 uint16_t c=utf8_get_char(&cp);
198 if ((font=vo_font->font[c])>=0)
199 draw_alpha_buf(obj,x,obj->y,
200 vo_font->width[c],
201 vo_font->pic_a[font]->h,
202 vo_font->pic_b[font]->bmp+vo_font->start[c],
203 vo_font->pic_a[font]->bmp+vo_font->start[c],
204 vo_font->pic_a[font]->w);
205 x+=vo_font->width[c]+vo_font->charspace;
209 int vo_osd_progbar_type=-1;
210 int vo_osd_progbar_value=100; // 0..256
212 // if we have n=256 bars then OSD progbar looks like below
214 // 0 1 2 3 ... 256 <= vo_osd_progbar_value
215 // | | | | |
216 // [ === === === ... === ]
218 // the above schema is rescalled to n=elems bars
220 inline static void vo_update_text_progbar(mp_osd_obj_t* obj,int dxs,int dys){
222 obj->flags|=OSDFLAG_CHANGED|OSDFLAG_VISIBLE;
224 if(vo_osd_progbar_type<0 || !vo_font){
225 obj->flags&=~OSDFLAG_VISIBLE;
226 return;
229 render_one_glyph(vo_font, OSD_PB_START);
230 render_one_glyph(vo_font, OSD_PB_END);
231 render_one_glyph(vo_font, OSD_PB_0);
232 render_one_glyph(vo_font, OSD_PB_1);
233 render_one_glyph(vo_font, vo_osd_progbar_type);
235 // calculate bbox corners:
236 { int h=0;
237 int y=(dys-vo_font->height)/2;
238 int delimw=vo_font->width[OSD_PB_START]
239 +vo_font->width[OSD_PB_END]
240 +vo_font->charspace;
241 int width=(2*dxs-3*delimw)/3;
242 int charw=vo_font->width[OSD_PB_0]+vo_font->charspace;
243 int elems=width/charw;
244 int x=(dxs-elems*charw-delimw)/2;
245 int delta = 0;
246 h=get_height(OSD_PB_START,h);
247 h=get_height(OSD_PB_END,h);
248 h=get_height(OSD_PB_0,h);
249 h=get_height(OSD_PB_1,h);
250 if (vo_osd_progbar_type>0 && vo_font->font[vo_osd_progbar_type]>=0){
251 delta = vo_font->width[vo_osd_progbar_type]+vo_font->spacewidth;
252 delta = (x-delta > 0) ? delta : x;
253 h=get_height(vo_osd_progbar_type,h);
255 obj->bbox.x1=obj->x=x;
256 obj->bbox.y1=obj->y=y;
257 obj->bbox.x2=x+width+delimw;
258 obj->bbox.y2=y+h; //vo_font->height;
259 obj->flags|=OSDFLAG_BBOX;
260 obj->params.progbar.elems=elems;
261 obj->bbox.x1-=delta; // space for an icon
264 alloc_buf(obj);
267 int minw = vo_font->width[OSD_PB_START]+vo_font->width[OSD_PB_END]+vo_font->width[OSD_PB_0];
268 if (vo_osd_progbar_type>0 && vo_font->font[vo_osd_progbar_type]>=0){
269 minw += vo_font->width[vo_osd_progbar_type]+vo_font->charspace+vo_font->spacewidth;
271 if (obj->bbox.x2 - obj->bbox.x1 < minw) return; // space too small, don't render anything
274 // render it:
275 { unsigned char *s;
276 unsigned char *sa;
277 int i,w,h,st,mark;
278 int x=obj->x;
279 int y=obj->y;
280 int c,font;
281 int charw=vo_font->width[OSD_PB_0]+vo_font->charspace;
282 int elems=obj->params.progbar.elems;
284 if (vo_osd_progbar_value<=0)
285 mark=0;
286 else {
287 int ev=vo_osd_progbar_value*elems;
288 mark=ev>>8;
289 if (ev & 0xFF) mark++;
290 if (mark>elems) mark=elems;
294 // printf("osd.progbar width=%d xpos=%d\n",width,x);
296 c=vo_osd_progbar_type;
297 if(vo_osd_progbar_type>0 && (font=vo_font->font[c])>=0) {
298 int xp=x-vo_font->width[c]-vo_font->spacewidth;
299 draw_alpha_buf(obj,(xp<0?0:xp),y,
300 vo_font->width[c],
301 vo_font->pic_a[font]->h,
302 vo_font->pic_b[font]->bmp+vo_font->start[c],
303 vo_font->pic_a[font]->bmp+vo_font->start[c],
304 vo_font->pic_a[font]->w);
307 c=OSD_PB_START;
308 if ((font=vo_font->font[c])>=0)
309 draw_alpha_buf(obj,x,y,
310 vo_font->width[c],
311 vo_font->pic_a[font]->h,
312 vo_font->pic_b[font]->bmp+vo_font->start[c],
313 vo_font->pic_a[font]->bmp+vo_font->start[c],
314 vo_font->pic_a[font]->w);
315 x+=vo_font->width[c]+vo_font->charspace;
317 c=OSD_PB_0;
318 if ((font=vo_font->font[c])>=0){
319 w=vo_font->width[c];
320 h=vo_font->pic_a[font]->h;
321 s=vo_font->pic_b[font]->bmp+vo_font->start[c];
322 sa=vo_font->pic_a[font]->bmp+vo_font->start[c];
323 st=vo_font->pic_a[font]->w;
324 if ((i=mark)) do {
325 draw_alpha_buf(obj,x,y,w,h,s,sa,st);
326 x+=charw;
327 } while(--i);
330 c=OSD_PB_1;
331 if ((font=vo_font->font[c])>=0){
332 w=vo_font->width[c];
333 h=vo_font->pic_a[font]->h;
334 s =vo_font->pic_b[font]->bmp+vo_font->start[c];
335 sa=vo_font->pic_a[font]->bmp+vo_font->start[c];
336 st=vo_font->pic_a[font]->w;
337 if ((i=elems-mark)) do {
338 draw_alpha_buf(obj,x,y,w,h,s,sa,st);
339 x+=charw;
340 } while(--i);
343 c=OSD_PB_END;
344 if ((font=vo_font->font[c])>=0)
345 draw_alpha_buf(obj,x,y,
346 vo_font->width[c],
347 vo_font->pic_a[font]->h,
348 vo_font->pic_b[font]->bmp+vo_font->start[c],
349 vo_font->pic_a[font]->bmp+vo_font->start[c],
350 vo_font->pic_a[font]->w);
351 // x+=vo_font->width[c]+vo_font->charspace;
354 // vo_osd_progbar_value=(vo_osd_progbar_value+1)&0xFF;
358 subtitle* vo_sub=NULL;
360 // vo_draw_text_sub(int dxs,int dys,void (*draw_alpha)(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride))
362 inline static void vo_update_text_sub(mp_osd_obj_t* obj,int dxs,int dys){
363 unsigned char *t;
364 int c,i,j,l,x,y,font,prevc,counter;
365 int len;
366 int k;
367 int lastStripPosition;
368 int xsize;
369 int xmin=dxs,xmax=0;
370 int h,lasth;
371 int xtblc, utblc;
373 obj->flags|=OSDFLAG_CHANGED|OSDFLAG_VISIBLE;
375 if(!vo_sub || !vo_font || !sub_visibility || (vo_font->font[40]<0)){
376 obj->flags&=~OSDFLAG_VISIBLE;
377 return;
380 obj->bbox.y2=obj->y=dys;
381 obj->params.subtitle.lines=0;
383 // too long lines divide into a smaller ones
384 i=k=lasth=0;
385 h=vo_font->height;
386 lastStripPosition=-1;
387 l=vo_sub->lines;
390 struct osd_text_t *osl, *cp_ott, *tmp_ott, *tmp;
391 struct osd_text_p *otp_sub = NULL, *otp_sub_tmp, // these are used to store the whole sub text osd
392 *otp, *tmp_otp, *pmt; // these are used to manage sub text osd coming from a single sub line
393 int *char_seq, char_position, xlimit = dxs * sub_width_p / 100, counter;
395 while (l) {
396 xsize = -vo_font->charspace;
397 l--;
398 t=vo_sub->text[i++];
399 len=strlen(t)-1;
400 char_position = 0;
401 char_seq = (int *) malloc((len + 1) * sizeof(int));
403 prevc = -1;
405 otp = NULL;
406 osl = NULL;
407 x = 1;
409 // reading the subtitle words from vo_sub->text[]
410 for (j=0;j<=len;j++){
411 if ((c=t[j])>=0x80){
412 if (sub_utf8){
413 if ((c & 0xe0) == 0xc0) /* 2 bytes U+00080..U+0007FF*/
414 c = (c & 0x1f)<<6 | (t[++j] & 0x3f);
415 else if((c & 0xf0) == 0xe0){ /* 3 bytes U+00800..U+00FFFF*/
416 c = (((c & 0x0f)<<6) | (t[++j] & 0x3f))<<6;
417 c |= (t[++j] & 0x3f);
419 } else if (sub_unicode)
420 c = (c<<8) + t[++j];
422 if (k==MAX_UCS){
423 len=j; // end here
424 mp_msg(MSGT_OSD,MSGL_WARN,"\nMAX_UCS exceeded!\n");
426 if (!c) c++; // avoid UCS 0
427 render_one_glyph(vo_font, c);
429 if (c == ' ') {
430 struct osd_text_t *tmp_ott = (struct osd_text_t *) calloc(1, sizeof(struct osd_text_t));
432 if (osl == NULL) {
433 osl = cp_ott = tmp_ott;
434 } else {
435 tmp_ott->prev = cp_ott;
436 cp_ott->next = tmp_ott;
437 tmp_ott->osd_kerning =
438 vo_font->charspace + vo_font->width[' '];
439 cp_ott = tmp_ott;
441 tmp_ott->osd_length = xsize;
442 tmp_ott->text_length = char_position;
443 tmp_ott->text = (int *) malloc(char_position * sizeof(int));
444 for (counter = 0; counter < char_position; ++counter)
445 tmp_ott->text[counter] = char_seq[counter];
446 char_position = 0;
447 xsize = 0;
448 prevc = c;
449 } else {
450 int delta_xsize = vo_font->width[c] + vo_font->charspace + kerning(vo_font, prevc, c);
452 if (xsize + delta_xsize <= dxs) {
453 if (!x) x = 1;
454 prevc = c;
455 char_seq[char_position++] = c;
456 xsize += delta_xsize;
457 if ((!suboverlap_enabled) && ((font = vo_font->font[c]) >= 0)) {
458 if (vo_font->pic_a[font]->h > h) {
459 h = vo_font->pic_a[font]->h;
462 } else {
463 if (x) {
464 mp_msg(MSGT_OSD, MSGL_WARN, "\nSubtitle word '%s' too long!\n", t);
465 x = 0;
469 }// for len (all words from subtitle line read)
471 // osl holds an ordered (as they appear in the lines) chain of the subtitle words
473 struct osd_text_t *tmp_ott = (struct osd_text_t *) calloc(1, sizeof(struct osd_text_t));
475 if (osl == NULL) {
476 osl = cp_ott = tmp_ott;
477 } else {
478 tmp_ott->prev = cp_ott;
479 cp_ott->next = tmp_ott;
480 tmp_ott->osd_kerning =
481 vo_font->charspace + vo_font->width[' '];
482 cp_ott = tmp_ott;
484 tmp_ott->osd_length = xsize;
485 tmp_ott->text_length = char_position;
486 tmp_ott->text = (int *) malloc(char_position * sizeof(int));
487 for (counter = 0; counter < char_position; ++counter)
488 tmp_ott->text[counter] = char_seq[counter];
489 char_position = 0;
490 xsize = -vo_font->charspace;
492 free(char_seq);
494 if (osl != NULL) {
495 int value = 0, exit = 0, minimum = 0;
497 // otp will contain the chain of the osd subtitle lines coming from the single vo_sub line.
498 otp = tmp_otp = (struct osd_text_p *) calloc(1, sizeof(struct osd_text_p));
499 tmp_otp->ott = osl;
500 for (tmp_ott = tmp_otp->ott; exit == 0; ) {
501 do {
502 value += tmp_ott->osd_kerning + tmp_ott->osd_length;
503 tmp_ott = tmp_ott->next;
504 } while ((tmp_ott != NULL) && (value + tmp_ott->osd_kerning + tmp_ott->osd_length <= xlimit));
505 if (tmp_ott != NULL) {
506 struct osd_text_p *tmp = (struct osd_text_p *) calloc(1, sizeof(struct osd_text_p));
508 tmp_otp->value = value;
509 tmp_otp->next = tmp;
510 tmp->prev = tmp_otp;
511 tmp_otp = tmp;
512 tmp_otp->ott = tmp_ott;
513 value = -2 * vo_font->charspace - vo_font->width[' '];
514 } else {
515 tmp_otp->value = value;
516 exit = 1;
521 #ifdef NEW_SPLITTING
522 // minimum holds the 'sum of the differences in lenght among the lines',
523 // a measure of the eveness of the lenghts of the lines
524 for (tmp_otp = otp; tmp_otp->next != NULL; tmp_otp = tmp_otp->next) {
525 pmt = tmp_otp->next;
526 while (pmt != NULL) {
527 minimum += abs(tmp_otp->value - pmt->value);
528 pmt = pmt->next;
532 if (otp->next != NULL) {
533 int mem1, mem2;
534 struct osd_text_p *mem, *hold;
536 exit = 0;
537 // until the last word of a line can be moved to the beginning of following line
538 // reducing the 'sum of the differences in lenght among the lines', it is done
539 while (exit == 0) {
540 hold = NULL;
541 exit = 1;
542 for (tmp_otp = otp; tmp_otp->next != NULL; tmp_otp = tmp_otp->next) {
543 pmt = tmp_otp->next;
544 for (tmp = tmp_otp->ott; tmp->next != pmt->ott; tmp = tmp->next);
545 if (pmt->value + tmp->osd_length + pmt->ott->osd_kerning <= xlimit) {
546 mem1 = tmp_otp->value;
547 mem2 = pmt->value;
548 tmp_otp->value = mem1 - tmp->osd_length - tmp->osd_kerning;
549 pmt->value = mem2 + tmp->osd_length + pmt->ott->osd_kerning;
551 value = 0;
552 for (mem = otp; mem->next != NULL; mem = mem->next) {
553 pmt = mem->next;
554 while (pmt != NULL) {
555 value += abs(mem->value - pmt->value);
556 pmt = pmt->next;
559 if (value < minimum) {
560 minimum = value;
561 hold = tmp_otp;
562 exit = 0;
564 tmp_otp->value = mem1;
565 tmp_otp->next->value = mem2;
568 // merging
569 if (exit == 0) {
570 tmp_otp = hold;
571 pmt = tmp_otp->next;
572 for (tmp = tmp_otp->ott; tmp->next != pmt->ott; tmp = tmp->next);
573 mem1 = tmp_otp->value;
574 mem2 = pmt->value;
575 tmp_otp->value = mem1 - tmp->osd_length - tmp->osd_kerning;
576 pmt->value = mem2 + tmp->osd_length + pmt->ott->osd_kerning;
577 pmt->ott = tmp;
578 }//~merging
579 }//~while(exit == 0)
580 }//~if(otp->next!=NULL)
581 #endif
583 // adding otp (containing splitted lines) to otp chain
584 if (otp_sub == NULL) {
585 otp_sub = otp;
586 for (otp_sub_tmp = otp_sub; otp_sub_tmp->next != NULL; otp_sub_tmp = otp_sub_tmp->next);
587 } else {
588 //updating ott chain
589 tmp = otp_sub->ott;
590 while (tmp->next != NULL) tmp = tmp->next;
591 tmp->next = otp->ott;
592 otp->ott->prev = tmp;
593 //attaching new subtitle line at the end
594 otp_sub_tmp->next = otp;
595 otp->prev = otp_sub_tmp;
597 otp_sub_tmp = otp_sub_tmp->next;
598 while (otp_sub_tmp->next != NULL);
600 }//~ if(osl != NULL)
601 } // while
603 // write lines into utbl
604 xtblc = 0;
605 utblc = 0;
606 obj->y = dys;
607 obj->params.subtitle.lines = 0;
608 for (tmp_otp = otp_sub; tmp_otp != NULL; tmp_otp = tmp_otp->next) {
610 if ((obj->params.subtitle.lines++) >= MAX_UCSLINES)
611 break;
613 if (h > obj->y) { // out of the screen so end parsing
614 obj->y -= lasth - vo_font->height; // correct the y position
615 break;
617 xsize = tmp_otp->value;
618 obj->params.subtitle.xtbl[xtblc++] = (dxs - xsize) / 2;
619 if (xmin > (dxs - xsize) / 2)
620 xmin = (dxs - xsize) / 2;
621 if (xmax < (dxs + xsize) / 2)
622 xmax = (dxs + xsize) / 2;
624 tmp = (tmp_otp->next == NULL) ? NULL : tmp_otp->next->ott;
625 for (tmp_ott = tmp_otp->ott; tmp_ott != tmp; tmp_ott = tmp_ott->next) {
626 for (counter = 0; counter < tmp_ott->text_length; ++counter) {
627 if (utblc > MAX_UCS) {
628 break;
630 c = tmp_ott->text[counter];
631 render_one_glyph(vo_font, c);
632 obj->params.subtitle.utbl[utblc++] = c;
633 k++;
635 obj->params.subtitle.utbl[utblc++] = ' ';
637 obj->params.subtitle.utbl[utblc - 1] = 0;
638 obj->y -= vo_font->height;
640 if(obj->params.subtitle.lines)
641 obj->y = dys - ((obj->params.subtitle.lines - 1) * vo_font->height + vo_font->pic_a[vo_font->font[40]]->h);
643 // free memory
644 if (otp_sub != NULL) {
645 for (tmp = otp_sub->ott; tmp->next != NULL; free(tmp->prev)) {
646 free(tmp->text);
647 tmp = tmp->next;
649 free(tmp->text);
650 free(tmp);
652 for(pmt = otp_sub; pmt->next != NULL; free(pmt->prev)) {
653 pmt = pmt->next;
655 free(pmt);
659 /// vertical alignment
660 h = dys - obj->y;
661 if (sub_alignment == 2)
662 obj->y = dys * sub_pos / 100 - h;
663 else if (sub_alignment == 1)
664 obj->y = dys * sub_pos / 100 - h / 2;
665 else
666 obj->y = dys * sub_pos / 100;
668 if (obj->y < 0)
669 obj->y = 0;
670 if (obj->y > dys - h)
671 obj->y = dys - h;
673 obj->bbox.y2 = obj->y + h;
675 // calculate bbox:
676 if (sub_justify) xmin = 10;
677 obj->bbox.x1=xmin;
678 obj->bbox.x2=xmax;
679 obj->bbox.y1=obj->y;
680 // obj->bbox.y2=obj->y+obj->params.subtitle.lines*vo_font->height;
681 obj->flags|=OSDFLAG_BBOX;
683 alloc_buf(obj);
685 y = obj->y;
687 obj->alignment = 0;
688 switch(vo_sub->alignment) {
689 case SUB_ALIGNMENT_BOTTOMLEFT:
690 case SUB_ALIGNMENT_MIDDLELEFT:
691 case SUB_ALIGNMENT_TOPLEFT:
692 obj->alignment |= 0x1;
693 break;
694 case SUB_ALIGNMENT_BOTTOMRIGHT:
695 case SUB_ALIGNMENT_MIDDLERIGHT:
696 case SUB_ALIGNMENT_TOPRIGHT:
697 obj->alignment |= 0x2;
698 break;
699 case SUB_ALIGNMENT_BOTTOMCENTER:
700 case SUB_ALIGNMENT_MIDDLECENTER:
701 case SUB_ALIGNMENT_TOPCENTER:
702 default:
703 obj->alignment |= 0x0;
706 i=j=0;
707 if ((l = obj->params.subtitle.lines)) {
708 for(counter = dxs; i < l; ++i)
709 if (obj->params.subtitle.xtbl[i] < counter) counter = obj->params.subtitle.xtbl[i];
710 for (i = 0; i < l; ++i) {
711 switch (obj->alignment&0x3) {
712 case 1:
713 // left
714 x = counter;
715 break;
716 case 2:
717 // right
718 x = 2 * obj->params.subtitle.xtbl[i] - counter - ((obj->params.subtitle.xtbl[i] == counter) ? 0 : 1);
719 break;
720 default:
721 //center
722 x = obj->params.subtitle.xtbl[i];
724 prevc = -1;
725 while ((c=obj->params.subtitle.utbl[j++])){
726 x += kerning(vo_font,prevc,c);
727 if ((font=vo_font->font[c])>=0)
728 draw_alpha_buf(obj,x,y,
729 vo_font->width[c],
730 vo_font->pic_a[font]->h+y<obj->dys ? vo_font->pic_a[font]->h : obj->dys-y,
731 vo_font->pic_b[font]->bmp+vo_font->start[c],
732 vo_font->pic_a[font]->bmp+vo_font->start[c],
733 vo_font->pic_a[font]->w);
734 x+=vo_font->width[c]+vo_font->charspace;
735 prevc = c;
737 y+=vo_font->height;
743 inline static void vo_update_spudec_sub(mp_osd_obj_t* obj, int dxs, int dys)
745 unsigned int bbox[4];
746 spudec_calc_bbox(vo_spudec, dxs, dys, bbox);
747 obj->bbox.x1 = bbox[0];
748 obj->bbox.x2 = bbox[1];
749 obj->bbox.y1 = bbox[2];
750 obj->bbox.y2 = bbox[3];
751 obj->flags |= OSDFLAG_BBOX;
754 inline static void vo_draw_spudec_sub(mp_osd_obj_t* obj, void (*draw_alpha)(int x0, int y0, int w, int h, unsigned char* src, unsigned char* srca, int stride))
756 spudec_draw_scaled(vo_spudec, obj->dxs, obj->dys, draw_alpha);
759 void *vo_spudec=NULL;
760 void *vo_vobsub=NULL;
762 static int draw_alpha_init_flag=0;
764 extern void vo_draw_alpha_init(void);
766 mp_osd_obj_t* vo_osd_list=NULL;
768 static mp_osd_obj_t* new_osd_obj(int type){
769 mp_osd_obj_t* osd=malloc(sizeof(mp_osd_obj_t));
770 memset(osd,0,sizeof(mp_osd_obj_t));
771 osd->next=vo_osd_list;
772 vo_osd_list=osd;
773 osd->type=type;
774 osd->alpha_buffer = NULL;
775 osd->bitmap_buffer = NULL;
776 osd->allocated = -1;
777 return osd;
780 void free_osd_list(void){
781 mp_osd_obj_t* obj=vo_osd_list;
782 while(obj){
783 mp_osd_obj_t* next=obj->next;
784 if (obj->alpha_buffer) free(obj->alpha_buffer);
785 if (obj->bitmap_buffer) free(obj->bitmap_buffer);
786 free(obj);
787 obj=next;
789 vo_osd_list=NULL;
792 #define FONT_LOAD_DEFER 6
794 int vo_update_osd(int dxs,int dys){
795 mp_osd_obj_t* obj=vo_osd_list;
796 int chg=0;
797 #ifdef HAVE_FREETYPE
798 static int defer_counter = 0, prev_dxs = 0, prev_dys = 0;
799 #endif
801 #ifdef HAVE_FREETYPE
802 // here is the right place to get screen dimensions
803 if (((dxs != vo_image_width)
804 && (subtitle_autoscale == 2 || subtitle_autoscale == 3))
805 || ((dys != vo_image_height)
806 && (subtitle_autoscale == 1 || subtitle_autoscale == 3)))
808 // screen dimensions changed
809 // wait a while to avoid useless reloading of the font
810 if (dxs == prev_dxs || dys == prev_dys) {
811 defer_counter++;
812 } else {
813 prev_dxs = dxs;
814 prev_dys = dys;
815 defer_counter = 0;
817 if (defer_counter >= FONT_LOAD_DEFER) force_load_font = 1;
820 if (!vo_font || force_load_font) {
821 force_load_font = 0;
822 load_font_ft(dxs, dys);
823 prev_dxs = dxs;
824 prev_dys = dys;
825 defer_counter = 0;
827 #endif
829 while(obj){
830 if(dxs!=obj->dxs || dys!=obj->dys || obj->flags&OSDFLAG_FORCE_UPDATE){
831 int vis=obj->flags&OSDFLAG_VISIBLE;
832 obj->flags&=~OSDFLAG_BBOX;
833 switch(obj->type){
834 case OSDTYPE_SUBTITLE:
835 vo_update_text_sub(obj,dxs,dys);
836 break;
837 case OSDTYPE_PROGBAR:
838 vo_update_text_progbar(obj,dxs,dys);
839 break;
840 case OSDTYPE_SPU:
841 if(sub_visibility && vo_spudec && spudec_visible(vo_spudec)){
842 vo_update_spudec_sub(obj, dxs, dys);
843 obj->flags|=OSDFLAG_VISIBLE|OSDFLAG_CHANGED;
845 else
846 obj->flags&=~OSDFLAG_VISIBLE;
847 break;
848 case OSDTYPE_OSD:
849 if(vo_font && vo_osd_text && vo_osd_text[0]){
850 vo_update_text_osd(obj,dxs,dys); // update bbox
851 obj->flags|=OSDFLAG_VISIBLE|OSDFLAG_CHANGED;
852 } else
853 obj->flags&=~OSDFLAG_VISIBLE;
854 break;
856 // check bbox:
857 if(!(obj->flags&OSDFLAG_BBOX)){
858 // we don't know, so assume the whole screen changed :(
859 obj->bbox.x1=obj->bbox.y1=0;
860 obj->bbox.x2=dxs;
861 obj->bbox.y2=dys;
862 obj->flags|=OSDFLAG_BBOX;
863 } else {
864 // check bbox, reduce it if it's out of bounds (corners):
865 if(obj->bbox.x1<0) obj->bbox.x1=0;
866 if(obj->bbox.y1<0) obj->bbox.y1=0;
867 if(obj->bbox.x2>dxs) obj->bbox.x2=dxs;
868 if(obj->bbox.y2>dys) obj->bbox.y2=dys;
869 if(obj->flags&OSDFLAG_VISIBLE)
870 // debug:
871 mp_msg(MSGT_OSD,MSGL_DBG2,"OSD update: %d;%d %dx%d \n",
872 obj->bbox.x1,obj->bbox.y1,obj->bbox.x2-obj->bbox.x1,
873 obj->bbox.y2-obj->bbox.y1);
875 // check if visibility changed:
876 if(vis != (obj->flags&OSDFLAG_VISIBLE) ) obj->flags|=OSDFLAG_CHANGED;
877 // remove the cause of automatic update:
878 obj->dxs=dxs; obj->dys=dys;
879 obj->flags&=~OSDFLAG_FORCE_UPDATE;
881 if(obj->flags&OSDFLAG_CHANGED){
882 chg|=1<<obj->type;
883 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);
885 obj=obj->next;
887 return chg;
890 void vo_init_osd(void){
891 if(!draw_alpha_init_flag){
892 draw_alpha_init_flag=1;
893 vo_draw_alpha_init();
895 if(vo_osd_list) free_osd_list();
896 // temp hack, should be moved to mplayer/mencoder later
897 new_osd_obj(OSDTYPE_OSD);
898 new_osd_obj(OSDTYPE_SUBTITLE);
899 new_osd_obj(OSDTYPE_PROGBAR);
900 new_osd_obj(OSDTYPE_SPU);
901 #ifdef HAVE_FREETYPE
902 force_load_font = 1;
903 #endif
906 int vo_osd_changed_flag=0;
908 void vo_remove_text(int dxs,int dys,void (*remove)(int x0,int y0, int w,int h)){
909 mp_osd_obj_t* obj=vo_osd_list;
910 vo_update_osd(dxs,dys);
911 while(obj){
912 if(((obj->flags&OSDFLAG_CHANGED) || (obj->flags&OSDFLAG_VISIBLE)) &&
913 (obj->flags&OSDFLAG_OLD_BBOX)){
914 int w=obj->old_bbox.x2-obj->old_bbox.x1;
915 int h=obj->old_bbox.y2-obj->old_bbox.y1;
916 if(w>0 && h>0){
917 vo_osd_changed_flag=obj->flags&OSDFLAG_CHANGED; // temp hack
918 remove(obj->old_bbox.x1,obj->old_bbox.y1,w,h);
920 // obj->flags&=~OSDFLAG_OLD_BBOX;
922 obj=obj->next;
926 void vo_draw_text(int dxs,int dys,void (*draw_alpha)(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride)){
927 mp_osd_obj_t* obj=vo_osd_list;
928 vo_update_osd(dxs,dys);
929 while(obj){
930 if(obj->flags&OSDFLAG_VISIBLE){
931 vo_osd_changed_flag=obj->flags&OSDFLAG_CHANGED; // temp hack
932 switch(obj->type){
933 case OSDTYPE_SPU:
934 vo_draw_spudec_sub(obj, draw_alpha); // FIXME
935 break;
936 case OSDTYPE_OSD:
937 case OSDTYPE_SUBTITLE:
938 case OSDTYPE_PROGBAR:
939 vo_draw_text_from_buffer(obj,draw_alpha);
940 break;
942 obj->old_bbox=obj->bbox;
943 obj->flags|=OSDFLAG_OLD_BBOX;
945 obj->flags&=~OSDFLAG_CHANGED;
946 obj=obj->next;
950 static int vo_osd_changed_status = 0;
952 int vo_osd_changed(int new_value)
954 mp_osd_obj_t* obj=vo_osd_list;
955 int ret = vo_osd_changed_status;
956 vo_osd_changed_status = new_value;
958 while(obj){
959 if(obj->type==new_value) obj->flags|=OSDFLAG_FORCE_UPDATE;
960 obj=obj->next;
963 return ret;
966 // BBBBBBBBBBBB AAAAAAAAAAAAA BBBBBBBBBBB
967 // BBBBBBBBBBBB BBBBBBBBBBBBB
968 // BBBBBBB
970 // return TRUE if we have osd in the specified rectangular area:
971 int vo_osd_check_range_update(int x1,int y1,int x2,int y2){
972 mp_osd_obj_t* obj=vo_osd_list;
973 while(obj){
974 if(obj->flags&OSDFLAG_VISIBLE){
975 if( (obj->bbox.x1<=x2 && obj->bbox.x2>=x1) &&
976 (obj->bbox.y1<=y2 && obj->bbox.y2>=y1) ) return 1;
978 obj=obj->next;
980 return 0;