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