diff vobsub.c @ 25292:a6a49a7a4be0

Move vobsub palette->yuv convert code into a common function.
author ulion
date Sat, 08 Dec 2007 12:15:03 +0000
parents 4bbc9ad3124c
children 46c9a864dc26
line wrap: on
line diff
--- a/vobsub.c	Sat Dec 08 04:29:11 2007 +0000
+++ b/vobsub.c	Sat Dec 08 12:15:03 2007 +0000
@@ -13,6 +13,7 @@
 #include <unistd.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <math.h>
 
 #include "config.h"
 #include "version.h"
@@ -802,6 +803,21 @@
     return 0;
 }
 
+unsigned int vobsub_palette_to_yuv(unsigned int pal)
+{
+    int r, g, b, y, u, v;
+    // Palette in idx file is not rgb value, it was calculated by wrong forumla.
+    // Here's reversed forumla of the one used to generate palette in idx file.
+    r = pal >> 16 & 0xff;
+    g = pal >> 8 & 0xff;
+    b = pal & 0xff;
+    y = av_clip_uint8( 0.1494  * r + 0.6061 * g + 0.2445 * b);
+    u = av_clip_uint8( 0.6066  * r - 0.4322 * g - 0.1744 * b + 128);
+    v = av_clip_uint8(-0.08435 * r - 0.3422 * g + 0.4266 * b + 128);
+    y = y * 219 / 255 + 16;
+    return y << 16 | u << 8 | v;
+}
+
 static int
 vobsub_parse_palette(vobsub_t *vob, const char *line)
 {
@@ -810,7 +826,7 @@
     n = 0;
     while (1) {
 	const char *p;
-	int r, g, b, y, u, v, tmp;
+	int tmp;
 	while (isspace(*line))
 	    ++line;
 	p = line;
@@ -819,14 +835,7 @@
 	if (p - line != 6)
 	    return -1;
 	tmp = strtoul(line, NULL, 16);
-	r = tmp >> 16 & 0xff;
-	g = tmp >> 8 & 0xff;
-	b = tmp & 0xff;
-	y = av_clip_uint8( 0.1494  * r + 0.6061 * g + 0.2445 * b);
-	u = av_clip_uint8( 0.6066  * r - 0.4322 * g - 0.1744 * b + 128);
-	v = av_clip_uint8(-0.08435 * r - 0.3422 * g + 0.4266 * b + 128);
-	y = y * 219 / 255 + 16;
-	vob->palette[n++] = y << 16 | u << 8 | v;
+	vob->palette[n++] = vobsub_palette_to_yuv(tmp);
 	if (n == 16)
 	    break;
 	if (*p == ',')