From: <sn0...@la...> - 2007-12-27 14:40:23
|
# HG changeset patch # User Dubois Nicolas <sn0...@la...> # Date 1198765488 0 # Node ID 60fe1b0a86e0162e28060a0e4db6139bc609c971 # Parent 04ac0cafbc0908c9bb3ace5e0607695425a227ea [Subtitles] SSA-tags patch I watch a lot of movies with subtitles and I need "vobsub subtitles" work in xine, then I decide to write this patch. It may support SSA tags for all text subtiles (ssa, ass, srt, ...) This patch : 1. Remove all SSA tags from stream (they are ugly : {\a6}) 2. Handle some of them (b, i, a, an, pos). The other ones control colours, shadow, animation, ... I can't make them work in an easy way. 3. Correct wrap algorithm which have minors bugs (we can see them only with SSA patch...) Modified files : libsputext/demux_sputext.c just remove unneeded code (which remove some of SSA-tags) libsputext/xine_sputext_decoder.c the main modified file. video_out/video_out_xshm.c video_out/video_out_xv.c xine-engine/video_out.h get video output (position and size). See below. 1. Removing SSA tags is done in ogm_render_line_internal() like for html-like tags. (this was done in the previous version of xine) 2. b(bold) and i(italic) are implemented like html-ones, in ogm_render_line_internal(). The other tags this patch support are : aX : alignment in SSA-code anX : alignment in numpad code pos(X,Y) : position, depend on alignment For those ones, I need in first a full-screen OSD, not a five lines one. Then, I need to remember where the last subtitle was drawn, in order to erase it. At last, I need a translation function to convert subtitle coordinates in screen coordinates. For this last point, I first write a full-screen translation (don't care about blacks borders), but it's not really good: the 'pos' tag is sometime used to point out something in the video. (Moreover, ASS spec say we have to draw subtitle on the video) For doing this, I need the real video output size and position, which are only know by the video output driver! Then I had 4 VO properties (in xine-engine/video_out.h) for video driver could give us those informations. I implement it only in xshm and xv drivers (I can't test other ones). If video driver can't give us those informations, the patch fallback in a full-screen translation. 3. there was 3 problem with the wrap algorithm : 1. It was in double: exactly the same, twice. Look like a merge problem. I remove one and all work fine. 2. It want to cut string in equivalent display length but it cut it in equivalent byte length. In most cases, this is the same, but if we have UTF-8 chars or long SSA-tags (which will not be displayed) the result is strange. 3. If we have a too-long part (in bytes) of the string without spaces (bad subtitle file or long SSA-code), the algorithm don't know what to do. (this case is not handled) I re-write the wrap algorithm to correct those problems. Note that my version is slower than previous one : working with bytes is really faster than computing text-length. Maybe I should had to propose an other patch for this part... diff -r 60fe1b0a86e0162e28060a0e4db6139bc609c971 -r 04ac0cafbc0908c9bb3ace5e0607695425a227ea src/libsputext/demux_sputext.c --- a/src/libsputext/demux_sputext.c Thu Dec 27 14:24:48 2007 +0000 +++ b/src/libsputext/demux_sputext.c Thu Dec 27 14:20:37 2007 +0000 @@ -380,31 +380,6 @@ static subtitle_t *sub_read_line_subrip( temp_line[temp_index++]='\0'; /* end of curent line */ p++; } else - temp_line[temp_index++]=*p; - break; - case '{': -#if 0 /* italic not implemented in renderer, ignore them for now */ - if(!strncmp(p,"{\\i1}",5) && temp_index+3<SUB_BUFSIZE) { - temp_line[temp_index++]='<'; - temp_line[temp_index++]='i'; - temp_line[temp_index++]='>'; -#else - if(!strncmp(p,"{\\i1}",5)) { -#endif - p+=4; - } -#if 0 /* italic not implemented in renderer, ignore them for now */ - else if(!strncmp(p,"{\\i0}",5) && temp_index+4<SUB_BUFSIZE) { - temp_line[temp_index++]='<'; - temp_line[temp_index++]='/'; - temp_line[temp_index++]='i'; - temp_line[temp_index++]='>'; -#else - else if(!strncmp(p,"{\\i0}",5)) { -#endif - p+=4; - } - else temp_line[temp_index++]=*p; break; case '\r': /* just ignore '\r's */ diff -r 60fe1b0a86e0162e28060a0e4db6139bc609c971 -r 04ac0cafbc0908c9bb3ace5e0607695425a227ea src/libsputext/xine_sputext_decoder.c --- a/src/libsputext/xine_sputext_decoder.c Thu Dec 27 14:24:48 2007 +0000 +++ b/src/libsputext/xine_sputext_decoder.c Thu Dec 27 14:20:37 2007 +0000 @@ -41,6 +41,26 @@ #define SUB_MAX_TEXT 5 /* lines */ #define SUB_BUFSIZE 256 /* chars per line */ +/* alignment in SSA codes */ +#define ALIGN_LEFT 1 +#define ALIGN_CENTER 2 +#define ALIGN_RIGHT 3 +#define ALIGN_BOTTOM 0 +#define ALIGN_TOP 4 +#define ALIGN_MIDDLE 8 +#define GET_X_ALIGNMENT(a) ((a) & 3) +#define GET_Y_ALIGNMENT(a) ((a) - ((a) & 3)) + +/* subtitles projection */ +/* for subrip file with SSA tags, those values are always correct.*/ +/* But for SSA files, those values are the default ones. we have */ +/* to use PlayResX and PlayResY defined in [Script Info] section. */ +/* not implemented yet... */ +#define SPU_PROJECTION_X 384 +#define SPU_PROJECTION_Y 288 + + + #define rgb2yuv(R,G,B) ((((((66*R+129*G+25*B+128)>>8)+16)<<8)|(((112*R-94*G-18*B+128)>>8)+128))<<8|(((-38*R-74*G+112*B+128)>>8)+128)) static uint32_t sub_palette[22]={ @@ -106,6 +126,15 @@ typedef struct sputext_class_s { } sputext_class_t; +/* Convert subtiles coordinates in window coordinates. */ +/* (a, b) --> (x + a * dx, y + b * dy) */ +typedef struct video2wnd_s { + int x; + int y; + double dx; + double dy; +} video2wnd_t; + typedef struct sputext_decoder_s { spu_decoder_t spu_decoder; @@ -141,7 +170,9 @@ typedef struct sputext_decoder_s { int64_t last_subtitle_end; /* no new subtitle before this vpts */ int unscaled; /* use unscaled OSD */ + int last_y; /* location of the previous subtitle */ int last_lines; /* number of lines of the previous subtitle */ + video2wnd_t video2wnd; } sputext_decoder_t; static inline char *get_font (sputext_class_t *class) @@ -156,8 +187,6 @@ static void update_font_size (sputext_de static void update_font_size (sputext_decoder_t *this, int force_update) { static int sizes[SUBTITLE_SIZE_NUM] = { 16, 20, 24, 32, 48, 64 }; - int y; - if ((this->subtitle_size != this->class->subtitle_size) || (this->vertical_offset != this->class->vertical_offset) || force_update) { @@ -170,21 +199,17 @@ static void update_font_size (sputext_de this->line_height = this->font_size + 10; - y = this->height - (SUB_MAX_TEXT * this->line_height) - 5; - - if(((y - this->class->vertical_offset) >= 0) && ((y - this->class->vertical_offset) <= this->height)) - y -= this->class->vertical_offset; - + /* Create a full-window OSD */ if( this->osd ) this->renderer->free_object (this->osd); - lprintf("new osd object, width %d, height %d*%d\n", this->width, SUB_MAX_TEXT, this->line_height); this->osd = this->renderer->new_object (this->renderer, this->width, - SUB_MAX_TEXT * this->line_height); + this->height); this->renderer->set_font (this->osd, get_font (this->class), this->font_size); - this->renderer->set_position (this->osd, 0, y); + + this->renderer->set_position (this->osd, 0, 0); } } @@ -208,7 +233,7 @@ static void update_output_size (sputext_ VO_PROP_WINDOW_HEIGHT) || !this->img_duration || !this->osd ) { - int width = 0, height = 0; /* dummy */ + int width = 0, height = 0; this->stream->video_out->status(this->stream->video_out, NULL, &width, &height, &this->img_duration ); @@ -220,8 +245,36 @@ static void update_output_size (sputext_ VO_PROP_WINDOW_HEIGHT); if(!this->osd || (this->width && this->height)) { + + /* in unscaled mode, we have to convert subtitle position in window coordinates. */ + /* we have a scale factor because video may be zommed */ + /* and a displacement factor because video may have blacks lines. */ + int output_width, output_height, output_xoffset, output_yoffset; + + output_width = this->stream->video_out->get_property(this->stream->video_out, + VO_PROP_OUTPUT_WIDTH); + output_height = this->stream->video_out->get_property(this->stream->video_out, + VO_PROP_OUTPUT_HEIGHT); + output_xoffset = this->stream->video_out->get_property(this->stream->video_out, + VO_PROP_OUTPUT_XOFFSET); + output_yoffset = this->stream->video_out->get_property(this->stream->video_out, + VO_PROP_OUTPUT_YOFFSET); + + /* driver don't seen to be capable to give us those values */ + /* fallback to a default full-window values */ + if (output_width <= 0 || output_height <= 0) { + output_width = this->width; + output_height = this->height; + output_xoffset = 0; + output_yoffset = 0; + } + [... 459 lines omitted ...] - } else { /* regenerate all the lines to find something that better fits */ - char buf[SUB_BUFSIZE*SUB_MAX_TEXT]; - int a,w,chunks; - buf[0]='\0'; - for(a=0;a<this->lines;a++) { - if(a) { - int len=strlen(buf); - buf[len]=' '; - buf[len+1]='\0'; - } - strcat(buf,this->text[a]); - } - w = ogm_get_width( this, buf); - chunks=(int)(w/this->width)+(w%this->width?1:0); - xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "Complete subtitle line splitting in %i chunks\n",chunks); - if(chunks<=SUB_MAX_TEXT) {/* if the length is over than SUB_MAX_TEXT*this->width nothing can be done */ - int b,len=strlen(buf); - char *p=buf; - for(b=0;b<chunks;b++) { - char *c; - if(b==chunks-1) { /* if we are reading the last chunk, copy it completly */ - strncpy(this->text[b],p,SUB_BUFSIZE); - this->text[b][SUB_BUFSIZE - 1] = '\0'; - } else { - for(c=p+(int)(len/chunks)+(len%chunks?1:0);*c!=' ' && c>p && c!='\0';c--); - if(*c==' ') { - *c='\0'; - strncpy(this->text[b],p,SUB_BUFSIZE); - this->text[b][SUB_BUFSIZE - 1] = '\0'; - p=c+1; - } - } - } - this->lines=chunks; - } else - xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG, "Subtitle too long to be splited\n"); - line=this->lines; - } - } - } - - if (this->last_lines) - this->renderer->filled_rect (this->osd, 0, this->line_height * (SUB_MAX_TEXT - this->last_lines), - this->width - 1, this->line_height * SUB_MAX_TEXT - 1, 0); - this->last_lines = this->lines; - y = (SUB_MAX_TEXT - this->lines) * this->line_height; + } + + /* line maybe too long, but we have reached last subtitle line */ + else { + best_cut = current_cut = stream + strlen(stream); + } + + /* copy current line */ + if (best_cut != NULL) *best_cut = 0; + strncpy(this->text[this->lines], stream, SUB_BUFSIZE); + this->lines = this->lines + 1; + + stream = best_cut + 1; + + } while (best_cut != current_cut); + + } + + + /* Erase subtitle : use last_y and last_lines saved last turn. */ + if (this->last_lines) { + this->renderer->filled_rect (this->osd, 0, this->last_y, + this->width - 1, this->last_y + this->last_lines * this->line_height, + 0); + } + + switch (GET_Y_ALIGNMENT(alignment)) { + case ALIGN_TOP: + if (sub_y >= 0) y = sub_y; + else y = 5; + break; + + case ALIGN_MIDDLE: + if (sub_y >= 0) y = sub_y - (this->lines * this->line_height) / 2; + else y = (this->height - this->lines * this->line_height) / 2; + break; + + case ALIGN_BOTTOM: + default: + if (sub_y >= 0) y = sub_y - this->lines * this->line_height; + else y = this->height - this->lines * this->line_height - this->class->vertical_offset; + break; + } + if (y < 0 || y >= this->height) + y = this->height - this->line_height * this->lines; + + this->last_lines = this->lines; + this->last_y = y; + for (line = 0; line < this->lines; line++) { int w, x; while(1) { w = ogm_get_width( this, this->text[line]); - x = (this->width - w) / 2; - - if( w > this->width && font_size > 16 ) { + + switch (GET_X_ALIGNMENT(alignment)) { + case ALIGN_LEFT: + if (sub_x >= 0) x = sub_x; + else x = 5; + break; + + case ALIGN_RIGHT: + if (sub_x >= 0) x = sub_x - w; + else x = max_width - w - 5; + break; + + case ALIGN_CENTER: + default: + if (sub_x >= 0) x = sub_x - w / 2; + else x = (max_width - w) / 2; + break; + } + + + if( w > max_width && font_size > 16 ) { font_size -= 4; this->renderer->set_font (this->osd, get_font (this->class), font_size); } else { diff -r 60fe1b0a86e0162e28060a0e4db6139bc609c971 -r 04ac0cafbc0908c9bb3ace5e0607695425a227ea src/video_out/video_out_xshm.c --- a/src/video_out/video_out_xshm.c Thu Dec 27 14:24:48 2007 +0000 +++ b/src/video_out/video_out_xshm.c Thu Dec 27 14:20:37 2007 +0000 @@ -820,6 +820,14 @@ static int xshm_get_property (vo_driver_ return this->sc.gui_width; case VO_PROP_WINDOW_HEIGHT: return this->sc.gui_height; + case VO_PROP_OUTPUT_WIDTH: + return this->cur_frame->sc.output_width; + case VO_PROP_OUTPUT_HEIGHT: + return this->cur_frame->sc.output_height; + case VO_PROP_OUTPUT_XOFFSET: + return this->cur_frame->sc.output_xoffset; + case VO_PROP_OUTPUT_YOFFSET: + return this->cur_frame->sc.output_yoffset; default: xprintf(this->xine, XINE_VERBOSITY_DEBUG, "video_out_xshm: tried to get unsupported property %d\n", property); diff -r 60fe1b0a86e0162e28060a0e4db6139bc609c971 -r 04ac0cafbc0908c9bb3ace5e0607695425a227ea src/video_out/video_out_xv.c --- a/src/video_out/video_out_xv.c Thu Dec 27 14:24:48 2007 +0000 +++ b/src/video_out/video_out_xv.c Thu Dec 27 14:20:37 2007 +0000 @@ -870,6 +870,18 @@ static int xv_get_property (vo_driver_t break; case VO_PROP_WINDOW_HEIGHT: this->props[property].value = this->sc.gui_height; + break; + case VO_PROP_OUTPUT_WIDTH: + this->props[property].value = this->sc.output_width; + break; + case VO_PROP_OUTPUT_HEIGHT: + this->props[property].value = this->sc.output_height; + break; + case VO_PROP_OUTPUT_XOFFSET: + this->props[property].value = this->sc.output_xoffset; + break; + case VO_PROP_OUTPUT_YOFFSET: + this->props[property].value = this->sc.output_yoffset; break; } diff -r 60fe1b0a86e0162e28060a0e4db6139bc609c971 -r 04ac0cafbc0908c9bb3ace5e0607695425a227ea src/xine-engine/video_out.h --- a/src/xine-engine/video_out.h Thu Dec 27 14:24:48 2007 +0000 +++ b/src/xine-engine/video_out.h Thu Dec 27 14:20:37 2007 +0000 @@ -245,7 +245,11 @@ struct xine_video_port_s { #define VO_PROP_WINDOW_HEIGHT 16 /* read-only */ #define VO_PROP_BUFS_IN_FIFO 17 /* read-only */ #define VO_PROP_NUM_STREAMS 18 /* read-only */ -#define VO_NUM_PROPERTIES 19 +#define VO_PROP_OUTPUT_WIDTH 19 /* read-only */ +#define VO_PROP_OUTPUT_HEIGHT 20 /* read-only */ +#define VO_PROP_OUTPUT_XOFFSET 21 /* read-only */ +#define VO_PROP_OUTPUT_YOFFSET 22 /* read-only */ +#define VO_NUM_PROPERTIES 23 /* number of colors in the overlay palette. Currently limited to 256 at most, because some alphablend functions use an 8-bit index into |