MythTV  master
pxsup2dast.c
Go to the documentation of this file.
1 /*
2  #
3  # pxsup2dast.c version YYYY-MM-DD (take from most recent change below).
4  #
5  # Project X sup to dvdauthor subtitle xml file.
6  # too ät iki piste fi
7  #
8  # -------------------------------------------------
9  #
10  # This is currently wery picky what this expects of ProjectX .sup to contain.
11  # Update: 2005/07/02 Not so picky anymore, but control sequence parsing is
12  # not perfect (yet!?)
13  #
14  # Change 2010-08-29: Initial handling of 0x07 control code (from
15  # Simon Liddicott). Currently just ignores contents but doesn't choke on
16  # it anymore...
17  #
18  # Change 2009-08-09: Renamed getline() as getpixelline() (to avoid function
19  # name collision. Fixed GPL version to 2 (only). Thanks Ville Skyttä.
20  #
21  # Change 2009-01-10: Added subtitle indexing change (fix) from patch
22  # sent by Ian Stewart.
23  #
24  # This program is released under GNU GPL version 2. Check
25  # http://www.fsf.org/licenses/licenses.html
26  # to get your own copy of the GPL v2 license.
27  #
28  */
29 
30 typedef enum { false = 0, true = 1 } bool;
31 typedef unsigned char bool8;
32 
33 
34 #include <unistd.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdarg.h>
38 #include <stddef.h>
39 #include <string.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <time.h>
43 #include <errno.h>
44 #include <ctype.h>
45 #include <setjmp.h>
46 #include <zlib.h>
47 #ifdef _WIN32
48 #include <dirent.h>
49 #endif
50 
51 #if 1
52 /* this is all speed, not so much portability -- using C99 features... */
53 #include <stdint.h>
54 
55 typedef int8_t ei8; typedef uint8_t eu8;
56 typedef int16_t ei16; typedef uint16_t eu16;
57 typedef int32_t ei32; typedef uint32_t eu32;
58 
59 typedef int_least8_t li8; typedef uint_least8_t lu8;
60 typedef int_least16_t li16; typedef uint_least16_t lu16;
61 typedef int_least32_t li32; typedef uint_least32_t lu32;
62 
63 typedef int_fast8_t fi8; typedef uint_fast8_t fu8;
64 typedef int_fast16_t fi16; typedef uint_fast16_t fu16;
65 typedef int_fast32_t fi32; typedef uint_fast32_t fu32;
66 #endif
67 
68 
69 #if (__GNUC__ >= 3)
70 #define GCCATTR_PRINTF(m, n) __attribute__ ((format (printf, m, n)))
71 #define GCCATTR_UNUSED __attribute ((unused))
72 #define GCCATTR_NORETURN __attribute ((noreturn))
73 #define GCCATTR_CONST __attribute ((const))
74 #else
75 #define GCCATTR_PRINTF(m, n)
76 #define GCCATTR_UNUSED
77 #define GCCATTR_NORETURN
78 #define GCCATTR_CONST
79 #endif
80 
81 /* use this only to cast quoted strings in function calls */
82 #define CUS (const unsigned char *)
83 
85 
86 int sup2dast(const char *supfile, const char *ifofile, int delay_ms);
87 
88 /****** Poor man's exception code ... (heavily inspired by cexcept). ******/
89 
90 struct exc__state
91 {
92  struct exc__state *m_prev;
93  jmp_buf m_env;
94 };
95 
96 struct
97 {
98  struct exc__state *m_last;
99  char m_msgbuf[1024];
100  int m_buflen;
101 } EXC /* = { 0 }*/ ;
102 
103 // These macros now all take a parameter 'x' to specify a recursion
104 // level. This will then generate unique variable names for each
105 // level of recursion, preventing the compiler from complaining about
106 // shadowed variables.
107 #define exc_try(x) do { struct exc__state exc_s##x; int exc_type##x GCCATTR_UNUSED; \
108  exc_s##x.m_prev = EXC.m_last; EXC.m_last = &exc_s##x; if ((exc_type##x = setjmp(exc_s##x.m_env)) == 0)
109 
110 #define exc_ftry(x) do { struct exc__state exc_s##x, *exc_p##x = EXC.m_last; \
111  int exc_type##x GCCATTR_UNUSED; exc_s##x.prev = EXC.m_last; \
112  EXC.m_last = &exc_s##x; if ((exc_type##x = setjmp(exc_s##x.env)) == 0)
113 
114 #define exc_catch(x,t) else if ((t) == exc_type##x)
115 
116 #define exc_end(x) else __exc_throw(exc_type##x); EXC.m_last = exc_s##x.m_prev; } while (0)
117 
118 #define exc_return(x) for (EXC.m_last = exc_p##x;) return
119 
120 #define exc_fthrow(x) for (EXC.m_last = exc_p##x;) ex_throw
121 
122 #define exc_catchall else
123 #define exc_endall(x) EXC.m_last = exc_s##x.m_prev; } while (0)
124 
125 #define exc_cleanup() EXC.m_last = NULL;
126 
127 static void __exc_throw(int type) /* protoadd GCCATTR_NORETURN */
128 {
129  struct exc__state * exc_s = EXC.m_last;
130  EXC.m_last = EXC.m_last->m_prev;
131  longjmp(exc_s->m_env, type);
132 }
133 
134 static void exc_throw(int type, const char * format, ...)
135  /* protoadd GCCATTR_NORETURN */
136 {
137  if (format != NULL)
138  {
139  va_list ap;
140  int err = errno;
141 
142  va_start(ap, format);
143  unsigned int len = vsnprintf(EXC.m_msgbuf, sizeof EXC.m_msgbuf, format, ap);
144  va_end(ap);
145 
146  if (len >= sizeof EXC.m_msgbuf)
147  {
148  len = sizeof EXC.m_msgbuf - 1;
149  EXC.m_msgbuf[len] = '\0';
150  }
151  else
152  {
153  if (format[strlen(format) - 1] == ':')
154  {
155  int l = snprintf(&EXC.m_msgbuf[len], sizeof EXC.m_msgbuf - len,
156  " %s.", strerror(err));
157  if (l + len >= sizeof EXC.m_msgbuf)
158  {
159  len = sizeof EXC.m_msgbuf - 1;
160  EXC.m_msgbuf[len] = '\0';
161  }
162  else
163  len += l;
164  }
165  }
166  EXC.m_buflen = len;
167  }
168  else
169  {
170  EXC.m_msgbuf[0] = '\0';
171  EXC.m_buflen = 0;
172  }
173 
174  __exc_throw(type);
175 }
176 
177 /****** end exception code block ******/
178 
179 
180 static eu8 * xxfread(FILE * stream, eu8 * ptr, size_t size)
181 {
182  size_t n = fread(ptr, size, 1, stream);
183  if (n == 0)
184  {
185  if (ferror(stream))
186  exc_throw(MiscError, "fread failure:");
187  exc_throw(EOFIndicator, NULL); }
188  return ptr;
189 }
190 
191 static void xxfwrite(FILE * stream, const eu8 * ptr, size_t size)
192 {
193  size_t n = fwrite(ptr, size, 1, stream);
194  if (n == 0)
195  {
196  if (ferror(stream))
197  exc_throw(MiscError, "fwrite failure:");
198  exc_throw(MiscError, "fwrite failure:");
199  }
200 }
201 
202 #define xxfwriteCS(f, s) xxfwrite(f, CUS s, sizeof (s) - 1)
203 
204 static void yuv2rgb(int y, int cr, int cb,
205  eu8 * r, eu8 * g, eu8 * b)
206 {
207  /* from dvdauthor... */
208  int lr = (500 + 1164 * (y - 16) + 1596 * (cr - 128) ) /1000;
209  int lg = (500 + 1164 * (y - 16) - 813 * (cr - 128) - 391 * (cb - 128)) / 1000;
210  int lb = (500 + 1164 * (y - 16) + 2018 * (cb - 128)) / 1000;
211 
212  *r = (lr < 0)? 0: (lr > 255)? 255: (eu8)lr;
213  *g = (lg < 0)? 0: (lg > 255)? 255: (eu8)lg;
214  *b = (lb < 0)? 0: (lb > 255)? 255: (eu8)lb;
215 }
216 
217 static void rgb2yuv(eu8 r, eu8 g, eu8 b,
218  eu8 * y, eu8 * cr, eu8 * cb)
219 {
220  /* int ly, lcr, lcb; */
221 
222  /* from dvdauthor... */
223  *y = ( 257 * r + 504 * g + 98 * b + 16500) / 1000;
224  *cr = ( 439 * r - 368 * g - 71 * b + 128500) / 1000;
225  *cb = (-148 * r - 291 * g + 439 * b + 128500) / 1000;
226 }
227 
228 /* the code above matches nicely with http://www.fourcc.org/fccyvrgb.php
229 
230  * RGB to YUV Conversion
231 
232  * Y = (0.257 * R) + (0.504 * G) + (0.098 * B) + 16
233  * Cr = V = (0.439 * R) - (0.368 * G) - (0.071 * B) + 128
234  * Cb = U = -(0.148 * R) - (0.291 * G) + (0.439 * B) + 128
235 
236  * YUV to RGB Conversion
237 
238  * B = 1.164(Y - 16) + 2.018(U - 128)
239  * G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128)
240  * R = 1.164(Y - 16) + 1.596(V - 128)
241 
242 */
243 
244 
245 static FILE * xfopen(const char * filename, const char * mode)
246 {
247  FILE * fh = fopen(filename, mode);
248  if (fh == NULL)
249  exc_throw(MiscError, "fopen(\"%s\", \"%s\") failure:", filename, mode);
250  return fh;
251 }
252 
253 static void xfseek0(FILE * stream, long offset)
254 {
255  if (fseek(stream, offset, SEEK_SET) < 0)
256  exc_throw(MiscError, "fseek(stream, %ld, SEEK_SET) failure:",offset);
257 }
258 
259 static void xmkdir(const char * path, int mode)
260 {
261 #ifdef _WIN32
262  (void)mode;
263  if (mkdir(path) < 0)
264  exc_throw(MiscError, "mkdir(%s%o) failure:", path);
265 #else
266  if (mkdir(path, mode) < 0)
267  exc_throw(MiscError, "mkdir(%s, 0%o) failure:", path, mode);
268 #endif
269 }
270 
271 
272 static bool fexists(const char * filename)
273 {
274  struct stat st;
275 
276  if (stat(filename, &st) == 0 && S_ISREG(st.st_mode))
277  return true;
278  return false;
279 }
280 
281 static bool dexists(const char * filename)
282 {
283  struct stat st;
284 
285  if (stat(filename, &st) == 0 && S_ISDIR(st.st_mode))
286  return true;
287 
288  return false;
289 }
290 
291 static fu32 get_uint32_be(const eu8 * bytes)
292 {
293  return (bytes[0] << 24) + (bytes[1] << 16) + (bytes[2] << 8) + bytes[3];
294 }
295 
296 static fu16 get_uint16_be(const eu8 * bytes)
297 {
298  return (bytes[0] << 8) + bytes[1];
299 }
300 
301 static fu32 get_uint32_le(const eu8 * bytes)
302 {
303  return (bytes[3] << 24) + (bytes[2] << 16) + (bytes[1] << 8) + bytes[0];
304 }
305 
306 #if 0 /* protoline */
307 static fu32 get_uint16_le(const eu8 * bytes) {
308  return (bytes[1] << 8) + bytes[0]; }
309 #endif /* protoline */
310 
311 
312 static void set_uint32_be(eu8 * ptr, eu32 value)
313 {
314  ptr[0] = value>>24; ptr[1] = value>>16; ptr[2] = value>>8; ptr[3] =value;
315 }
316 
317 #if 0 /* protoline */
318 static void set_uint16_be(eu8 * ptr, eu32 value) {
319  ptr[0] = value>>8; ptr[1] = value; }
320 
321 static void set_uint32_le(eu8 * ptr, eu32 value) {
322  ptr[3] = value>>24; ptr[2] = value>>16; ptr[1] = value>>8; ptr[0] =value; }
323 #endif /* protoline */
324 
325 static void set_uint16_le(eu8 * ptr, eu16 value)
326 {
327  ptr[1] = value>>8; ptr[0] =value;
328 }
329 
330 static void xxfwrite_uint32_be(FILE * fh, eu32 value)
331 {
332  eu8 buf[4];
333  set_uint32_be(buf, value);
334  xxfwrite(fh, buf, 4);
335 }
336 
337 static void ifopalette(const char * filename,
338  eu8 yuvpalette[16][3], eu8 rgbpalette[16][3])
339 {
340  eu8 buf[1024];
341 
342  FILE *fh = xfopen(filename, "rb");
343  if (memcmp(xxfread(fh, buf, 12), "DVDVIDEO-VTS", 12) != 0)
345  "(IFO) file %s not of type DVDVIDEO-VTS.", filename);
346 
347  xfseek0(fh, 0xcc);
348  fu32 offset = get_uint32_be(xxfread(fh, buf, 4));
349  xfseek0(fh, offset * 0x800 + 12);
350  fu32 pgc = offset * 0x800 + get_uint32_be(xxfread(fh, buf, 4));
351  /* seek to palette */
352  xfseek0(fh, pgc + 0xa4);
353  xxfread(fh, buf, (size_t)(16 * 4));
354  fclose(fh);
355  for (int i = 0; i < 16; i++)
356  {
357  eu8 r = 0;
358  eu8 g = 0;
359  eu8 b = 0;
360  eu8 * p = buf + (ptrdiff_t)(i) * 4 + 1;
361  yuvpalette[i][0] =p[0]; yuvpalette[i][1] =p[1]; yuvpalette[i][2] =p[2];
362  yuv2rgb(p[0], p[1], p[2], &r, &g, &b);
363  rgbpalette[i][0] = r; rgbpalette[i][1] = g; rgbpalette[i][2] = b;
364  }
365 }
366 
367 
368 static void set2palettes(int value, eu8 * yuvpalpart, eu8 * rgbpalpart)
369 {
370  eu8 y = 0;
371  eu8 cr = 0;
372  eu8 cb = 0;
373  eu8 r = value >> 16;
374  eu8 g = value >> 8;
375  eu8 b = value;
376  rgbpalpart[0] = r, rgbpalpart[1] = g, rgbpalpart[2] = b;
377  rgb2yuv(r, g, b, &y, &cr, &cb);
378  yuvpalpart[0] = y, yuvpalpart[1] = cr, yuvpalpart[2] = cb;
379 }
380 
381 
382 static void argpalette(const char * arg,
383  eu8 yuvpalette[16][3], eu8 rgbpalette[16][3])
384 {
385  unsigned int i = 0;
386 
387  if (strlen(arg) != 20 || arg[6] != ',' || arg[13] != ',')
388  exc_throw(MiscError, "Palette arg %s invalid.\n", arg);
389 
390  for (i = 0; i < 16; i++)
391  set2palettes(i * 0x111111, yuvpalette[i], rgbpalette[i]);
392 
393  sscanf(arg, "%x", &i); /* "%x," ? */
394  set2palettes(i, yuvpalette[1], rgbpalette[1]);
395  sscanf(arg + 7, "%x", &i);
396  set2palettes(i, yuvpalette[2], rgbpalette[2]);
397  sscanf(arg + 14, "%x", &i);
398  set2palettes(i, yuvpalette[3], rgbpalette[3]);
399 }
400 
401 
402 typedef struct Png4File
403 {
405  int m_width;
406  int m_hleft;
407  int m_nibble;
408  int m_bufPos;
413  eu8 m_buffer[65536];
414 } Png4File;
415 
416 static void png4file_init(Png4File * self, eu8 palette[4][3])
417 {
418  memcpy(self->m_paletteChunk, "\0\0\0\x0c" "PLTE", 8);
419  memcpy(self->m_paletteChunk + 8, palette, 12);
420  self->m_crc = 0 /*crc32(0, Z_NULL, 0)*/;
421  self->m_crc = crc32(self->m_crc, self->m_paletteChunk + 4, 16);
422  set_uint32_be(self->m_paletteChunk + 20, self->m_crc);
423 }
424 
425 static void png4file_open(Png4File * self,
426  const char * filename, int height, int width)
427 {
428  self->m_fh = xfopen(filename, "wb");
429  self->m_width = width;
430  self->m_hleft = height;
431  self->m_nibble = -1;
432 
433  xxfwrite(self->m_fh, CUS "\x89PNG\r\n\x1a\n" "\0\0\0\x0d", 12);
434 
435  memcpy(self->m_buffer, "IHDR", 4);
436  set_uint32_be(self->m_buffer + 4, width);
437  set_uint32_be(self->m_buffer + 8, height);
438  memcpy(self->m_buffer + 12, "\004\003\0\0\0", 5);
439 
440  eu32 crc = crc32(0, self->m_buffer, 17);
441  set_uint32_be(self->m_buffer + 17, crc);
442  xxfwrite(self->m_fh, self->m_buffer, 21);
443 
444  xxfwrite(self->m_fh, self->m_paletteChunk, sizeof self->m_paletteChunk);
445 
446  /* XXX quick hack, first color transparent. */
447  xxfwriteCS(self->m_fh, "\0\0\0\001" "tRNS" "\0" "\x40\xe6\xd8\x66");
448 
449  xxfwrite(self->m_fh, CUS "\0\0\0\0IDAT" "\x78\001", 10);
450  self->m_buffer[0] = '\0';
451  self->m_buffer[5] = '\0';
452  self->m_bufPos = 6;
453  self->m_chunkLen = 2; /* 78 01, zlib header */
454  self->m_crc = crc32(0, CUS "IDAT" "\x78\001", 6);
455  self->m_adler = 1 /* adler32(0, Z_NULL, 0) */;
456 }
457 
458 static void png4file_flush(Png4File * self)
459 {
460  int l = self->m_bufPos - 5;
461  self->m_chunkLen += self->m_bufPos;
462  set_uint16_le(self->m_buffer + 1, l);
463  set_uint16_le(self->m_buffer + 3, l ^ 0xffff);
464  xxfwrite(self->m_fh, self->m_buffer, self->m_bufPos);
465  self->m_crc = crc32(self->m_crc, self->m_buffer, self->m_bufPos);
466  self->m_adler = adler32(self->m_adler, self->m_buffer + 5, self->m_bufPos - 5);
467  self->m_buffer[0] = '\0';
468  self->m_bufPos = 5;
469 }
470 
471 static void png4file_addpixel(Png4File * self, eu8 pixel)
472 {
473  if (self->m_nibble < 0)
474  self->m_nibble = (pixel << 4);
475  else
476  {
477  self->m_buffer[self->m_bufPos++] = self->m_nibble | pixel;
478  self->m_nibble = -1;
479  if (self->m_bufPos == sizeof self->m_buffer - 4)
480  png4file_flush(self);
481  }
482 }
483 
484 static void png4file_endrow(Png4File * self)
485 {
486  if (self->m_nibble >= 0)
487  {
488  self->m_buffer[self->m_bufPos++] = self->m_nibble;
489  self->m_nibble = -1;
490  }
491 
492  self->m_hleft--;
493  if (self->m_hleft)
494  self->m_buffer[self->m_bufPos++] = '\0';
495 }
496 
497 static void png4file_close(Png4File * self)
498 {
499  eu8 adlerbuf[4];
500  self->m_buffer[0] = 0x01;
501  if (self->m_bufPos)
502  png4file_flush(self);
503  else
504  {
505  self->m_bufPos = 5;
506  png4file_flush(self);
507  }
508 
509  set_uint32_be(adlerbuf, self->m_adler);
510  xxfwrite(self->m_fh, adlerbuf, 4);
511  self->m_crc = crc32(self->m_crc, adlerbuf, 4);
512  xxfwrite_uint32_be(self->m_fh, self->m_crc);
513  xxfwriteCS(self->m_fh, "\0\0\0\0" "IEND" "\xae\x42\x60\x82");
514  xfseek0(self->m_fh, 70);
515  xxfwrite_uint32_be(self->m_fh, self->m_chunkLen + 4 /* adler*/);
516  fclose(self->m_fh);
517  self->m_fh = NULL;
518 }
519 
520 static eu8 getnibble(eu8 ** data, int * nibble)
521 {
522  if (*nibble >= 0)
523  {
524  eu8 rv = *nibble & 0x0f;
525  *nibble = -1;
526  return rv;
527  }
528  /* else */
529  *nibble = *(*data)++;
530  return *nibble >> 4;
531 }
532 
533 
534 static void getpixelline(eu8 ** data, int width, Png4File * picfile)
535 {
536  int nibble = -1;
537  int col = 0;
538  int number = 0;
539  int cindex = 0;
540  /* Originally from gtkspu - this from the python implementation of this */
541 
542  while (1)
543  {
544  int bits = getnibble(data, &nibble);
545  if ((bits & 0xc) != 0)
546  {
547  /* have 4-bit code */
548  number = (bits & 0xc) >> 2;
549  cindex = bits & 0x3; }
550  else
551  {
552  bits = (bits << 4) | getnibble(data, &nibble);
553  if ((bits & 0xf0) != 0)
554  {
555  /* have 8-bit code */
556  number = (bits & 0x3c) >> 2;
557  cindex = bits & 0x3;
558  }
559  else
560  {
561  bits = (bits << 4) | getnibble(data, &nibble);
562  if ((bits & 0xfc0) != 0)
563  {
564  /* have 12-bit code */
565  number = (bits & 0xfc) >> 2;
566  cindex = bits & 0x3;
567  }
568  else
569  {
570  /* have 16-bit code */
571  bits = (bits << 4) | getnibble(data, &nibble);
572  number = (bits & 0x3fc) >> 2;
573  cindex = bits & 0x3;
574 
575  if (number == 0)
576  number = width;
577  }
578  }
579  }
580 
581  /* printf("%d %d %d %d\n", number, col, width, cindex); */
582  for (; number > 0 && col < width; number--, col++)
583  png4file_addpixel(picfile, cindex);
584 
585  if (col == width)
586  {
587  png4file_endrow(picfile);
588  return;
589  }
590  }
591 }
592 
593 static void makebitmap(eu8 * data, int w, int h, int top, int bot,
594  char * filename, eu8 palette[4][3])
595 {
596  eu8 * top_ibuf = data + top;
597  eu8 * bot_ibuf = data + bot;
598  Png4File picfile;
599 
600  png4file_init(&picfile, palette); /* not bottleneck even re-doing this every time */
601  png4file_open(&picfile, filename, h, w);
602  for (int i = 0; i < h / 2; i++)
603  {
604  getpixelline(&top_ibuf, w, &picfile);
605  getpixelline(&bot_ibuf, w, &picfile);
606  }
607 
608  png4file_close(&picfile);
609 }
610 
611 
612 static char * pts2ts(fu32 pts, char * rvbuf, bool is_png_filename)
613 {
614  uint32_t h = pts / (3600 * 90000UL);
615  uint32_t m = pts / (60 * 90000UL) % 60;
616  uint32_t s = pts / 90000 % 60;
617  uint32_t hs = (pts + 450) / 900 % 100;
618 
619  if (is_png_filename)
620  sprintf(rvbuf, "%02d+%02d+%02d.%02d.png", h, m, s, hs);
621  else
622  sprintf(rvbuf, "%02d:%02d:%02d.%02d", h, m, s, hs);
623 
624  return rvbuf;
625 }
626 
627 /* *********** */
628 
629 typedef struct BoundStr
630 {
632  int m_l;
633 } BoundStr;
634 
635 static void boundstr_init(BoundStr * bs, eu8 * p, int l)
636 {
637  bs->m_p = p;
638  bs->m_l = l;
639 }
640 
641 static eu8 * boundstr_read(BoundStr * bs, int l)
642 {
643  if (l > bs->m_l)
644  exc_throw(IndexError, "XXX IndexError %p.", bs);
645  eu8 * rp = bs->m_p;
646  bs->m_p += l;
647  bs->m_l -= l;
648  return rp;
649 }
650 
651 /* *********** */
652 
653 
654 static void pxsubtitle(const char * supfile, FILE * ofh, eu8 palette[16][3],
655  bool createpics, int delay_ms,
656  char * fnbuf, char * fnbuf_fp)
657 {
658  char junk[32];
659  char sptsstr[32];
660  eu8 data[65536];
661  time_t volatile pt = 0;
662  bool volatile last = false;
663  /*char transparent[8]; */
664  FILE * sfh = xfopen(supfile, "rb");
665  if (memcmp(xxfread(sfh, data, 2), "SP", 2) != 0)
666  {
667  exc_throw(MiscError, "Syncword missing. XXX bailing out.");
668  }
669 
670  /*sprintf(transparent,
671  "%02x%02x%02x", palette[0][0], palette[0][1], palette[0][2]); */
672 
673  exc_try(1)
674  {
675  eu32 volatile lastendpts = 0;
676 
677  while (1)
678  {
679  /* X.java reads 5 bytes of pts, SubPicture.java writes 4. With
680  * 4 bytes 47721 seconds (13.25 hours) can be handled.
681  * Later, bytes 1,2,3,4 (instead of 0,1,2,3) could be used.
682  * 256/90000 = 1/351 sec -- way better than required resolution.
683  */
684  eu32 volatile pts = get_uint32_le(xxfread(sfh, data, 8));
685  eu16 size = get_uint16_be(xxfread(sfh, data, 2));
686  eu16 pack = get_uint16_be(xxfread(sfh, data, 2));
687  xxfread(sfh, data, pack - 4);
688  eu8 * ctrl = data + pack - 4;
689  xxfread(sfh, ctrl, size - pack);
690 
691  exc_try(2)
692  {
693  if (memcmp(xxfread(sfh, (unsigned char *)junk, 2),
694  "SP", 2) != 0)
695  exc_throw(MiscError, "Syncword missing. XXX bailing out.");
696  }
698  last = true;
699  exc_end(2);
700  {
701  BoundStr bs;
702  int x1 = -1;
703  int x2 = -1;
704  int y1 = -1;
705  int y2 = -1;
706  int top_field = -1;
707  int bot_field = -1;
708  int end = 0;
709  eu8 this_palette[4][3];
710  boundstr_init(&bs, ctrl, size - pack);
711 
712  int prev = 0;
713  while (1)
714  {
715  int date = get_uint16_be(boundstr_read(&bs, 2));
716  int next = get_uint16_be(boundstr_read(&bs, 2));
717 
718  while (1)
719  {
720  eu8 * p = 0;
721  eu8 cmd = boundstr_read(&bs, 1)[0];
722  int xpalette = 0;
723  int colcon_length = 0;
724  switch (cmd)
725  {
726  case 0x00: /* force display: */
727  case 0x01: /* start date (read above) */
728  continue;
729  case 0x02: /* stop date (read above) */
730  end = date;
731  continue;
732  case 0x03: /* palette */
733  xpalette = get_uint16_be(boundstr_read(&bs, 2));
734  for (int n = 0; n < 4; n++) {
735  int i = (xpalette >> (n * 4) & 0x0f);
736  this_palette[n][0] = palette[i][0];
737  this_palette[n][1] = palette[i][1];
738  this_palette[n][2] = palette[i][2];
739  }
740  continue;
741  case 0x04: /* alpha channel */
742  /*alpha =*/ boundstr_read(&bs, 2);
743  continue;
744  case 0x05: /* coordinates */
745  p = boundstr_read(&bs, 6);
746  x1 = (p[0] << 4) + (p[1] >> 4);
747  x2 = ((p[1] & 0xf) << 8) + p[2];
748  y1 = (p[3] << 4) + (p[4] >> 4);
749  y2 = ((p[4] & 0xf) << 8) + p[5];
750  continue;
751  case 0x06: /* rle offsets */
752  top_field = get_uint16_be(boundstr_read(&bs, 2));
753  bot_field = get_uint16_be(boundstr_read(&bs, 2));
754  continue;
755  case 0x07: /* */
756  colcon_length = get_uint16_be(boundstr_read(&bs, 2)-2);
757  boundstr_read(&bs, colcon_length);
758  continue;
759  }
760  if (cmd == 0xff) /* end command */
761  break;
762  exc_throw(MiscError, "%d: Unknown control sequence", cmd);
763  }
764 
765  if (prev == next)
766  break;
767  prev = next;
768  }
769 
770  /* check for overlapping subtitles */
771  eu32 tmppts = pts;
772  if (delay_ms)
773  tmppts += delay_ms * 90;
774 
775  /* hack to fix projectx bug adding wrong end times around a cut point*/
776  if (end > 500)
777  end = 500;
778 
779  eu32 endpts = tmppts + end * 1000; /* ProjectX ! (other: 900, 1024) */
780 
781  if (tmppts <= lastendpts)
782  {
783  if (lastendpts < endpts)
784  {
785  pts = lastendpts + 2 * (1000 / 30) * 90;/*??? */
786  tmppts = pts;
787  if (delay_ms)
788  tmppts += delay_ms * 90;
789  printf("Fixed overlapping subtitle!\n");
790  }
791  else
792  {
793  printf("Dropping overlapping subtitle\n");
794  continue;
795  }
796  }
797 
798  lastendpts = endpts;
799 
800  pts2ts(pts, fnbuf_fp, true);
801  pts2ts(tmppts, sptsstr + 1, false);
802 
803  time_t ct = 0;
804  if (pt != time(&ct))
805  {
806  pt = ct;
807  sptsstr[0] = '\r';
808  size_t len = write(1, sptsstr, strlen(sptsstr));
809  if (len != strlen(sptsstr))
810  printf("ERROR: write failed");
811  }
812 
813  fprintf(ofh, " <spu start=\"%s\" end=\"%s\" image=\"%s\""
814  " xoffset=\"%d\" yoffset=\"%d\" />\n",
815  sptsstr + 1, pts2ts(endpts, junk, false),
816  fnbuf, x1, y1);
817 
818  if (createpics)
819  makebitmap(data, x2 - x1 + 1, y2 - y1 + 1, top_field - 4,
820  bot_field - 4, fnbuf, this_palette);
821  if (last)
822  exc_throw(EOFIndicator, NULL);
823  }
824  }
825  }
827  {
828  size_t len = write(1, sptsstr, strlen(sptsstr));
829  if (len != strlen(sptsstr))
830  printf("ERROR: write failed");
831  exc_cleanup();
832  return;
833  }
834  exc_end(1);
835 }
836 
837 #if 0
838 static void usage(const char * pn) /* protoadd GCCATTR_NORETURN */
839 {
840  exc_throw(MiscError, "\n"
841  "Usage: %s [--delay ms] <supfile> <ifofile>|<palette>" "\n"
842  "\n"
843  "\tExamples:" "\n"
844  "\n"
845  "\t ProjectX decoded recording.sup and recording.sup.IFO" "\n"
846  "\n"
847  "\t\t$ pxsup2dast recording.sup*" "\n"
848  "\n"
849  "\t Having test.sup and map.ifo" "\n"
850  "\n"
851  "\t\t$ pxsup2dast test.sup map.ifo" "\n"
852  "\n"
853  "\t No .IFO, so giving 3 colors (rgb components in hex)" "\n"
854  "\n"
855  "\t\t$ pxsup2dast titles.sup ff0000,00ff00,0000ff" "\n"
856  "\n"
857  "\t Trying to fix sync in recording" "\n"
858  "\n"
859  "\t\t$ pxsup2dast --delay 750 recording.sup*" "\n"
860  , pn);
861 }
862 #endif
863 
864 static bool samepalette(char * filename, eu8 palette[16][3])
865 {
866  if (!fexists(filename))
867  return false;
868 
869  FILE *fh = xfopen(filename, "rb");
870  for (int i = 0; i < 16; i++)
871  {
872  unsigned int r=0;
873  unsigned int g=0;
874  unsigned int b=0;
875  if (fscanf(fh, "%02x%02x%02x\n", &r, &g, &b) != 3 ||
876  r != palette[i][0] || g != palette[i][1] || b != palette[i][2])
877  {
878  fclose(fh);
879  return false;
880  }
881  }
882  fclose(fh);
883  return true;
884 }
885 
886 int sup2dast(const char *supfile, const char *ifofile ,int delay_ms)
887 {
888  exc_try(1)
889  {
890  int i = 0;
891  eu8 yuvpalette[16][3];
892  eu8 rgbpalette[16][3];
893  char fnbuf[1024];
894  char * p = NULL;
895 
896  bool createpics = false;
897 
898  memset(yuvpalette, 0, sizeof(yuvpalette));
899  memset(rgbpalette, 0, sizeof(rgbpalette));
900 
901  if (write(1, "\n", 1) != 1)
902  printf("ERROR: write failed");
903 
904  if (sizeof (char) != 1 || sizeof (int) < 2) /* very unlikely */
905  exc_throw(MiscError, "Incompatible variable sizes.");
906 
907  if ((p = strrchr(supfile, '.')) != NULL)
908  i = p - supfile;
909  else
910  i = strlen(supfile);
911  if (i > 950)
912  exc_throw(MiscError, "Can "
913  "not manage filenames longer than 950 characters.");
914  memcpy(fnbuf, supfile, i);
915  p = fnbuf + i;
916  strcpy(p, ".d/");
917  p += strlen(p);
918 
919  if (!dexists(fnbuf))
920  xmkdir(fnbuf, 0755);
921 
922  if (fexists(ifofile))
923  ifopalette(ifofile, yuvpalette, rgbpalette);
924  else
925  argpalette(ifofile, yuvpalette, rgbpalette);
926 
927  strcpy(p, "palette.ycrcb");
928  if (samepalette(fnbuf, yuvpalette))
929  {
930  createpics = false;
931  printf("Found palette.yuv having our palette information...\n");
932  printf("...Skipping image files, Writing spumux.tmp.\n");
933  }
934  else
935  {
936  createpics = true;
937  printf("Writing image files and spumux.tmp.\n");
938  }
939 
940  strcpy(p, "spumux.tmp");
941  FILE *fh = xfopen(fnbuf, "wb");
942 
943  xxfwriteCS(fh, "<subpictures>\n <stream>\n");
944  pxsubtitle(supfile, fh, rgbpalette, createpics, delay_ms, fnbuf, p);
945 
946  if (write(1, "\n", 1) != 1)
947  printf("ERROR: write failed");
948 
949  xxfwriteCS(fh, " </stream>\n</subpictures>\n");
950  fclose(fh);
951 
952  if (createpics)
953  {
954  printf("Writing palette.ycrcb and palette.rgb.\n");
955  strcpy(p, "palette.ycrcb");
956  FILE *yuvfh = xfopen(fnbuf, "wb");
957  strcpy(p, "palette.rgb");
958  FILE *rgbfh = xfopen(fnbuf, "wb");
959  for (i = 0; i < 16; i++)
960  {
961  fprintf(yuvfh, "%02x%02x%02x\n",
962  yuvpalette[i][0], yuvpalette[i][1], yuvpalette[i][2]);
963  fprintf(rgbfh, "%02x%02x%02x\n",
964  rgbpalette[i][0], rgbpalette[i][1], rgbpalette[i][2]);
965  }
966  fclose(yuvfh);
967  fclose(rgbfh);
968  }
969 
970  {
971  char buf[1024];
972  printf("Renaming spumux.tmp to spumux.xml.\n");
973  strcpy(p, "spumux.tmp");
974  i = strlen(fnbuf);
975  memcpy(buf, fnbuf, i);
976  strcpy(&buf[i - 3], "xml");
977  unlink(buf);
978  rename(fnbuf, buf);
979  }
980  p[0] = '\0';
981  printf("Output files reside in %s\n", fnbuf);
982  printf("All done.\n");
983  }
984 
985  exc_catchall
986  {
987  if (write(2, EXC.m_msgbuf, EXC.m_buflen) != EXC.m_buflen)
988  printf("ERROR: write failed");
989 
990  if (write(2, "\n", 1) != 1)
991  printf("ERROR: write failed");
992 
993  exc_cleanup();
994  return 1;
995  }
996  exc_endall(1);
997 
998  return 0;
999 }
BoundStr::m_p
eu8 * m_p
Definition: pxsup2dast.c:631
xfseek0
static void xfseek0(FILE *stream, long offset)
Definition: pxsup2dast.c:253
bool
bool
Definition: pxsup2dast.c:30
li16
int_least16_t li16
Definition: pxsup2dast.c:60
exc__state::m_prev
struct exc__state * m_prev
Definition: pxsup2dast.c:92
samepalette
static bool samepalette(char *filename, eu8 palette[16][3])
Definition: pxsup2dast.c:864
Png4File
struct Png4File Png4File
ei16
int16_t ei16
Definition: pxsup2dast.c:56
pxsubtitle
static void pxsubtitle(const char *supfile, FILE *ofh, eu8 palette[16][3], bool createpics, int delay_ms, char *fnbuf, char *fnbuf_fp)
Definition: pxsup2dast.c:654
xxfwrite
static void xxfwrite(FILE *stream, const eu8 *ptr, size_t size)
Definition: pxsup2dast.c:191
x2
static int x2
Definition: mythsocket.cpp:51
Png4File::m_buffer
eu8 m_buffer[65536]
Definition: pxsup2dast.c:413
rgb2yuv
static void rgb2yuv(eu8 r, eu8 g, eu8 b, eu8 *y, eu8 *cr, eu8 *cb)
Definition: pxsup2dast.c:217
exc_end
#define exc_end(x)
Definition: pxsup2dast.c:116
mythburn.write
def write(text, progress=True)
Definition: mythburn.py:308
set2palettes
static void set2palettes(int value, eu8 *yuvpalpart, eu8 *rgbpalpart)
Definition: pxsup2dast.c:368
ifopalette
static void ifopalette(const char *filename, eu8 yuvpalette[16][3], eu8 rgbpalette[16][3])
Definition: pxsup2dast.c:337
exc__state::m_env
jmp_buf m_env
Definition: pxsup2dast.c:93
fu32
uint_fast32_t fu32
Definition: pxsup2dast.c:65
pixel
static guint32 * pixel
--------------------------------------------------—**
Definition: goom_core.cpp:24
argpalette
static void argpalette(const char *arg, eu8 yuvpalette[16][3], eu8 rgbpalette[16][3])
Definition: pxsup2dast.c:382
EXC
struct @87 EXC
Png4File::m_paletteChunk
eu8 m_paletteChunk[24]
Definition: pxsup2dast.c:412
exc_try
#define exc_try(x)
Definition: pxsup2dast.c:107
bool8
unsigned char bool8
Definition: pxsup2dast.c:31
__exc_throw
static void __exc_throw(int type)
Definition: pxsup2dast.c:127
exc_endall
#define exc_endall(x)
Definition: pxsup2dast.c:123
dexists
static bool dexists(const char *filename)
Definition: pxsup2dast.c:281
fi8
int_fast8_t fi8
Definition: pxsup2dast.c:63
mythburn.FILE
int FILE
Definition: mythburn.py:139
xxfwrite_uint32_be
static void xxfwrite_uint32_be(FILE *fh, eu32 value)
Definition: pxsup2dast.c:330
BoundStr::m_l
int m_l
Definition: pxsup2dast.c:632
lu16
uint_least16_t lu16
Definition: pxsup2dast.c:60
Png4File::m_bufPos
int m_bufPos
Definition: pxsup2dast.c:408
li8
int_least8_t li8
Definition: pxsup2dast.c:59
CUS
#define CUS
Definition: pxsup2dast.c:82
set_uint32_be
static void set_uint32_be(eu8 *ptr, eu32 value)
Definition: pxsup2dast.c:312
m_msgbuf
char m_msgbuf[1024]
Definition: pxsup2dast.c:99
yuv2rgb
static void yuv2rgb(int y, int cr, int cb, eu8 *r, eu8 *g, eu8 *b)
Definition: pxsup2dast.c:204
Png4File
Definition: pxsup2dast.c:402
hardwareprofile.config.p
p
Definition: config.py:33
makebitmap
static void makebitmap(eu8 *data, int w, int h, int top, int bot, char *filename, eu8 palette[4][3])
Definition: pxsup2dast.c:593
png4file_addpixel
static void png4file_addpixel(Png4File *self, eu8 pixel)
Definition: pxsup2dast.c:471
BoundStr
struct BoundStr BoundStr
Png4File::m_width
int m_width
Definition: pxsup2dast.c:405
x1
static int x1
Definition: mythsocket.cpp:50
m_buflen
int m_buflen
Definition: pxsup2dast.c:100
exc__state
Definition: pxsup2dast.c:90
boundstr_read
static eu8 * boundstr_read(BoundStr *bs, int l)
Definition: pxsup2dast.c:641
lu32
uint_least32_t lu32
Definition: pxsup2dast.c:61
li32
int_least32_t li32
Definition: pxsup2dast.c:61
xxfread
static eu8 * xxfread(FILE *stream, eu8 *ptr, size_t size)
Definition: pxsup2dast.c:180
fu16
uint_fast16_t fu16
Definition: pxsup2dast.c:64
Png4File::m_fh
FILE * m_fh
Definition: pxsup2dast.c:404
Png4File::m_nibble
int m_nibble
Definition: pxsup2dast.c:407
png4file_flush
static void png4file_flush(Png4File *self)
Definition: pxsup2dast.c:458
Png4File::m_adler
eu32 m_adler
Definition: pxsup2dast.c:411
usage
static void usage(char *progname)
Definition: replex.cpp:2415
eu32
uint32_t eu32
Definition: pxsup2dast.c:57
getnibble
static eu8 getnibble(eu8 **data, int *nibble)
Definition: pxsup2dast.c:520
exc_cleanup
#define exc_cleanup()
Definition: pxsup2dast.c:125
ei32
int32_t ei32
Definition: pxsup2dast.c:57
EOFIndicator
@ EOFIndicator
Definition: pxsup2dast.c:84
png4file_open
static void png4file_open(Png4File *self, const char *filename, int height, int width)
Definition: pxsup2dast.c:425
pts2ts
static char * pts2ts(fu32 pts, char *rvbuf, bool is_png_filename)
Definition: pxsup2dast.c:612
xfopen
static FILE * xfopen(const char *filename, const char *mode)
Definition: pxsup2dast.c:245
musicbrainzngs.compat.bytes
bytes
Definition: compat.py:49
fexists
static bool fexists(const char *filename)
Definition: pxsup2dast.c:272
BoundStr
Definition: pxsup2dast.c:629
xmkdir
static void xmkdir(const char *path, int mode)
Definition: pxsup2dast.c:259
exc_catch
#define exc_catch(x, t)
Definition: pxsup2dast.c:114
getpixelline
static void getpixelline(eu8 **data, int width, Png4File *picfile)
Definition: pxsup2dast.c:534
MiscError
@ MiscError
Definition: pxsup2dast.c:84
lu8
uint_least8_t lu8
Definition: pxsup2dast.c:59
png4file_endrow
static void png4file_endrow(Png4File *self)
Definition: pxsup2dast.c:484
fu8
uint_fast8_t fu8
Definition: pxsup2dast.c:63
get_uint32_be
static fu32 get_uint32_be(const eu8 *bytes)
Definition: pxsup2dast.c:291
Png4File::m_hleft
int m_hleft
Definition: pxsup2dast.c:406
Png4File::m_crc
eu32 m_crc
Definition: pxsup2dast.c:410
fi16
int_fast16_t fi16
Definition: pxsup2dast.c:64
eu8
uint8_t eu8
Definition: pxsup2dast.c:55
mpeg::chrono::pts
std::chrono::duration< CHRONO_TYPE, std::ratio< 1, 90000 > > pts
Definition: mythchrono.h:55
png4file_close
static void png4file_close(Png4File *self)
Definition: pxsup2dast.c:497
boundstr_init
static void boundstr_init(BoundStr *bs, eu8 *p, int l)
Definition: pxsup2dast.c:635
fi32
int_fast32_t fi32
Definition: pxsup2dast.c:65
exc_throw
static void exc_throw(int type, const char *format,...)
Definition: pxsup2dast.c:134
png4file_init
static void png4file_init(Png4File *self, eu8 palette[4][3])
Definition: pxsup2dast.c:416
uint16_t
unsigned short uint16_t
Definition: iso6937tables.h:3
xxfwriteCS
#define xxfwriteCS(f, s)
Definition: pxsup2dast.c:202
get_uint16_be
static fu16 get_uint16_be(const eu8 *bytes)
Definition: pxsup2dast.c:296
set_uint16_le
static void set_uint16_le(eu8 *ptr, eu16 value)
Definition: pxsup2dast.c:325
exc_catchall
#define exc_catchall
Definition: pxsup2dast.c:122
crc32
static uint32_t crc32(const unsigned char *data, int len)
Definition: dsmcc.cpp:616
build_compdb.filename
filename
Definition: build_compdb.py:21
get_uint32_le
static fu32 get_uint32_le(const eu8 *bytes)
Definition: pxsup2dast.c:301
ei8
int8_t ei8
Definition: pxsup2dast.c:55
Png4File::m_chunkLen
int m_chunkLen
Definition: pxsup2dast.c:409
sup2dast
int sup2dast(const char *supfile, const char *ifofile, int delay_ms)
Definition: pxsup2dast.c:886
IndexError
@ IndexError
Definition: pxsup2dast.c:84
eu16
uint16_t eu16
Definition: pxsup2dast.c:56
m_last
struct exc__state * m_last
Definition: pxsup2dast.c:98