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