52typedef int8_t
ei8;
typedef uint8_t
eu8;
56typedef int_least8_t
li8;
typedef uint_least8_t
lu8;
57typedef int_least16_t
li16;
typedef uint_least16_t
lu16;
58typedef int_least32_t
li32;
typedef uint_least32_t
lu32;
60typedef int_fast8_t
fi8;
typedef uint_fast8_t
fu8;
61typedef int_fast16_t
fi16;
typedef uint_fast16_t
fu16;
62typedef int_fast32_t
fi32;
typedef uint_fast32_t
fu32;
66#define GCCATTR_PRINTF(m, n) __attribute__ ((format (printf, m, n)))
67#define GCCATTR_UNUSED __attribute ((unused))
68#define GCCATTR_NORETURN __attribute ((noreturn))
69#define GCCATTR_CONST __attribute ((const))
72#define CUS (const unsigned char *)
76int sup2dast(
const char *supfile,
const char *ifofile,
int delay_ms);
97#define exc_try(x) do { struct exc__state exc_s##x; int exc_type##x GCCATTR_UNUSED; \
98 exc_s##x.m_prev = EXC.m_last; \
99 EXC.m_last = &exc_s##x; \
100 exc_type##x = setjmp(exc_s##x.m_env); \
101 if (exc_type##x == 0)
103#define exc_ftry(x) do { struct exc__state exc_s##x, *exc_p##x = EXC.m_last; \
104 int exc_type##x GCCATTR_UNUSED; \
105 exc_s##x.prev = EXC.m_last; \
106 EXC.m_last = &exc_s##x; \
107 exc_type##x = setjmp(exc_s##x.env); \
108 if (exc_type##x == 0)
110#define exc_catch(x,t) else if ((t) == exc_type##x)
112#define exc_end(x) else __exc_throw(exc_type##x); EXC.m_last = exc_s##x.m_prev; } while (0)
114#define exc_return(x) for (EXC.m_last = exc_p##x;) return
116#define exc_fthrow(x) for (EXC.m_last = exc_p##x;) ex_throw
118#define exc_catchall else
119#define exc_endall(x) EXC.m_last = exc_s##x.m_prev; } while (0)
121#define exc_cleanup() EXC.m_last = NULL;
126 EXC.m_last =
EXC.m_last->m_prev;
137 va_start(ap, format);
138 unsigned int len = vsnprintf(
EXC.m_msgbuf,
sizeof EXC.m_msgbuf, format, ap);
141 if (len >=
sizeof EXC.m_msgbuf)
143 len =
sizeof EXC.m_msgbuf - 1;
144 EXC.m_msgbuf[len] =
'\0';
148 if (format[strlen(format) - 1] ==
':')
150 int l = snprintf(&
EXC.m_msgbuf[len],
sizeof EXC.m_msgbuf - len,
151 " %s.", strerror(err));
152 if (l + len >=
sizeof EXC.m_msgbuf)
154 len =
sizeof EXC.m_msgbuf - 1;
155 EXC.m_msgbuf[len] =
'\0';
167 EXC.m_msgbuf[0] =
'\0';
179 size_t n = fread(ptr, size, 1, stream);
190 size_t n = fwrite(ptr, size, 1, stream);
199#define xxfwriteCS(f, s) xxfwrite(f, CUS s, sizeof (s) - 1)
214 int lr = (500 + 1164 * (y - 16) + 1596 * (cr - 128) ) /1000;
215 int lg = (500 + 1164 * (y - 16) - 813 * (cr - 128) - 391 * (cb - 128)) / 1000;
216 int lb = (500 + 1164 * (y - 16) + 2018 * (cb - 128)) / 1000;
218 *r =
clamp(lr, 0, 255);
219 *g =
clamp(lg, 0, 255);
220 *b =
clamp(lb, 0, 255);
229 *y = ( 257 * r + 504 * g + 98 * b + 16500) / 1000;
230 *cr = ( 439 * r - 368 * g - 71 * b + 128500) / 1000;
231 *cb = (-148 * r - 291 * g + 439 * b + 128500) / 1000;
261 if (fseek(stream, offset, SEEK_SET) < 0)
265static void xmkdir(
const char * path,
int mode)
272 if (mkdir(path, mode) < 0)
282 if (stat(
filename, &st) == 0 && S_ISREG(st.st_mode))
291 if (stat(
filename, &st) == 0 && S_ISDIR(st.st_mode))
328 ptr[0] = value>>24; ptr[1] = value>>16; ptr[2] = value>>8; ptr[3] =value;
332static void set_uint16_be(
eu8 * ptr,
eu32 value) {
333 ptr[0] = value>>8; ptr[1] = value; }
335static void set_uint32_le(
eu8 * ptr,
eu32 value) {
336 ptr[3] = value>>24; ptr[2] = value>>16; ptr[1] = value>>8; ptr[0] =value; }
341 ptr[1] = value>>8; ptr[0] =value;
352 eu8 yuvpalette[16][3],
eu8 rgbpalette[16][3])
357 if (memcmp(
xxfread(fh, buf, 12),
"DVDVIDEO-VTS", 12) != 0)
359 "(IFO) file %s not of type DVDVIDEO-VTS.",
filename);
363 xfseek0(fh, (offset * 0x800) + 12);
367 xxfread(fh, buf, (
size_t)(16 * 4));
369 for (
int i = 0; i < 16; i++)
374 eu8 *
p = buf + ((ptrdiff_t)(i) * 4) + 1;
375 yuvpalette[i][0] =
p[0]; yuvpalette[i][1] =
p[1]; yuvpalette[i][2] =
p[2];
377 rgbpalette[i][0] = r; rgbpalette[i][1] = g; rgbpalette[i][2] = b;
390 rgbpalpart[0] = r, rgbpalpart[1] = g, rgbpalpart[2] = b;
391 rgb2yuv(r, g, b, &y, &cr, &cb);
392 yuvpalpart[0] = y, yuvpalpart[1] = cr, yuvpalpart[2] = cb;
397 eu8 yuvpalette[16][3],
eu8 rgbpalette[16][3])
401 if (strlen(arg) != 20 || arg[6] !=
',' || arg[13] !=
',')
404 for (i = 0; i < 16; i++)
405 set2palettes(i * 0x111111, yuvpalette[i], rgbpalette[i]);
407 sscanf(arg,
"%x", &i);
409 sscanf(arg + 7,
"%x", &i);
411 sscanf(arg + 14,
"%x", &i);
440 const char *
filename,
int height,
int width)
452 memcpy(self->
m_buffer + 12,
"\004\003\0\0\0", 5);
461 xxfwriteCS(self->
m_fh,
"\0\0\0\001" "tRNS" "\0" "\x40\xe6\xd8\x66");
538 eu8 rv = *nibble & 0x0f;
543 *nibble = *(*data)++;
559 if ((bits & 0xc) != 0)
562 number = (bits & 0xc) >> 2;
563 cindex = bits & 0x3; }
566 bits = (bits << 4) |
getnibble(data, &nibble);
567 if ((bits & 0xf0) != 0)
570 number = (bits & 0x3c) >> 2;
575 bits = (bits << 4) |
getnibble(data, &nibble);
576 if ((bits & 0xfc0) != 0)
579 number = (bits & 0xfc) >> 2;
585 bits = (bits << 4) |
getnibble(data, &nibble);
586 number = (bits & 0x3fc) >> 2;
596 for (; number > 0 && col < width; number--, col++)
610 if (top < 0 || bot < 0)
612 printf(
"ERROR: invalid arguments to makebitmap");
615 eu8 * top_ibuf = data + top;
616 eu8 * bot_ibuf = data + bot;
621 for (
int i = 0; i < h / 2; i++)
633 uint32_t h =
pts / (3600 * 90000UL);
634 uint32_t m =
pts / (60 * 90000UL) % 60;
635 uint32_t s =
pts / 90000 % 60;
636 uint32_t hs = (
pts + 450) / 900 % 100;
639 sprintf(rvbuf,
"%02d+%02d+%02d.%02d.png", h, m, s, hs);
641 sprintf(rvbuf,
"%02d:%02d:%02d.%02d", h, m, s, hs);
674 bool createpics,
int delay_ms,
675 char * fnbuf,
char * fnbuf_fp)
680 time_t
volatile pt = 0;
681 bool volatile last =
false;
684 if (memcmp(
xxfread(sfh, data, 2),
"SP", 2) != 0)
694 eu32 volatile lastendpts = 0;
707 eu8 * ctrl = data + pack - 4;
708 xxfread(sfh, ctrl, size - pack);
712 if (memcmp(
xxfread(sfh, (
unsigned char *)junk, 2),
728 eu8 this_palette[4][3];
742 int colcon_length = 0;
753 for (
int n = 0; n < 4; n++) {
754 int i = (xpalette >> (n * 4) & 0x0f);
755 this_palette[n][0] = palette[i][0];
756 this_palette[n][1] = palette[i][1];
757 this_palette[n][2] = palette[i][2];
765 x1 = (
p[0] << 4) + (
p[1] >> 4);
766 x2 = ((
p[1] & 0xf) << 8) +
p[2];
767 y1 = (
p[3] << 4) + (
p[4] >> 4);
768 y2 = ((
p[4] & 0xf) << 8) +
p[5];
792 tmppts += delay_ms * 90;
798 eu32 endpts = tmppts + (end * 1000);
800 if (tmppts <= lastendpts)
802 if (lastendpts < endpts)
804 pts = lastendpts + (2 * (1000 / 30) * 90);
807 tmppts += delay_ms * 90;
808 printf(
"Fixed overlapping subtitle!\n");
812 printf(
"Dropping overlapping subtitle\n");
820 pts2ts(tmppts, sptsstr + 1,
false);
827 size_t len =
write(1, sptsstr, strlen(sptsstr));
828 if (len != strlen(sptsstr))
829 printf(
"ERROR: write failed");
832 fprintf(ofh,
" <spu start=\"%s\" end=\"%s\" image=\"%s\""
833 " xoffset=\"%d\" yoffset=\"%d\" />\n",
834 sptsstr + 1,
pts2ts(endpts, junk,
false),
839 bot_field - 4, fnbuf, this_palette);
847 size_t len =
write(1, sptsstr, strlen(sptsstr));
848 if (len != strlen(sptsstr))
849 printf(
"ERROR: write failed");
861 "Usage: %s [--delay ms] <supfile> <ifofile>|<palette>" "\n"
865 "\t ProjectX decoded recording.sup and recording.sup.IFO" "\n"
867 "\t\t$ pxsup2dast recording.sup*" "\n"
869 "\t Having test.sup and map.ifo" "\n"
871 "\t\t$ pxsup2dast test.sup map.ifo" "\n"
873 "\t No .IFO, so giving 3 colors (rgb components in hex)" "\n"
875 "\t\t$ pxsup2dast titles.sup ff0000,00ff00,0000ff" "\n"
877 "\t Trying to fix sync in recording" "\n"
879 "\t\t$ pxsup2dast --delay 750 recording.sup*" "\n"
890 for (
int i = 0; i < 16; i++)
895 if (fscanf(fh,
"%02x%02x%02x\n", &r, &g, &b) != 3 ||
896 r != palette[i][0] || g != palette[i][1] || b != palette[i][2])
906int sup2dast(
const char *supfile,
const char *ifofile ,
int delay_ms)
911 eu8 yuvpalette[16][3];
912 eu8 rgbpalette[16][3];
916 bool createpics =
false;
918 memset(yuvpalette, 0,
sizeof(yuvpalette));
919 memset(rgbpalette, 0,
sizeof(rgbpalette));
921 if (
write(1,
"\n", 1) != 1)
922 printf(
"ERROR: write failed");
924 if (
sizeof (
char) != 1 ||
sizeof (
int) < 2)
927 p = strrchr(supfile,
'.');
934 "not manage filenames longer than 950 characters.");
935 memcpy(fnbuf, supfile, i);
948 strcpy(
p,
"palette.ycrcb");
952 printf(
"Found palette.yuv having our palette information...\n");
953 printf(
"...Skipping image files, Writing spumux.tmp.\n");
958 printf(
"Writing image files and spumux.tmp.\n");
961 strcpy(
p,
"spumux.tmp");
965 pxsubtitle(supfile, fh, rgbpalette, createpics, delay_ms, fnbuf,
p);
967 if (
write(1,
"\n", 1) != 1)
968 printf(
"ERROR: write failed");
970 xxfwriteCS(fh,
" </stream>\n</subpictures>\n");
975 printf(
"Writing palette.ycrcb and palette.rgb.\n");
976 strcpy(
p,
"palette.ycrcb");
978 strcpy(
p,
"palette.rgb");
980 for (i = 0; i < 16; i++)
982 fprintf(yuvfh,
"%02x%02x%02x\n",
983 yuvpalette[i][0], yuvpalette[i][1], yuvpalette[i][2]);
984 fprintf(rgbfh,
"%02x%02x%02x\n",
985 rgbpalette[i][0], rgbpalette[i][1], rgbpalette[i][2]);
993 printf(
"Renaming spumux.tmp to spumux.xml.\n");
994 strcpy(
p,
"spumux.tmp");
996 memcpy(buf, fnbuf, i);
997 strcpy(&buf[i - 3],
"xml");
1002 printf(
"Output files reside in %s\n", fnbuf);
1003 printf(
"All done.\n");
1009 printf(
"ERROR: write failed");
1011 if (
write(2,
"\n", 1) != 1)
1012 printf(
"ERROR: write failed");
static uint32_t crc32(const unsigned char *data, int len)
static std::vector< uint32_t > pixel
--------------------------------------------------—**
std::chrono::duration< CHRONO_TYPE, std::ratio< 1, 90000 > > pts
def write(text, progress=True)
static void set_uint32_be(eu8 *ptr, eu32 value)
static char * pts2ts(fu32 pts, char *rvbuf, bool is_png_filename)
static bool fexists(const char *filename)
static fu32 get_uint32_le(const eu8 *bytes)
static GCCATTR_NORETURN void __exc_throw(int type)
static eu8 * boundstr_read(BoundStr *bs, int l)
static void png4file_close(Png4File *self)
struct exc__state * m_last
static bool samepalette(char *filename, eu8 palette[16][3])
static void yuv2rgb(int y, int cr, int cb, eu8 *r, eu8 *g, eu8 *b)
static void xxfwrite(FILE *stream, const eu8 *ptr, size_t size)
static void png4file_init(Png4File *self, eu8 palette[4][3])
static void ifopalette(const char *filename, eu8 yuvpalette[16][3], eu8 rgbpalette[16][3])
static void png4file_open(Png4File *self, const char *filename, int height, int width)
static void boundstr_init(BoundStr *bs, eu8 *p, int l)
static fu32 get_uint32_be(const eu8 *bytes)
static void rgb2yuv(eu8 r, eu8 g, eu8 b, eu8 *y, eu8 *cr, eu8 *cb)
static bool dexists(const char *filename)
static void set2palettes(int value, eu8 *yuvpalpart, eu8 *rgbpalpart)
static void argpalette(const char *arg, eu8 yuvpalette[16][3], eu8 rgbpalette[16][3])
static eu8 getnibble(eu8 **data, int *nibble)
static void pxsubtitle(const char *supfile, FILE *ofh, eu8 palette[16][3], bool createpics, int delay_ms, char *fnbuf, char *fnbuf_fp)
static void xfseek0(FILE *stream, long offset)
static GCCATTR_NORETURN void exc_throw(int type, const char *format,...)
static void png4file_flush(Png4File *self)
static void makebitmap(eu8 *data, int w, int h, int top, int bot, char *filename, eu8 palette[4][3])
static void xmkdir(const char *path, int mode)
int sup2dast(const char *supfile, const char *ifofile, int delay_ms)
static void png4file_endrow(Png4File *self)
static eu8 * xxfread(FILE *stream, eu8 *ptr, size_t size)
static FILE * xfopen(const char *filename, const char *mode)
static void xxfwrite_uint32_be(FILE *fh, eu32 value)
static void getpixelline(eu8 **data, int width, Png4File *picfile)
static void set_uint16_le(eu8 *ptr, eu16 value)
static fu16 get_uint16_be(const eu8 *bytes)
static eu8 clamp(eu8 value, eu8 low, eu8 high)
static void png4file_addpixel(Png4File *self, eu8 pixel)
static void usage(char *progname)
struct exc__state * m_prev