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 * prev;
93  jmp_buf env;
94 };
95 
96 struct
97 {
98  struct exc__state * last;
99  char msgbuf[1024];
100  int buflen;
101 } EXC /* = { 0 }*/ ;
102 
103 #define exc_try do { struct exc__state exc_s; int exc_type GCCATTR_UNUSED; \
104  exc_s.prev = EXC.last; EXC.last = &exc_s; if ((exc_type = setjmp(exc_s.env)) == 0)
105 
106 #define exc_ftry do { struct exc__state exc_s, *exc_p = EXC.last; \
107  int exc_type GCCATTR_UNUSED; exc_s.prev = EXC.last; \
108  EXC.last = &exc_s; if ((exc_type = setjmp(exc_s.env)) == 0)
109 
110 #define exc_catch(t) else if (t == exc_type)
111 
112 #define exc_end else __exc_throw(exc_type); EXC.last = exc_s.prev; } while (0)
113 
114 #define exc_return for (EXC.last = exc_p;;) return
115 
116 #define exc_fthrow for (EXC.last = exc_p;;) ex_throw
117 
118 #define exc_catchall else
119 #define exc_endall EXC.last = exc_s.prev; } while (0)
120 
121 static void __exc_throw(int type) /* protoadd GCCATTR_NORETURN */
122 {
123  struct exc__state * exc_s;
124  exc_s = EXC.last;
125  EXC.last = EXC.last->prev;
126  longjmp(exc_s->env, type);
127 }
128 
129 static void exc_throw(int type, const char * format, ...)
130  /* protoadd GCCATTR_NORETURN */
131 {
132  if (format != NULL)
133  {
134  va_list ap;
135  unsigned int len;
136  int err = errno;
137 
138  va_start(ap, format);
139  len = vsnprintf(EXC.msgbuf, sizeof EXC.msgbuf, format, ap);
140  va_end(ap);
141 
142  if (len >= sizeof EXC.msgbuf)
143  {
144  len = sizeof EXC.msgbuf - 1;
145  EXC.msgbuf[len] = '\0';
146  }
147  else
148  {
149  if (format[strlen(format) - 1] == ':')
150  {
151  int l = snprintf(&EXC.msgbuf[len], sizeof EXC.msgbuf - len,
152  " %s.", strerror(err));
153  if (l + len >= sizeof EXC.msgbuf)
154  {
155  len = sizeof EXC.msgbuf - 1;
156  EXC.msgbuf[len] = '\0';
157  }
158  else
159  len += l;
160  }
161  }
162  EXC.buflen = len;
163  }
164  else
165  {
166  EXC.msgbuf[0] = '\0';
167  EXC.buflen = 0;
168  }
169 
170  __exc_throw(type);
171 }
172 
173 /****** end exception code block ******/
174 
175 
176 static eu8 * xxfread(FILE * stream, eu8 * ptr, size_t size)
177 {
178  size_t n = fread(ptr, size, 1, stream);
179  if (n == 0)
180  {
181  if (ferror(stream))
182  exc_throw(MiscError, "fread failure:");
184  return ptr;
185 }
186 
187 static void xxfwrite(FILE * stream, const eu8 * ptr, size_t size)
188 {
189  size_t n = fwrite(ptr, size, 1, stream);
190  if (n == 0)
191  {
192  if (ferror(stream))
193  exc_throw(MiscError, "fwrite failure:");
194  exc_throw(MiscError, "fwrite failure:");
195  }
196 }
197 
198 #define xxfwriteCS(f, s) xxfwrite(f, CUS s, sizeof s - 1)
199 
200 static void yuv2rgb(int y, int cr, int cb,
201  eu8 * r, eu8 * g, eu8 * b)
202 {
203  int lr, lg, lb;
204 
205  /* from dvdauthor... */
206  lr = (500 + 1164 * (y - 16) + 1596 * (cr - 128) ) /1000;
207  lg = (500 + 1164 * (y - 16) - 813 * (cr - 128) - 391 * (cb - 128)) / 1000;
208  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], r, g, b;
333  fu32 offset, pgc;
334  int i;
335  FILE * fh;
336 
337  fh = xfopen(filename, "rb");
338  if (memcmp(xxfread(fh, buf, 12), "DVDVIDEO-VTS", 12) != 0)
340  "(IFO) file %s not of type DVDVIDEO-VTS.", filename);
341 
342  xfseek0(fh, 0xcc);
343  offset = get_uint32_be(xxfread(fh, buf, 4));
344  xfseek0(fh, offset * 0x800 + 12);
345  pgc = offset * 0x800 + get_uint32_be(xxfread(fh, buf, 4));
346  /* seek to palette */
347  xfseek0(fh, pgc + 0xa4);
348  xxfread(fh, buf, 16 * 4);
349  fclose(fh);
350  for (i = 0; i < 16; i++)
351  {
352  eu8 * p = buf + i * 4 + 1;
353  yuvpalette[i][0] =p[0]; yuvpalette[i][1] =p[1]; yuvpalette[i][2] =p[2];
354  yuv2rgb(p[0], p[1], p[2], &r, &g, &b);
355  rgbpalette[i][0] = r; rgbpalette[i][1] = g; rgbpalette[i][2] = b;
356  }
357 }
358 
359 
360 static void set2palettes(int value, eu8 * yuvpalpart, eu8 * rgbpalpart)
361 {
362  eu8 r, g, b, y, cr, cb;
363 
364  r = value >> 16; g = value >> 8; 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;
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 {
394  FILE * fh;
395  int width;
396  int hleft;
397  int nibble;
398  int bufpos;
399  int chunklen;
403  eu8 buffer[65536];
404 };
405 
406 static void png4file_init(Png4File * self, eu8 palette[4][3])
407 {
408  memcpy(self->palettechunk, "\0\0\0\x0c" "PLTE", 8);
409  memcpy(self->palettechunk + 8, palette, 12);
410  self->crc = 0 /*crc32(0, Z_NULL, 0)*/;
411  self->crc = crc32(self->crc, self->palettechunk + 4, 16);
412  set_uint32_be(self->palettechunk + 20, self->crc);
413 }
414 
415 static void png4file_open(Png4File * self,
416  const char * filename, int height, int width)
417 {
418  eu32 crc;
419  self->fh = xfopen(filename, "wb");
420  self->width = width;
421  self->hleft = height;
422  self->nibble = -1;
423 
424  xxfwrite(self->fh, CUS "\x89PNG\r\n\x1a\n" "\0\0\0\x0d", 12);
425 
426  memcpy(self->buffer, "IHDR", 4);
427  set_uint32_be(self->buffer + 4, width);
428  set_uint32_be(self->buffer + 8, height);
429  memcpy(self->buffer + 12, "\004\003\0\0\0", 5);
430 
431  crc = crc32(0, self->buffer, 17);
432  set_uint32_be(self->buffer + 17, crc);
433  xxfwrite(self->fh, self->buffer, 21);
434 
435  xxfwrite(self->fh, self->palettechunk, sizeof self->palettechunk);
436 
437  /* XXX quick hack, first color transparent. */
438  xxfwriteCS(self->fh, "\0\0\0\001" "tRNS" "\0" "\x40\xe6\xd8\x66");
439 
440  xxfwrite(self->fh, CUS "\0\0\0\0IDAT" "\x78\001", 10);
441  self->buffer[0] = '\0';
442  self->buffer[5] = '\0';
443  self->bufpos = 6;
444  self->chunklen = 2; /* 78 01, zlib header */
445  self->crc = crc32(0, CUS "IDAT" "\x78\001", 6);
446  self->adler = 1 /* adler32(0, Z_NULL, 0) */;
447 }
448 
449 static void png4file_flush(Png4File * self)
450 {
451  int l = self->bufpos - 5;
452  self->chunklen += self->bufpos;
453  set_uint16_le(self->buffer + 1, l);
454  set_uint16_le(self->buffer + 3, l ^ 0xffff);
455  xxfwrite(self->fh, self->buffer, self->bufpos);
456  self->crc = crc32(self->crc, self->buffer, self->bufpos);
457  self->adler = adler32(self->adler, self->buffer + 5, self->bufpos - 5);
458  self->buffer[0] = '\0';
459  self->bufpos = 5;
460 }
461 
462 static void png4file_addpixel(Png4File * self, eu8 pixel)
463 {
464  if (self->nibble < 0)
465  self->nibble = (pixel << 4);
466  else
467  {
468  self->buffer[self->bufpos++] = self->nibble | pixel;
469  self->nibble = -1;
470  if (self->bufpos == sizeof self->buffer - 4)
471  png4file_flush(self);
472  }
473 }
474 
475 static void png4file_endrow(Png4File * self)
476 {
477  if (self->nibble >= 0)
478  {
479  self->buffer[self->bufpos++] = self->nibble;
480  self->nibble = -1;
481  }
482 
483  self->hleft--;
484  if (self->hleft)
485  self->buffer[self->bufpos++] = '\0';
486 }
487 
488 static void png4file_close(Png4File * self)
489 {
490  eu8 adlerbuf[4];
491  self->buffer[0] = 0x01;
492  if (self->bufpos)
493  png4file_flush(self);
494  else
495  {
496  self->bufpos = 5;
497  png4file_flush(self);
498  }
499 
500  set_uint32_be(adlerbuf, self->adler);
501  xxfwrite(self->fh, adlerbuf, 4);
502  self->crc = crc32(self->crc, adlerbuf, 4);
503  xxfwrite_uint32_be(self->fh, self->crc);
504  xxfwriteCS(self->fh, "\0\0\0\0" "IEND" "\xae\x42\x60\x82");
505  xfseek0(self->fh, 70);
506  xxfwrite_uint32_be(self->fh, self->chunklen + 4 /* adler*/);
507  fclose(self->fh);
508  self->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, cindex;
530  /* Originally from gtkspu - this from the python implementation of this */
531 
532  while (1)
533  {
534  int bits = getnibble(data, &nibble);
535  if ((bits & 0xc) != 0)
536  {
537  /* have 4-bit code */
538  number = (bits & 0xc) >> 2;
539  cindex = bits & 0x3; }
540  else
541  {
542  bits = (bits << 4) | getnibble(data, &nibble);
543  if ((bits & 0xf0) != 0)
544  {
545  /* have 8-bit code */
546  number = (bits & 0x3c) >> 2;
547  cindex = bits & 0x3;
548  }
549  else
550  {
551  bits = (bits << 4) | getnibble(data, &nibble);
552  if ((bits & 0xfc0) != 0)
553  {
554  /* have 12-bit code */
555  number = (bits & 0xfc) >> 2;
556  cindex = bits & 0x3;
557  }
558  else
559  {
560  /* have 16-bit code */
561  bits = (bits << 4) | getnibble(data, &nibble);
562  number = (bits & 0x3fc) >> 2;
563  cindex = bits & 0x3;
564 
565  if (number == 0)
566  number = width;
567  }
568  }
569  }
570 
571  /* printf("%d %d %d %d\n", number, col, width, cindex); */
572  for (; number > 0 && col < width; number--, col++)
573  png4file_addpixel(picfile, cindex);
574 
575  if (col == width)
576  {
577  png4file_endrow(picfile);
578  return;
579  }
580  }
581 }
582 
583 static void makebitmap(eu8 * data, int w, int h, int top, int bot,
584  char * filename, eu8 palette[4][3])
585 {
586  eu8 * top_ibuf = data + top;
587  eu8 * bot_ibuf = data + bot;
588  int i;
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 (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  int h = pts / (3600 * 90000);
606  int m = pts / (60 * 90000) % 60;
607  int s = pts / 90000 % 60;
608  int 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 struct _BoundStr
621 {
622  eu8 * p;
623  int l;
624 };
625 
626 static void boundstr_init(BoundStr * bs, eu8 * p, int l)
627 {
628  bs->p = p;
629  bs->l = l;
630 }
631 
632 static eu8 * boundstr_read(BoundStr * bs, int l)
633 {
634  eu8 * rp;
635  if (l > bs->l)
636  exc_throw(IndexError, "XXX IndexError %p.", bs);
637  rp = bs->p;
638  bs->p += l;
639  bs->l -= l;
640  return rp;
641 }
642 
643 /* *********** */
644 
645 
646 static void pxsubtitle(const char * supfile, FILE * ofh, eu8 palette[16][3],
647  bool createpics, int delay_ms,
648  char * fnbuf, char * fnbuf_fp)
649 {
650  char junk[32];
651  char sptsstr[32];
652  eu8 data[65536];
653  eu8 * ctrl;
654  time_t volatile pt = 0;
655  bool volatile last = false;
656  /*char transparent[8]; */
657  FILE * sfh = xfopen(supfile, "rb");
658  if (memcmp(xxfread(sfh, data, 2), "SP", 2) != 0)
659  {
660  exc_throw(MiscError, "Syncword missing. XXX bailing out.");
661  }
662 
663  /*sprintf(transparent,
664  "%02x%02x%02x", palette[0][0], palette[0][1], palette[0][2]); */
665 
666  exc_try
667  {
668  eu32 volatile lastendpts = 0;
669 
670  while (1)
671  {
672  /* X.java reads 5 bytes of pts, SubPicture.java writes 4. With
673  * 4 bytes 47721 seconds (13.25 hours) can be handled.
674  * Later, bytes 1,2,3,4 (instead of 0,1,2,3) could be used.
675  * 256/90000 = 1/351 sec -- way better than required resolution.
676  */
677  eu32 volatile pts = get_uint32_le(xxfread(sfh, data, 8));
678  eu16 size = get_uint16_be(xxfread(sfh, data, 2));
679  eu16 pack = get_uint16_be(xxfread(sfh, data, 2));
680  eu32 endpts;
681  xxfread(sfh, data, pack - 4);
682  ctrl = data + pack - 4;
683  xxfread(sfh, ctrl, size - pack);
684 
685  exc_try
686  {
687  if (memcmp(xxfread(sfh, (unsigned char *)junk, 2),
688  "SP", 2) != 0)
689  exc_throw(MiscError, "Syncword missing. XXX bailing out.");
690  }
692  last = true;
693  exc_end;
694  {
695  time_t ct;
696  BoundStr bs;
697  int prev;
698  int x1 = -1, x2 = -1, y1 = -1, y2 = -1;
699  int top_field = -1, bot_field = -1;
700  int end = 0;
701  int colcon_length;
702  eu8 this_palette[4][3];
703  boundstr_init(&bs, ctrl, size - pack);
704 
705  prev = 0;
706  while (1)
707  {
708  int date = get_uint16_be(boundstr_read(&bs, 2));
709  int next = get_uint16_be(boundstr_read(&bs, 2));
710 
711  while (1)
712  {
713  eu8 * p;
714  eu8 cmd = boundstr_read(&bs, 1)[0];
715  int xpalette, i, n;
716  switch (cmd)
717  {
718  case 0x00: /* force display: */
719  continue;
720  case 0x01: /* start date (read above) */
721  continue;
722  case 0x02: /* stop date (read above) */
723  end = date;
724  continue;
725  case 0x03: /* palette */
726  xpalette = get_uint16_be(boundstr_read(&bs, 2));
727  for (n = 0; n < 4; n++) {
728  i = (xpalette >> (n * 4) & 0x0f);
729  this_palette[n][0] = palette[i][0];
730  this_palette[n][1] = palette[i][1];
731  this_palette[n][2] = palette[i][2];
732  }
733  continue;
734  case 0x04: /* alpha channel */
735  /*alpha =*/ boundstr_read(&bs, 2);
736  continue;
737  case 0x05: /* coordinates */
738  p = boundstr_read(&bs, 6);
739  x1 = (p[0] << 4) + (p[1] >> 4);
740  x2 = ((p[1] & 0xf) << 8) + p[2];
741  y1 = (p[3] << 4) + (p[4] >> 4);
742  y2 = ((p[4] & 0xf) << 8) + p[5];
743  continue;
744  case 0x06: /* rle offsets */
745  top_field = get_uint16_be(boundstr_read(&bs, 2));
746  bot_field = get_uint16_be(boundstr_read(&bs, 2));
747  continue;
748  case 0x07: /* */
749  colcon_length = get_uint16_be(boundstr_read(&bs, 2)-2);
750  boundstr_read(&bs, colcon_length);
751  continue;
752  }
753  if (cmd == 0xff) /* end command */
754  break;
755  exc_throw(MiscError, "%d: Unknown control sequence", cmd);
756  }
757 
758  if (prev == next)
759  break;
760  prev = next;
761  }
762 
763  /* check for overlapping subtitles */
764  eu32 tmppts = pts;
765  if (delay_ms)
766  tmppts += delay_ms * 90;
767 
768  /* hack to fix projectx bug adding wrong end times around a cut point*/
769  if (end > 500)
770  end = 500;
771 
772  endpts = tmppts + end * 1000; /* ProjectX ! (other: 900, 1024) */
773 
774  if (tmppts <= lastendpts)
775  {
776  if (lastendpts < endpts)
777  {
778  pts = lastendpts + 2 * (1000 / 30) * 90;/*??? */
779  tmppts = pts;
780  if (delay_ms)
781  tmppts += delay_ms * 90;
782  printf("Fixed overlapping subtitle!\n");
783  }
784  else
785  {
786  printf("Dropping overlapping subtitle\n");
787  continue;
788  }
789  }
790 
791  lastendpts = endpts;
792 
793  pts2ts(pts, fnbuf_fp, true);
794  pts2ts(tmppts, sptsstr + 1, false);
795 
796  if (pt != time(&ct))
797  {
798  pt = ct;
799  sptsstr[0] = '\r';
800  size_t len = write(1, sptsstr, strlen(sptsstr));
801  if (len != strlen(sptsstr))
802  printf("ERROR: write failed");
803  }
804 
805  fprintf(ofh, " <spu start=\"%s\" end=\"%s\" image=\"%s\""
806  " xoffset=\"%d\" yoffset=\"%d\" />\n",
807  sptsstr + 1, pts2ts(endpts, junk, false),
808  fnbuf, x1, y1);
809 
810  if (createpics)
811  makebitmap(data, x2 - x1 + 1, y2 - y1 + 1, top_field - 4,
812  bot_field - 4, fnbuf, this_palette);
813  if (last)
815  }
816  }
817  }
819  {
820  size_t len = write(1, sptsstr, strlen(sptsstr));
821  if (len != strlen(sptsstr))
822  printf("ERROR: write failed");
823  return;
824  }
825  exc_end;
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  FILE * fh;
858  int i;
859  unsigned int r,g,b;
860 
861  if (!fexists(filename))
862  return false;
863 
864  fh = xfopen(filename, "rb");
865  for (i = 0; i < 16; i++)
866  {
867  if (fscanf(fh, "%02x%02x%02x\n", &r, &g, &b) != 3 ||
868  r != palette[i][0] || g != palette[i][1] || b != palette[i][2])
869  {
870  fclose(fh);
871  return false;
872  }
873  }
874  fclose(fh);
875  return true;
876 }
877 
878 int sup2dast(const char *supfile, const char *ifofile ,int delay_ms)
879 {
880  exc_try
881  {
882  int i;
883  eu8 yuvpalette[16][3], rgbpalette[16][3];
884  char fnbuf[1024];
885  char * p;
886 
887  bool createpics;
888  FILE * fh;
889 
890  memset(yuvpalette, 0, sizeof(yuvpalette));
891  memset(rgbpalette, 0, sizeof(rgbpalette));
892 
893  if (write(1, "\n", 1) != 1)
894  printf("ERROR: write failed");
895 
896  if (sizeof (char) != 1 || sizeof (int) < 2) /* very unlikely */
897  exc_throw(MiscError, "Incompatible variable sizes.");
898 
899  if ((p = strrchr(supfile, '.')) != NULL)
900  i = p - supfile;
901  else
902  i = strlen(supfile);
903  if (i > 950)
904  exc_throw(MiscError, "Can "
905  "not manage filenames longer than 950 characters.");
906  memcpy(fnbuf, supfile, i);
907  p = fnbuf + i;
908  strcpy(p, ".d/");
909  p += strlen(p);
910 
911  if (!dexists(fnbuf))
912  xmkdir(fnbuf, 0755);
913 
914  if (fexists(ifofile))
915  ifopalette(ifofile, yuvpalette, rgbpalette);
916  else
917  argpalette(ifofile, yuvpalette, rgbpalette);
918 
919  strcpy(p, "palette.ycrcb");
920  if (samepalette(fnbuf, yuvpalette))
921  {
922  createpics = false;
923  printf("Found palette.yuv having our palette information...\n");
924  printf("...Skipping image files, Writing spumux.tmp.\n");
925  }
926  else
927  {
928  createpics = true;
929  printf("Writing image files and spumux.tmp.\n");
930  }
931 
932  strcpy(p, "spumux.tmp");
933  fh = xfopen(fnbuf, "wb");
934 
935  xxfwriteCS(fh, "<subpictures>\n <stream>\n");
936  pxsubtitle(supfile, fh, rgbpalette, createpics, delay_ms, fnbuf, p);
937 
938  if (write(1, "\n", 1) != 1)
939  printf("ERROR: write failed");
940 
941  xxfwriteCS(fh, " </stream>\n</subpictures>\n");
942  fclose(fh);
943 
944  if (createpics)
945  {
946  FILE * yuvfh, *rgbfh;
947  printf("Writing palette.ycrcb and palette.rgb.\n");
948  strcpy(p, "palette.ycrcb");
949  yuvfh = xfopen(fnbuf, "wb");
950  strcpy(p, "palette.rgb");
951  rgbfh = xfopen(fnbuf, "wb");
952  for (i = 0; i < 16; i++)
953  {
954  fprintf(yuvfh, "%02x%02x%02x\n",
955  yuvpalette[i][0], yuvpalette[i][1], yuvpalette[i][2]);
956  fprintf(rgbfh, "%02x%02x%02x\n",
957  rgbpalette[i][0], rgbpalette[i][1], rgbpalette[i][2]);
958  }
959  fclose(yuvfh);
960  fclose(rgbfh);
961  }
962 
963  {
964  char buf[1024];
965  printf("Renaming spumux.tmp to spumux.xml.\n");
966  strcpy(p, "spumux.tmp");
967  i = strlen(fnbuf);
968  memcpy(buf, fnbuf, i);
969  strcpy(&buf[i - 3], "xml");
970  unlink(buf);
971  rename(fnbuf, buf);
972  }
973  p[0] = '\0';
974  printf("Output files reside in %s\n", fnbuf);
975  printf("All done.\n");
976  }
977 
978  exc_catchall
979  {
980  if (write(2, EXC.msgbuf, EXC.buflen) != EXC.buflen)
981  printf("ERROR: write failed");
982 
983  if (write(2, "\n", 1) != 1)
984  printf("ERROR: write failed");
985 
986  return 1;
987  }
988  exc_endall;
989 
990  return 0;
991 }
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
struct exc__state * prev
Definition: pxsup2dast.c:92
uint32_t eu32
Definition: pxsup2dast.c:53
struct exc__state * last
Definition: pxsup2dast.c:98
eu32 crc
Definition: pxsup2dast.c:400
static eu8 * boundstr_read(BoundStr *bs, int l)
Definition: pxsup2dast.c:632
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
#define exc_end
Definition: pxsup2dast.c:112
static void png4file_close(Png4File *self)
Definition: pxsup2dast.c:488
static void boundstr_init(BoundStr *bs, eu8 *p, int l)
Definition: pxsup2dast.c:626
int buflen
Definition: pxsup2dast.c:100
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
eu8 buffer[65536]
Definition: pxsup2dast.c:403
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
static void xxfwrite(FILE *stream, const eu8 *ptr, size_t size)
Definition: pxsup2dast.c:187
static eu8 getnibble(eu8 **data, int *nibble)
Definition: pxsup2dast.c:511
#define exc_catchall
Definition: pxsup2dast.c:118
int32_t ei32
Definition: pxsup2dast.c:53
int_fast16_t fi16
Definition: pxsup2dast.c:60
int16_t ei16
Definition: pxsup2dast.c:52
static void set_uint32_be(eu8 *ptr, eu32 value)
Definition: pxsup2dast.c:304
unsigned char r
Definition: ParseText.cpp:340
unsigned char b
Definition: ParseText.cpp:340
static void png4file_init(Png4File *self, eu8 palette[4][3])
Definition: pxsup2dast.c:406
static void png4file_flush(Png4File *self)
Definition: pxsup2dast.c:449
jmp_buf env
Definition: pxsup2dast.c:93
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
int chunklen
Definition: pxsup2dast.c:399
uint_fast16_t fu16
Definition: pxsup2dast.c:60
FILE * fh
Definition: pxsup2dast.c:394
static int x2
Definition: mythsocket.cpp:61
static char * pts2ts(fu32 pts, char *rvbuf, bool is_png_filename)
Definition: pxsup2dast.c:603
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:646
static void xfseek0(FILE *stream, long offset)
Definition: pxsup2dast.c:251
uint16_t eu16
Definition: pxsup2dast.c:52
int_least16_t li16
Definition: pxsup2dast.c:56
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:200
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:583
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:360
static bool samepalette(char *filename, eu8 palette[16][3])
Definition: pxsup2dast.c:855
static bool fexists(const char *filename)
Definition: pxsup2dast.c:264
int FILE
Definition: mythburn.py:110
int_least32_t li32
Definition: pxsup2dast.c:57
char msgbuf[1024]
Definition: pxsup2dast.c:99
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:129
static void getpixelline(eu8 **data, int width, Png4File *picfile)
Definition: pxsup2dast.c:525
eu8 * p
Definition: pxsup2dast.c:622
#define xxfwriteCS(f, s)
Definition: pxsup2dast.c:198
PictureAttribute next(PictureAttributeSupported supported, PictureAttribute attribute)
static void __exc_throw(int type)
Definition: pxsup2dast.c:121
eu8 palettechunk[24]
Definition: pxsup2dast.c:402
static void xxfwrite_uint32_be(FILE *fh, eu32 value)
Definition: pxsup2dast.c:322
struct @83 EXC
int bufpos
Definition: pxsup2dast.c:398
static guint32 * pixel
--------------------------------------------------—**
Definition: goom_core.c:33
eu32 adler
Definition: pxsup2dast.c:401
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:878
#define exc_catch(t)
Definition: pxsup2dast.c:110
static void png4file_endrow(Png4File *self)
Definition: pxsup2dast.c:475
int nibble
Definition: pxsup2dast.c:397
#define exc_endall
Definition: pxsup2dast.c:119
static eu8 * xxfread(FILE *stream, eu8 *ptr, size_t size)
Definition: pxsup2dast.c:176
int_fast32_t fi32
Definition: pxsup2dast.c:61
static uint32_t crc32(const unsigned char *data, int len)
Definition: dsmcc.cpp:632
#define exc_try
Definition: pxsup2dast.c:103
uint_fast8_t fu8
Definition: pxsup2dast.c:59
static void png4file_addpixel(Png4File *self, eu8 pixel)
Definition: pxsup2dast.c:462
static int x1
Definition: mythsocket.cpp:60
unsigned char g
Definition: ParseText.cpp:340