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