[MPlayer-dev-eng] [PATCH]use custom colors in vobsub's .idx file

陆 然 hephooey at hotmail.com
Thu Apr 11 16:42:37 CEST 2002


Hi,
I write a patch to use custom colors in .idx file to display subtitles. it 
get custom colors from .idx, then if custom is ON, use custom color instead 
of palette, if custom is OFF but no palette item found, it will also use 
custom color. I add a function spudec_new_scale_vobsub in spudec.c since I 
don't know if other subtitles will use spudec_new_scale.
And I think the subtitle is a little bit slow than normal, but the time 
seems right.
 

diff -Nur -x CVS -x '.*' main/spudec.c main_dev/spudec.c
--- main/spudec.c	Thu Apr 11 18:45:09 2002
+++ main_dev/spudec.c	Thu Apr 11 22:02:27 2002
@@ -43,6 +43,8 @@
   unsigned int control_start;	/* index of start of control data */
   unsigned int palette[4];
   unsigned int alpha[4];
+  unsigned int cuspal[4];
+  unsigned int custom;
   unsigned int now_pts;
   unsigned int start_pts, end_pts;
   unsigned int start_col, end_col;
@@ -122,6 +124,11 @@
     alpha[i] = mkalpha(this->alpha[i]);
     if (alpha[i] == 0)
       cmap[i] = 0;
+    else if (this->custom){
+      cmap[i] = ((this->cuspal[i] >> 16) & 0xff);
+      if (cmap[i] + alpha[i] > 255)
+	cmap[i] = 256 - alpha[i];
+    }
     else {
       cmap[i] = ((this->global_palette[this->palette[i]] >> 16) & 0xff);
       if (cmap[i] + alpha[i] > 255)
@@ -685,6 +692,27 @@
   return this;
 }

+/* get palette custom color, width, height from .idx file */
+void *spudec_new_scaled_vobsub(unsigned int *palette, unsigned int 
*cuspal, unsigned int custom, unsigned int frame_width, unsigned int 
frame_height)
+{
+  spudec_handle_t *this = calloc(1, sizeof(spudec_handle_t));
+  if (this){
+    if (palette){
+      memcpy(this->global_palette, palette, sizeof(this->global_palette));
+      memcpy(this->cuspal, cuspal, sizeof(this->cuspal));
+    }
+    fprintf(stderr,"\n%d,%d,%d,%d", this->cuspal[0], this->cuspal[1], 
this->cuspal[2],this->cuspal[3]);
+    this->packet = NULL;
+    this->image = NULL;
+    this->scaled_image = NULL;
+    this->orig_frame_width = frame_width;
+    this->orig_frame_height = frame_height;
+    this->custom = custom;
+  }
+  else
+    perror("FATAL: spudec_init: calloc");
+  return this;
+}
 void *spudec_new(unsigned int *palette)
 {
     return spudec_new_scaled(palette, 0, 0);
diff -Nur -x CVS -x '.*' main/vobsub.c main_dev/vobsub.c
--- main/vobsub.c	Thu Apr 11 18:45:15 2002
+++ main_dev/vobsub.c	Thu Apr 11 22:04:39 2002
@@ -409,6 +409,9 @@
 typedef struct {
     void *spudec;
     unsigned int palette[16];
+    unsigned int cuspal[4];
+    unsigned int custom;
+    unsigned int have_palette;
     unsigned int orig_frame_width, orig_frame_height;
     /* index */
     packet_queue_t *spu_streams;
@@ -609,10 +612,61 @@
 	    ++p;
 	line = p;
     }
+    vob->have_palette = 1;
     return 0;
 }

 static int
+vobsub_parse_custom(vobsub_t *vob, const char *line)
+{
+    //custom colors: OFF/ON(0/1)
+    if ((strncmp("ON", line + 15, 2) == 0)||strncmp("1", line + 15, 1))
+        vob->custom=1;
+    else if ((strncmp("OFF", line + 15, 3) == 0)||strncmp("0", line + 15, 
1))
+        vob->custom=0;
+    else
+        return -1;
+    return 0;
+}
+
+static int
+vobsub_parse_cuspal(vobsub_t *vob, const char *line)
+{
+    //colors: XXXXXX, XXXXXX, XXXXXX, XXXXXX
+    unsigned int n;
+    n = 0;
+    line += 40;
+    while(1){
+    	const char *p;
+	while (isspace(*line))
+	    ++line;
+	p=line;
+	while (isxdigit(*p))
+	    ++p;
+	if (p - line !=6)
+	    return -1;
+	vob->cuspal[n++] = strtoul(line, NULL,16);
+	if (n==4)
+	    break;
+	if(*p == ',')
+	    ++p;
+	line = p;
+    }
+    return 0;
+}
+
+/* don't know how to use tridx */
+static int
+vobsub_parse_tridx(vobsub_t *vob, const char *line)
+{
+    //tridx: XXXX
+    int i;
+    int tridx;
+    tridx = strtoul((line + 26), NULL, 16);
+    tridx = ((tridx&0x1000)>>12) | ((tridx&0x100)>>7) | ((tridx&0x10)>>2) 
| ((tridx&1)<<3);
+}
+
+static int
 vobsub_parse_one_line(vobsub_t *vob, FILE *fd)
 {
     ssize_t line_size;
@@ -636,6 +690,9 @@
 	    res = vobsub_parse_size(vob, line + 5);
 	else if (strncmp("timestamp:", line, 10) == 0)
 	    res = vobsub_parse_timestamp(vob, line + 10);
+	else if (strncmp("custom colors:", line, 14) == 0)
+	    //custom colors: ON/OFF, tridx: XXXX, colors: XXXXXX, XXXXXX, 
XXXXXX,XXXXXX
+	    res = vobsub_parse_cuspal(vob, line) + vobsub_parse_tridx(vob, line) 
+ vobsub_parse_custom(vob, line);
 	else {
 	    if (verbose)
 		fprintf(stderr, "vobsub: ignoring %s", line);
@@ -741,8 +798,11 @@
 		    /* NOOP */ ;
 		fclose(fd);
 	    }
+	    /* if no palette in .idx then use custom colors */
+	    if ((vob->custom == 0)&&(vob->have_palette!=1))
+		vob->custom = 1;
 	    if (vob->orig_frame_width && vob->orig_frame_height)
-		vob->spudec = spudec_new_scaled(vob->palette, vob->orig_frame_width, 
vob->orig_frame_height);
+		vob->spudec = spudec_new_scaled_vobsub(vob->palette, vob->cuspal, 
vob->custom, vob->orig_frame_width, vob->orig_frame_height);

 	    /* read the indexed mpeg_stream */
 	    strcpy(buf, name);




-----------------
Best Regards,
LR

_________________________________________________________________
享用世界上最大的 Web 电子邮件系统 ―― MSN Hotmail。
http://www.hotmail.com/cn




More information about the MPlayer-dev-eng mailing list