10l initial patch by Oded Shimon <ods15 at ods15.dyndns.org>
[mplayer/greg.git] / libvo / sub.c
blobc645483925bbf0beed5698ed461fca5087e42d0c
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 "video_out.h"
13 #include "font_load.h"
14 #include "sub.h"
15 #include "spudec.h"
17 #define NEW_SPLITTING
20 // Structures needed for the new splitting algorithm.
21 // osd_text_t contains the single subtitle word.
22 // osd_text_p is used to mark the lines of subtitles
23 struct osd_text_t {
24 int osd_kerning, //kerning with the previous word
25 osd_length, //orizontal length inside the bbox
26 text_length, //number of characters
27 *text; //characters
28 struct osd_text_t *prev,
29 *next;
32 struct osd_text_p {
33 int value;
34 struct osd_text_t *ott;
35 struct osd_text_p *prev,
36 *next;
38 //^
40 char * __sub_osd_names[]={
41 "Seekbar",
42 "Play",
43 "Pause",
44 "Stop",
45 "Rewind",
46 "Forward",
47 "Clock",
48 "Contrast",
49 "Saturation",
50 "Volume",
51 "Brightness",
52 "Hue"
54 char * __sub_osd_names_short[] ={ "", "|>", "||", "[]", "<<" , ">>", "", "", "", "", "", ""};
56 //static int vo_font_loaded=-1;
57 font_desc_t* vo_font=NULL;
59 unsigned char* vo_osd_text=NULL;
60 int sub_unicode=0;
61 int sub_utf8=0;
62 int sub_pos=100;
63 int sub_width_p=100;
64 int sub_alignment=0; /* 0=top, 1=center, 2=bottom */
65 int sub_visibility=1;
66 int sub_bg_color=0; /* subtitles background color */
67 int sub_bg_alpha=0;
68 int sub_justify=0;
70 // return the real height of a char:
71 static inline int get_height(int c,int h){
72 int font;
73 if ((font=vo_font->font[c])>=0)
74 if(h<vo_font->pic_a[font]->h) h=vo_font->pic_a[font]->h;
75 return h;
78 // renders char to a big per-object buffer where alpha and bitmap are separated
79 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)
81 int dststride = obj->stride;
82 int dstskip = obj->stride-w;
83 int srcskip = stride-w;
84 int i, j;
85 unsigned char *b = obj->bitmap_buffer + (y0-obj->bbox.y1)*dststride + (x0-obj->bbox.x1);
86 unsigned char *a = obj->alpha_buffer + (y0-obj->bbox.y1)*dststride + (x0-obj->bbox.x1);
87 unsigned char *bs = src;
88 unsigned char *as = srca;
90 if (x0 < obj->bbox.x1 || x0+w > obj->bbox.x2 || y0 < obj->bbox.y1 || y0+h > obj->bbox.y2) {
91 fprintf(stderr, "osd text out of range: bbox [%d %d %d %d], txt [%d %d %d %d]\n",
92 obj->bbox.x1, obj->bbox.x2, obj->bbox.y1, obj->bbox.y2,
93 x0, x0+w, y0, y0+h);
94 return;
97 for (i = 0; i < h; i++) {
98 for (j = 0; j < w; j++, b++, a++, bs++, as++) {
99 if (*b < *bs) *b = *bs;
100 if (*as) {
101 if (*a == 0 || *a > *as) *a = *as;
104 b+= dstskip;
105 a+= dstskip;
106 bs+= srcskip;
107 as+= srcskip;
111 // allocates/enlarges the alpha/bitmap buffer
112 static void alloc_buf(mp_osd_obj_t* obj)
114 int len;
115 if (obj->bbox.x2 < obj->bbox.x1) obj->bbox.x2 = obj->bbox.x1;
116 if (obj->bbox.y2 < obj->bbox.y1) obj->bbox.y2 = obj->bbox.y1;
117 obj->stride = ((obj->bbox.x2-obj->bbox.x1)+7)&(~7);
118 len = obj->stride*(obj->bbox.y2-obj->bbox.y1);
119 if (obj->allocated<len) {
120 obj->allocated = len;
121 free(obj->bitmap_buffer);
122 free(obj->alpha_buffer);
123 obj->bitmap_buffer = (unsigned char *)memalign(16, len);
124 obj->alpha_buffer = (unsigned char *)memalign(16, len);
126 memset(obj->bitmap_buffer, sub_bg_color, len);
127 memset(obj->alpha_buffer, sub_bg_alpha, len);
130 // renders the buffer
131 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)){
132 if (obj->allocated > 0) {
133 draw_alpha(obj->bbox.x1,obj->bbox.y1,
134 obj->bbox.x2-obj->bbox.x1,
135 obj->bbox.y2-obj->bbox.y1,
136 obj->bitmap_buffer,
137 obj->alpha_buffer,
138 obj->stride);
142 inline static void vo_update_text_osd(mp_osd_obj_t* obj,int dxs,int dys){
143 unsigned char *cp=vo_osd_text;
144 int x=20;
145 int h=0;
146 int font;
148 obj->bbox.x1=obj->x=x;
149 obj->bbox.y1=obj->y=10;
151 while (*cp){
152 int c=*cp++;
153 render_one_glyph(vo_font, c);
154 x+=vo_font->width[c]+vo_font->charspace;
155 h=get_height(c,h);
158 obj->bbox.x2=x-vo_font->charspace;
159 obj->bbox.y2=obj->bbox.y1+h;
160 obj->flags|=OSDFLAG_BBOX;
162 alloc_buf(obj);
164 cp=vo_osd_text;
165 x = obj->x;
166 while (*cp){
167 int c=*cp++;
168 if ((font=vo_font->font[c])>=0)
169 draw_alpha_buf(obj,x,obj->y,
170 vo_font->width[c],
171 vo_font->pic_a[font]->h,
172 vo_font->pic_b[font]->bmp+vo_font->start[c],
173 vo_font->pic_a[font]->bmp+vo_font->start[c],
174 vo_font->pic_a[font]->w);
175 x+=vo_font->width[c]+vo_font->charspace;
179 int vo_osd_progbar_type=-1;
180 int vo_osd_progbar_value=100; // 0..256
182 // if we have n=256 bars then OSD progbar looks like below
184 // 0 1 2 3 ... 256 <= vo_osd_progbar_value
185 // | | | | |
186 // [ === === === ... === ]
188 // the above schema is rescalled to n=elems bars
190 inline static void vo_update_text_progbar(mp_osd_obj_t* obj,int dxs,int dys){
192 obj->flags|=OSDFLAG_CHANGED|OSDFLAG_VISIBLE;
194 if(vo_osd_progbar_type<0 || !vo_font){
195 obj->flags&=~OSDFLAG_VISIBLE;
196 return;
199 render_one_glyph(vo_font, OSD_PB_START);
200 render_one_glyph(vo_font, OSD_PB_END);
201 render_one_glyph(vo_font, OSD_PB_0);
202 render_one_glyph(vo_font, OSD_PB_1);
203 render_one_glyph(vo_font, vo_osd_progbar_type);
205 // calculate bbox corners:
206 { int h=0;
207 int y=(dys-vo_font->height)/2;
208 int delimw=vo_font->width[OSD_PB_START]
209 +vo_font->width[OSD_PB_END]
210 +vo_font->charspace;
211 int width=(2*dxs-3*delimw)/3;
212 int charw=vo_font->width[OSD_PB_0]+vo_font->charspace;
213 int elems=width/charw;
214 int x=(dxs-elems*charw-delimw)/2;
215 int delta = 0;
216 h=get_height(OSD_PB_START,h);
217 h=get_height(OSD_PB_END,h);
218 h=get_height(OSD_PB_0,h);
219 h=get_height(OSD_PB_1,h);
220 if (vo_osd_progbar_type>0 && vo_font->font[vo_osd_progbar_type]>=0){
221 delta = vo_font->width[vo_osd_progbar_type]+vo_font->spacewidth;
222 delta = (x-delta > 0) ? delta : x;
223 h=get_height(vo_osd_progbar_type,h);
225 obj->bbox.x1=obj->x=x;
226 obj->bbox.y1=obj->y=y;
227 obj->bbox.x2=x+width+delimw;
228 obj->bbox.y2=y+h; //vo_font->height;
229 obj->flags|=OSDFLAG_BBOX;
230 obj->params.progbar.elems=elems;
231 obj->bbox.x1-=delta; // space for an icon
234 alloc_buf(obj);
237 int minw = vo_font->width[OSD_PB_START]+vo_font->width[OSD_PB_END]+vo_font->width[OSD_PB_0];
238 if (vo_osd_progbar_type>0 && vo_font->font[vo_osd_progbar_type]>=0){
239 minw += vo_font->width[vo_osd_progbar_type]+vo_font->charspace+vo_font->spacewidth;
241 if (obj->bbox.x2 - obj->bbox.x1 < minw) return; // space too small, don't render anything
244 // render it:
245 { unsigned char *s;
246 unsigned char *sa;
247 int i,w,h,st,mark;
248 int x=obj->x;
249 int y=obj->y;
250 int c,font;
251 int charw=vo_font->width[OSD_PB_0]+vo_font->charspace;
252 int elems=obj->params.progbar.elems;
254 if (vo_osd_progbar_value<=0)
255 mark=0;
256 else {
257 int ev=vo_osd_progbar_value*elems;
258 mark=ev>>8;
259 if (ev & 0xFF) mark++;
260 if (mark>elems) mark=elems;
264 // printf("osd.progbar width=%d xpos=%d\n",width,x);
266 c=vo_osd_progbar_type;
267 if(vo_osd_progbar_type>0 && (font=vo_font->font[c])>=0) {
268 int xp=x-vo_font->width[c]-vo_font->spacewidth;
269 draw_alpha_buf(obj,(xp<0?0:xp),y,
270 vo_font->width[c],
271 vo_font->pic_a[font]->h,
272 vo_font->pic_b[font]->bmp+vo_font->start[c],
273 vo_font->pic_a[font]->bmp+vo_font->start[c],
274 vo_font->pic_a[font]->w);
277 c=OSD_PB_START;
278 if ((font=vo_font->font[c])>=0)
279 draw_alpha_buf(obj,x,y,
280 vo_font->width[c],
281 vo_font->pic_a[font]->h,
282 vo_font->pic_b[font]->bmp+vo_font->start[c],
283 vo_font->pic_a[font]->bmp+vo_font->start[c],
284 vo_font->pic_a[font]->w);
285 x+=vo_font->width[c]+vo_font->charspace;
287 c=OSD_PB_0;
288 if ((font=vo_font->font[c])>=0){
289 w=vo_font->width[c];
290 h=vo_font->pic_a[font]->h;
291 s=vo_font->pic_b[font]->bmp+vo_font->start[c];
292 sa=vo_font->pic_a[font]->bmp+vo_font->start[c];
293 st=vo_font->pic_a[font]->w;
294 if ((i=mark)) do {
295 draw_alpha_buf(obj,x,y,w,h,s,sa,st);
296 x+=charw;
297 } while(--i);
300 c=OSD_PB_1;
301 if ((font=vo_font->font[c])>=0){
302 w=vo_font->width[c];
303 h=vo_font->pic_a[font]->h;
304 s =vo_font->pic_b[font]->bmp+vo_font->start[c];
305 sa=vo_font->pic_a[font]->bmp+vo_font->start[c];
306 st=vo_font->pic_a[font]->w;
307 if ((i=elems-mark)) do {
308 draw_alpha_buf(obj,x,y,w,h,s,sa,st);
309 x+=charw;
310 } while(--i);
313 c=OSD_PB_END;
314 if ((font=vo_font->font[c])>=0)
315 draw_alpha_buf(obj,x,y,
316 vo_font->width[c],
317 vo_font->pic_a[font]->h,
318 vo_font->pic_b[font]->bmp+vo_font->start[c],
319 vo_font->pic_a[font]->bmp+vo_font->start[c],
320 vo_font->pic_a[font]->w);
321 // x+=vo_font->width[c]+vo_font->charspace;
324 // vo_osd_progbar_value=(vo_osd_progbar_value+1)&0xFF;
328 subtitle* vo_sub=NULL;
330 // 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))
332 inline static void vo_update_text_sub(mp_osd_obj_t* obj,int dxs,int dys){
333 unsigned char *t;
334 int c,i,j,l,x,y,font,prevc,counter;
335 int len;
336 int k;
337 int lastStripPosition;
338 int xsize;
339 int xmin=dxs,xmax=0;
340 int h,lasth;
341 int xtblc, utblc;
343 obj->flags|=OSDFLAG_CHANGED|OSDFLAG_VISIBLE;
345 if(!vo_sub || !vo_font || !sub_visibility || (vo_font->font[40]<0)){
346 obj->flags&=~OSDFLAG_VISIBLE;
347 return;
350 obj->bbox.y2=obj->y=dys;
351 obj->params.subtitle.lines=0;
353 // too long lines divide into a smaller ones
354 i=k=lasth=0;
355 h=vo_font->height;
356 lastStripPosition=-1;
357 l=vo_sub->lines;
360 struct osd_text_t *osl, *cp_ott, *tmp_ott, *tmp;
361 struct osd_text_p *otp_sub = NULL, *otp_sub_tmp, // these are used to store the whole sub text osd
362 *otp, *tmp_otp, *pmt; // these are used to manage sub text osd coming from a single sub line
363 int *char_seq, char_position, xlimit = dxs * sub_width_p / 100, counter;
365 while (l) {
366 xsize = -vo_font->charspace;
367 l--;
368 t=vo_sub->text[i++];
369 len=strlen(t)-1;
370 char_position = 0;
371 char_seq = (int *) malloc((len + 1) * sizeof(int));
373 prevc = -1;
375 otp = NULL;
376 osl = NULL;
377 x = 1;
379 // reading the subtitle words from vo_sub->text[]
380 for (j=0;j<=len;j++){
381 if ((c=t[j])>=0x80){
382 if (sub_utf8){
383 if ((c & 0xe0) == 0xc0) /* 2 bytes U+00080..U+0007FF*/
384 c = (c & 0x1f)<<6 | (t[++j] & 0x3f);
385 else if((c & 0xf0) == 0xe0){ /* 3 bytes U+00800..U+00FFFF*/
386 c = (((c & 0x0f)<<6) | (t[++j] & 0x3f))<<6;
387 c |= (t[++j] & 0x3f);
389 } else if (sub_unicode)
390 c = (c<<8) + t[++j];
392 if (k==MAX_UCS){
393 len=j; // end here
394 mp_msg(MSGT_OSD,MSGL_WARN,"\nMAX_UCS exceeded!\n");
396 if (!c) c++; // avoid UCS 0
397 render_one_glyph(vo_font, c);
399 if (c == ' ') {
400 struct osd_text_t *tmp_ott = (struct osd_text_t *) calloc(1, sizeof(struct osd_text_t));
402 if (osl == NULL) {
403 osl = cp_ott = tmp_ott;
404 } else {
405 tmp_ott->prev = cp_ott;
406 cp_ott->next = tmp_ott;
407 tmp_ott->osd_kerning =
408 vo_font->charspace + vo_font->width[' '];
409 cp_ott = tmp_ott;
411 tmp_ott->osd_length = xsize;
412 tmp_ott->text_length = char_position;
413 tmp_ott->text = (int *) malloc(char_position * sizeof(int));
414 for (counter = 0; counter < char_position; ++counter)
415 tmp_ott->text[counter] = char_seq[counter];
416 char_position = 0;
417 xsize = 0;
418 prevc = c;
419 } else {
420 int delta_xsize = vo_font->width[c] + vo_font->charspace + kerning(vo_font, prevc, c);
422 if (xsize + delta_xsize <= dxs) {
423 if (!x) x = 1;
424 prevc = c;
425 char_seq[char_position++] = c;
426 xsize += delta_xsize;
427 if ((!suboverlap_enabled) && ((font = vo_font->font[c]) >= 0)) {
428 if (vo_font->pic_a[font]->h > h) {
429 h = vo_font->pic_a[font]->h;
432 } else {
433 if (x) {
434 mp_msg(MSGT_OSD, MSGL_WARN, "\nSubtitle word '%s' too long!\n", t);
435 x = 0;
439 }// for len (all words from subtitle line read)
441 // osl holds an ordered (as they appear in the lines) chain of the subtitle words
443 struct osd_text_t *tmp_ott = (struct osd_text_t *) calloc(1, sizeof(struct osd_text_t));
445 if (osl == NULL) {
446 osl = cp_ott = tmp_ott;
447 } else {
448 tmp_ott->prev = cp_ott;
449 cp_ott->next = tmp_ott;
450 tmp_ott->osd_kerning =
451 vo_font->charspace + vo_font->width[' '];
452 cp_ott = tmp_ott;
454 tmp_ott->osd_length = xsize;
455 tmp_ott->text_length = char_position;
456 tmp_ott->text = (int *) malloc(char_position * sizeof(int));
457 for (counter = 0; counter < char_position; ++counter)
458 tmp_ott->text[counter] = char_seq[counter];
459 char_position = 0;
460 xsize = -vo_font->charspace;
463 if (osl != NULL) {
464 int value = 0, exit = 0, minimum = 0;
466 // otp will contain the chain of the osd subtitle lines coming from the single vo_sub line.
467 otp = tmp_otp = (struct osd_text_p *) calloc(1, sizeof(struct osd_text_p));
468 tmp_otp->ott = osl;
469 for (tmp_ott = tmp_otp->ott; exit == 0; ) {
470 do {
471 value += tmp_ott->osd_kerning + tmp_ott->osd_length;
472 tmp_ott = tmp_ott->next;
473 } while ((tmp_ott != NULL) && (value + tmp_ott->osd_kerning + tmp_ott->osd_length <= xlimit));
474 if (tmp_ott != NULL) {
475 struct osd_text_p *tmp = (struct osd_text_p *) calloc(1, sizeof(struct osd_text_p));
477 tmp_otp->value = value;
478 tmp_otp->next = tmp;
479 tmp->prev = tmp_otp;
480 tmp_otp = tmp;
481 tmp_otp->ott = tmp_ott;
482 value = -2 * vo_font->charspace - vo_font->width[' '];
483 } else {
484 tmp_otp->value = value;
485 exit = 1;
490 #ifdef NEW_SPLITTING
491 // minimum holds the 'sum of the differences in lenght among the lines',
492 // a measure of the eveness of the lenghts of the lines
493 for (tmp_otp = otp; tmp_otp->next != NULL; tmp_otp = tmp_otp->next) {
494 pmt = tmp_otp->next;
495 while (pmt != NULL) {
496 minimum += abs(tmp_otp->value - pmt->value);
497 pmt = pmt->next;
501 if (otp->next != NULL) {
502 int mem1, mem2;
503 struct osd_text_p *mem, *hold;
505 exit = 0;
506 // until the last word of a line can be moved to the beginning of following line
507 // reducing the 'sum of the differences in lenght among the lines', it is done
508 while (exit == 0) {
509 hold = NULL;
510 exit = 1;
511 for (tmp_otp = otp; tmp_otp->next != NULL; tmp_otp = tmp_otp->next) {
512 pmt = tmp_otp->next;
513 for (tmp = tmp_otp->ott; tmp->next != pmt->ott; tmp = tmp->next);
514 if (pmt->value + tmp->osd_length + pmt->ott->osd_kerning <= xlimit) {
515 mem1 = tmp_otp->value;
516 mem2 = pmt->value;
517 tmp_otp->value = mem1 - tmp->osd_length - tmp->osd_kerning;
518 pmt->value = mem2 + tmp->osd_length + pmt->ott->osd_kerning;
520 value = 0;
521 for (mem = otp; mem->next != NULL; mem = mem->next) {
522 pmt = mem->next;
523 while (pmt != NULL) {
524 value += abs(mem->value - pmt->value);
525 pmt = pmt->next;
528 if (value < minimum) {
529 minimum = value;
530 hold = tmp_otp;
531 exit = 0;
533 tmp_otp->value = mem1;
534 tmp_otp->next->value = mem2;
537 // merging
538 if (exit == 0) {
539 tmp_otp = hold;
540 pmt = tmp_otp->next;
541 for (tmp = tmp_otp->ott; tmp->next != pmt->ott; tmp = tmp->next);
542 mem1 = tmp_otp->value;
543 mem2 = pmt->value;
544 tmp_otp->value = mem1 - tmp->osd_length - tmp->osd_kerning;
545 pmt->value = mem2 + tmp->osd_length + pmt->ott->osd_kerning;
546 pmt->ott = tmp;
547 }//~merging
548 }//~while(exit == 0)
549 }//~if(otp->next!=NULL)
550 #endif
552 // adding otp (containing splitted lines) to otp chain
553 if (otp_sub == NULL) {
554 otp_sub = otp;
555 for (otp_sub_tmp = otp_sub; otp_sub_tmp->next != NULL; otp_sub_tmp = otp_sub_tmp->next);
556 } else {
557 //updating ott chain
558 tmp = otp_sub->ott;
559 while (tmp->next != NULL) tmp = tmp->next;
560 tmp->next = otp->ott;
561 otp->ott->prev = tmp;
562 //attaching new subtitle line at the end
563 otp_sub_tmp->next = otp;
564 otp->prev = otp_sub_tmp;
566 otp_sub_tmp = otp_sub_tmp->next;
567 while (otp_sub_tmp->next != NULL);
569 }//~ if(osl != NULL)
570 } // while
572 // write lines into utbl
573 xtblc = 0;
574 utblc = 0;
575 obj->y = dys;
576 obj->params.subtitle.lines = 0;
577 for (tmp_otp = otp_sub; tmp_otp != NULL; tmp_otp = tmp_otp->next) {
579 if ((obj->params.subtitle.lines++) >= MAX_UCSLINES)
580 break;
582 if (h > obj->y) { // out of the screen so end parsing
583 obj->y -= lasth - vo_font->height; // correct the y position
584 break;
586 xsize = tmp_otp->value;
587 obj->params.subtitle.xtbl[xtblc++] = (dxs - xsize) / 2;
588 if (xmin > (dxs - xsize) / 2)
589 xmin = (dxs - xsize) / 2;
590 if (xmax < (dxs + xsize) / 2)
591 xmax = (dxs + xsize) / 2;
593 tmp = (tmp_otp->next == NULL) ? NULL : tmp_otp->next->ott;
594 for (tmp_ott = tmp_otp->ott; tmp_ott != tmp; tmp_ott = tmp_ott->next) {
595 for (counter = 0; counter < tmp_ott->text_length; ++counter) {
596 if (utblc > MAX_UCS) {
597 break;
599 c = tmp_ott->text[counter];
600 render_one_glyph(vo_font, c);
601 obj->params.subtitle.utbl[utblc++] = c;
602 k++;
604 obj->params.subtitle.utbl[utblc++] = ' ';
606 obj->params.subtitle.utbl[utblc - 1] = 0;
607 obj->y -= vo_font->height;
609 if(obj->params.subtitle.lines)
610 obj->y = dys - ((obj->params.subtitle.lines - 1) * vo_font->height + vo_font->pic_a[vo_font->font[40]]->h);
612 // free memory
613 if (otp_sub != NULL) {
614 for (tmp = otp_sub->ott; tmp->next != NULL; free(tmp->prev)) {
615 free(tmp->text);
616 tmp = tmp->next;
618 free(tmp->text);
619 free(tmp);
621 for(pmt = otp_sub; pmt->next != NULL; free(pmt->prev)) {
622 pmt = pmt->next;
624 free(pmt);
628 /// vertical alignment
629 h = dys - obj->y;
630 if (sub_alignment == 2)
631 obj->y = dys * sub_pos / 100 - h;
632 else if (sub_alignment == 1)
633 obj->y = dys * sub_pos / 100 - h / 2;
634 else
635 obj->y = dys * sub_pos / 100;
637 if (obj->y < 0)
638 obj->y = 0;
639 if (obj->y > dys - h)
640 obj->y = dys - h;
642 obj->bbox.y2 = obj->y + h;
644 // calculate bbox:
645 if (sub_justify) xmin = 10;
646 obj->bbox.x1=xmin;
647 obj->bbox.x2=xmax;
648 obj->bbox.y1=obj->y;
649 // obj->bbox.y2=obj->y+obj->params.subtitle.lines*vo_font->height;
650 obj->flags|=OSDFLAG_BBOX;
652 alloc_buf(obj);
654 y = obj->y;
656 obj->alignment = 0;
657 switch(vo_sub->alignment) {
658 case SUB_ALIGNMENT_BOTTOMLEFT:
659 case SUB_ALIGNMENT_MIDDLELEFT:
660 case SUB_ALIGNMENT_TOPLEFT:
661 obj->alignment |= 0x1;
662 break;
663 case SUB_ALIGNMENT_BOTTOMRIGHT:
664 case SUB_ALIGNMENT_MIDDLERIGHT:
665 case SUB_ALIGNMENT_TOPRIGHT:
666 obj->alignment |= 0x2;
667 break;
668 case SUB_ALIGNMENT_BOTTOMCENTER:
669 case SUB_ALIGNMENT_MIDDLECENTER:
670 case SUB_ALIGNMENT_TOPCENTER:
671 default:
672 obj->alignment |= 0x0;
675 i=j=0;
676 if ((l = obj->params.subtitle.lines)) {
677 for(counter = dxs; i < l; ++i)
678 if (obj->params.subtitle.xtbl[i] < counter) counter = obj->params.subtitle.xtbl[i];
679 for (i = 0; i < l; ++i) {
680 switch (obj->alignment&0x3) {
681 case 1:
682 // left
683 x = counter;
684 break;
685 case 2:
686 // right
687 x = 2 * obj->params.subtitle.xtbl[i] - counter - ((obj->params.subtitle.xtbl[i] == counter) ? 0 : 1);
688 break;
689 default:
690 //center
691 x = obj->params.subtitle.xtbl[i];
693 prevc = -1;
694 while ((c=obj->params.subtitle.utbl[j++])){
695 x += kerning(vo_font,prevc,c);
696 if ((font=vo_font->font[c])>=0)
697 draw_alpha_buf(obj,x,y,
698 vo_font->width[c],
699 vo_font->pic_a[font]->h+y<obj->dys ? vo_font->pic_a[font]->h : obj->dys-y,
700 vo_font->pic_b[font]->bmp+vo_font->start[c],
701 vo_font->pic_a[font]->bmp+vo_font->start[c],
702 vo_font->pic_a[font]->w);
703 x+=vo_font->width[c]+vo_font->charspace;
704 prevc = c;
706 y+=vo_font->height;
712 inline static void vo_update_spudec_sub(mp_osd_obj_t* obj, int dxs, int dys)
714 unsigned int bbox[4];
715 spudec_calc_bbox(vo_spudec, dxs, dys, bbox);
716 obj->bbox.x1 = bbox[0];
717 obj->bbox.x2 = bbox[1];
718 obj->bbox.y1 = bbox[2];
719 obj->bbox.y2 = bbox[3];
720 obj->flags |= OSDFLAG_BBOX;
723 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))
725 spudec_draw_scaled(vo_spudec, obj->dxs, obj->dys, draw_alpha);
728 void *vo_spudec=NULL;
729 void *vo_vobsub=NULL;
731 static int draw_alpha_init_flag=0;
733 extern void vo_draw_alpha_init();
735 mp_osd_obj_t* vo_osd_list=NULL;
737 mp_osd_obj_t* new_osd_obj(int type){
738 mp_osd_obj_t* osd=malloc(sizeof(mp_osd_obj_t));
739 memset(osd,0,sizeof(mp_osd_obj_t));
740 osd->next=vo_osd_list;
741 vo_osd_list=osd;
742 osd->type=type;
743 osd->alpha_buffer = NULL;
744 osd->bitmap_buffer = NULL;
745 osd->allocated = -1;
746 return osd;
749 void free_osd_list(){
750 mp_osd_obj_t* obj=vo_osd_list;
751 while(obj){
752 mp_osd_obj_t* next=obj->next;
753 if (obj->alpha_buffer) free(obj->alpha_buffer);
754 if (obj->bitmap_buffer) free(obj->bitmap_buffer);
755 free(obj);
756 obj=next;
758 vo_osd_list=NULL;
761 int vo_update_osd(int dxs,int dys){
762 mp_osd_obj_t* obj=vo_osd_list;
763 int chg=0;
765 #ifdef HAVE_FREETYPE
766 // here is the right place to get screen dimensions
767 if (!vo_font || force_load_font) {
768 force_load_font = 0;
769 load_font_ft(dxs, dys);
771 #endif
773 while(obj){
774 if(dxs!=obj->dxs || dys!=obj->dys || obj->flags&OSDFLAG_FORCE_UPDATE){
775 int vis=obj->flags&OSDFLAG_VISIBLE;
776 obj->flags&=~OSDFLAG_BBOX;
777 switch(obj->type){
778 case OSDTYPE_SUBTITLE:
779 vo_update_text_sub(obj,dxs,dys);
780 break;
781 case OSDTYPE_PROGBAR:
782 vo_update_text_progbar(obj,dxs,dys);
783 break;
784 case OSDTYPE_SPU:
785 if(sub_visibility && vo_spudec && spudec_visible(vo_spudec)){
786 vo_update_spudec_sub(obj, dxs, dys);
787 obj->flags|=OSDFLAG_VISIBLE|OSDFLAG_CHANGED;
789 else
790 obj->flags&=~OSDFLAG_VISIBLE;
791 break;
792 case OSDTYPE_OSD:
793 if(vo_font && vo_osd_text && vo_osd_text[0]){
794 vo_update_text_osd(obj,dxs,dys); // update bbox
795 obj->flags|=OSDFLAG_VISIBLE|OSDFLAG_CHANGED;
796 } else
797 obj->flags&=~OSDFLAG_VISIBLE;
798 break;
800 // check bbox:
801 if(!(obj->flags&OSDFLAG_BBOX)){
802 // we don't know, so assume the whole screen changed :(
803 obj->bbox.x1=obj->bbox.y1=0;
804 obj->bbox.x2=dxs;
805 obj->bbox.y2=dys;
806 obj->flags|=OSDFLAG_BBOX;
807 } else {
808 // check bbox, reduce it if it's out of bounds (corners):
809 if(obj->bbox.x1<0) obj->bbox.x1=0;
810 if(obj->bbox.y1<0) obj->bbox.y1=0;
811 if(obj->bbox.x2>dxs) obj->bbox.x2=dxs;
812 if(obj->bbox.y2>dys) obj->bbox.y2=dys;
813 if(obj->flags&OSDFLAG_VISIBLE)
814 // debug:
815 mp_msg(MSGT_OSD,MSGL_DBG2,"OSD update: %d;%d %dx%d \n",
816 obj->bbox.x1,obj->bbox.y1,obj->bbox.x2-obj->bbox.x1,
817 obj->bbox.y2-obj->bbox.y1);
819 // check if visibility changed:
820 if(vis != (obj->flags&OSDFLAG_VISIBLE) ) obj->flags|=OSDFLAG_CHANGED;
821 // remove the cause of automatic update:
822 obj->dxs=dxs; obj->dys=dys;
823 obj->flags&=~OSDFLAG_FORCE_UPDATE;
825 if(obj->flags&OSDFLAG_CHANGED){
826 chg|=1<<obj->type;
827 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);
829 obj=obj->next;
831 return chg;
834 void vo_init_osd(){
835 if(!draw_alpha_init_flag){
836 draw_alpha_init_flag=1;
837 vo_draw_alpha_init();
839 if(vo_osd_list) free_osd_list();
840 // temp hack, should be moved to mplayer/mencoder later
841 new_osd_obj(OSDTYPE_OSD);
842 new_osd_obj(OSDTYPE_SUBTITLE);
843 new_osd_obj(OSDTYPE_PROGBAR);
844 new_osd_obj(OSDTYPE_SPU);
845 #ifdef HAVE_FREETYPE
846 force_load_font = 1;
847 #endif
850 int vo_osd_changed_flag=0;
852 void vo_remove_text(int dxs,int dys,void (*remove)(int x0,int y0, int w,int h)){
853 mp_osd_obj_t* obj=vo_osd_list;
854 vo_update_osd(dxs,dys);
855 while(obj){
856 if(((obj->flags&OSDFLAG_CHANGED) || (obj->flags&OSDFLAG_VISIBLE)) &&
857 (obj->flags&OSDFLAG_OLD_BBOX)){
858 int w=obj->old_bbox.x2-obj->old_bbox.x1;
859 int h=obj->old_bbox.y2-obj->old_bbox.y1;
860 if(w>0 && h>0){
861 vo_osd_changed_flag=obj->flags&OSDFLAG_CHANGED; // temp hack
862 remove(obj->old_bbox.x1,obj->old_bbox.y1,w,h);
864 // obj->flags&=~OSDFLAG_OLD_BBOX;
866 obj=obj->next;
870 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)){
871 mp_osd_obj_t* obj=vo_osd_list;
872 vo_update_osd(dxs,dys);
873 while(obj){
874 if(obj->flags&OSDFLAG_VISIBLE){
875 vo_osd_changed_flag=obj->flags&OSDFLAG_CHANGED; // temp hack
876 switch(obj->type){
877 case OSDTYPE_SPU:
878 vo_draw_spudec_sub(obj, draw_alpha); // FIXME
879 break;
880 case OSDTYPE_OSD:
881 case OSDTYPE_SUBTITLE:
882 case OSDTYPE_PROGBAR:
883 vo_draw_text_from_buffer(obj,draw_alpha);
884 break;
886 obj->old_bbox=obj->bbox;
887 obj->flags|=OSDFLAG_OLD_BBOX;
889 obj->flags&=~OSDFLAG_CHANGED;
890 obj=obj->next;
894 static int vo_osd_changed_status = 0;
896 int vo_osd_changed(int new_value)
898 mp_osd_obj_t* obj=vo_osd_list;
899 int ret = vo_osd_changed_status;
900 vo_osd_changed_status = new_value;
902 while(obj){
903 if(obj->type==new_value) obj->flags|=OSDFLAG_FORCE_UPDATE;
904 obj=obj->next;
907 return ret;
910 // BBBBBBBBBBBB AAAAAAAAAAAAA BBBBBBBBBBB
911 // BBBBBBBBBBBB BBBBBBBBBBBBB
912 // BBBBBBB
914 // return TRUE if we have osd in the specified rectangular area:
915 int vo_osd_check_range_update(int x1,int y1,int x2,int y2){
916 mp_osd_obj_t* obj=vo_osd_list;
917 while(obj){
918 if(obj->flags&OSDFLAG_VISIBLE){
919 if( (obj->bbox.x1<=x2 && obj->bbox.x2>=x1) &&
920 (obj->bbox.y1<=y2 && obj->bbox.y2>=y1) ) return 1;
922 obj=obj->next;
924 return 0;