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 
81 
82 int sup2dast(const char *supfile, const char *ifofile, int delay_ms);
83 
84 /****** Poor man's exception code ... (heavily inspired by cexcept). ******/
85 
86 struct exc__state
87 {
88  struct exc__state *m_prev;
89  jmp_buf m_env;
90 };
91 
92 struct
93 {
94  struct exc__state *m_last;
95  char m_msgbuf[1024];
96  int m_buflen;
97 } EXC /* = { 0 }*/ ;
98 
99 // These macros now all take a parameter 'x' to specify a recursion
100 // level. This will then generate unique variable names for each
101 // level of recursion, preventing the compiler from complaining about
102 // shadowed variables.
103 #define exc_try(x) do { struct exc__state exc_s##x; int exc_type##x GCCATTR_UNUSED; \
104  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)
105 
106 #define exc_ftry(x) do { struct exc__state exc_s##x, *exc_p##x = EXC.m_last; \
107  int exc_type##x GCCATTR_UNUSED; exc_s##x.prev = EXC.m_last; \
108  EXC.m_last = &exc_s##x; if ((exc_type##x = setjmp(exc_s##x.env)) == 0)
109 
110 #define exc_catch(x,t) else if ((t) == exc_type##x)
111 
112 #define exc_end(x) else __exc_throw(exc_type##x); EXC.m_last = exc_s##x.m_prev; } while (0)
113 
114 #define exc_return(x) for (EXC.m_last = exc_p##x;) return
115 
116 #define exc_fthrow(x) for (EXC.m_last = exc_p##x;) ex_throw
117 
118 #define exc_catchall else
119 #define exc_endall(x) EXC.m_last = exc_s##x.m_prev; } while (0)
120 
121 #define exc_cleanup() EXC.m_last = NULL;
122 
123 static void __exc_throw(int type) /* protoadd GCCATTR_NORETURN */
124 {
125  struct exc__state * exc_s = EXC.m_last;
126  EXC.m_last = EXC.m_last->m_prev;
127  longjmp(exc_s->m_env, type);
128 }
129 
130 static void exc_throw(int type, const char * format, ...)
131  /* protoadd GCCATTR_NORETURN */
132 {
133  if (format != NULL)
134  {
135  va_list ap;
136  int err = errno;
137 
138  va_start(ap, format);
139  unsigned int len = vsnprintf(EXC.m_msgbuf, sizeof EXC.m_msgbuf, format, ap);
140  va_end(ap);
141 
142  if (len >= sizeof EXC.m_msgbuf)
143  {
144  len = sizeof EXC.m_msgbuf - 1;
145  EXC.m_msgbuf[len] = '\0';
146  }
147  else
148  {
149  if (format[strlen(format) - 1] == ':')
150  {
151  int l = snprintf(&EXC.m_msgbuf[len], sizeof EXC.m_msgbuf - len,
152  " %s.", strerror(err));
153  if (l + len >= sizeof EXC.m_msgbuf)
154  {
155  len = sizeof EXC.m_msgbuf - 1;
156  EXC.m_msgbuf[len] = '\0';
157  }
158  else
159  len += l;
160  }
161  }
162  EXC.m_buflen = len;
163  }
164  else
165  {
166  EXC.m_msgbuf[0] = '\0';
167  EXC.m_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  /* from dvdauthor... */
204  int lr = (500 + 1164 * (y - 16) + 1596 * (cr - 128) ) /1000;
205  int lg = (500 + 1164 * (y - 16) - 813 * (cr - 128) - 391 * (cb - 128)) / 1000;
206  int lb = (500 + 1164 * (y - 16) + 2018 * (cb - 128)) / 1000;
207 
208  *r = (lr < 0)? 0: (lr > 255)? 255: (eu8)lr;
209  *g = (lg < 0)? 0: (lg > 255)? 255: (eu8)lg;
210  *b = (lb < 0)? 0: (lb > 255)? 255: (eu8)lb;
211 }
212 
213 static void rgb2yuv(eu8 r, eu8 g, eu8 b,
214  eu8 * y, eu8 * cr, eu8 * cb)
215 {
216  /* int ly, lcr, lcb; */
217 
218  /* from dvdauthor... */
219  *y = ( 257 * r + 504 * g + 98 * b + 16500) / 1000;
220  *cr = ( 439 * r - 368 * g - 71 * b + 128500) / 1000;
221  *cb = (-148 * r - 291 * g + 439 * b + 128500) / 1000;
222 }
223 
224 /* the code above matches nicely with http://www.fourcc.org/fccyvrgb.php
225 
226  * RGB to YUV Conversion
227 
228  * Y = (0.257 * R) + (0.504 * G) + (0.098 * B) + 16
229  * Cr = V = (0.439 * R) - (0.368 * G) - (0.071 * B) + 128
230  * Cb = U = -(0.148 * R) - (0.291 * G) + (0.439 * B) + 128
231 
232  * YUV to RGB Conversion
233 
234  * B = 1.164(Y - 16) + 2.018(U - 128)
235  * G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128)
236  * R = 1.164(Y - 16) + 1.596(V - 128)
237 
238 */
239 
240 
241 static FILE * xfopen(const char * filename, const char * mode)
242 {
243  FILE * fh = fopen(filename, mode);
244  if (fh == NULL)
245  exc_throw(MiscError, "fopen(\"%s\", \"%s\") failure:", filename, mode);
246  return fh;
247 }
248 
249 static void xfseek0(FILE * stream, long offset)
250 {
251  if (fseek(stream, offset, SEEK_SET) < 0)
252  exc_throw(MiscError, "fseek(stream, %ld, SEEK_SET) failure:",offset);
253 }
254 
255 static void xmkdir(const char * path, int mode)
256 {
257  if (mkdir(path, mode) < 0)
258  exc_throw(MiscError, "mkdir(%s, 0%o) failure:", path, mode);
259 }
260 
261 
262 static bool fexists(const char * filename)
263 {
264  struct stat st;
265 
266  if (stat(filename, &st) == 0 && S_ISREG(st.st_mode))
267  return true;
268  return false;
269 }
270 
271 static bool dexists(const char * filename)
272 {
273  struct stat st;
274 
275  if (stat(filename, &st) == 0 && S_ISDIR(st.st_mode))
276  return true;
277 
278  return false;
279 }
280 
281 static fu32 get_uint32_be(const eu8 * bytes)
282 {
283  return (bytes[0] << 24) + (bytes[1] << 16) + (bytes[2] << 8) + bytes[3];
284 }
285 
286 static fu16 get_uint16_be(const eu8 * bytes)
287 {
288  return (bytes[0] << 8) + bytes[1];
289 }
290 
291 static fu32 get_uint32_le(const eu8 * bytes)
292 {
293  return (bytes[3] << 24) + (bytes[2] << 16) + (bytes[1] << 8) + bytes[0];
294 }
295 
296 #if 0 /* protoline */
297 static fu32 get_uint16_le(const eu8 * bytes) {
298  return (bytes[1] << 8) + bytes[0]; }
299 #endif /* protoline */
300 
301 
302 static void set_uint32_be(eu8 * ptr, eu32 value)
303 {
304  ptr[0] = value>>24; ptr[1] = value>>16; ptr[2] = value>>8; ptr[3] =value;
305 }
306 
307 #if 0 /* protoline */
308 static void set_uint16_be(eu8 * ptr, eu32 value) {
309  ptr[0] = value>>8; ptr[1] = value; }
310 
311 static void set_uint32_le(eu8 * ptr, eu32 value) {
312  ptr[3] = value>>24; ptr[2] = value>>16; ptr[1] = value>>8; ptr[0] =value; }
313 #endif /* protoline */
314 
315 static void set_uint16_le(eu8 * ptr, eu16 value)
316 {
317  ptr[1] = value>>8; ptr[0] =value;
318 }
319 
320 static void xxfwrite_uint32_be(FILE * fh, eu32 value)
321 {
322  eu8 buf[4];
323  set_uint32_be(buf, value);
324  xxfwrite(fh, buf, 4);
325 }
326 
327 static void ifopalette(const char * filename,
328  eu8 yuvpalette[16][3], eu8 rgbpalette[16][3])
329 {
330  eu8 buf[1024];
331 
332  FILE *fh = xfopen(filename, "rb");
333  if (memcmp(xxfread(fh, buf, 12), "DVDVIDEO-VTS", 12) != 0)
335  "(IFO) file %s not of type DVDVIDEO-VTS.", filename);
336 
337  xfseek0(fh, 0xcc);
338  fu32 offset = get_uint32_be(xxfread(fh, buf, 4));
339  xfseek0(fh, offset * 0x800 + 12);
340  fu32 pgc = offset * 0x800 + get_uint32_be(xxfread(fh, buf, 4));
341  /* seek to palette */
342  xfseek0(fh, pgc + 0xa4);
343  xxfread(fh, buf, 16 * 4);
344  fclose(fh);
345  for (int i = 0; i < 16; i++)
346  {
347  eu8 r = 0;
348  eu8 g = 0;
349  eu8 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;
361  eu8 cr = 0;
362  eu8 cb = 0;
363  eu8 r = value >> 16;
364  eu8 g = value >> 8;
365  eu8 b = value;
366  rgbpalpart[0] = r, rgbpalpart[1] = g, rgbpalpart[2] = b;
367  rgb2yuv(r, g, b, &y, &cr, &cb);
368  yuvpalpart[0] = y, yuvpalpart[1] = cr, yuvpalpart[2] = cb;
369 }
370 
371 
372 static void argpalette(const char * arg,
373  eu8 yuvpalette[16][3], eu8 rgbpalette[16][3])
374 {
375  unsigned int i = 0;
376 
377  if (strlen(arg) != 20 || arg[6] != ',' || arg[13] != ',')
378  exc_throw(MiscError, "Palette arg %s invalid.\n", arg);
379 
380  for (i = 0; i < 16; i++)
381  set2palettes(i * 0x111111, yuvpalette[i], rgbpalette[i]);
382 
383  sscanf(arg, "%x", &i); /* "%x," ? */
384  set2palettes(i, yuvpalette[1], rgbpalette[1]);
385  sscanf(arg + 7, "%x", &i);
386  set2palettes(i, yuvpalette[2], rgbpalette[2]);
387  sscanf(arg + 14, "%x", &i);
388  set2palettes(i, yuvpalette[3], rgbpalette[3]);
389 }
390 
391 
392 typedef 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 } Png4File;
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;
529  int cindex = 0;
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  Png4File picfile;
589 
590  png4file_init(&picfile, palette); /* not bottleneck even re-doing this every time */
591  png4file_open(&picfile, filename, h, w);
592  for (int i = 0; i < h / 2; i++)
593  {
594  getpixelline(&top_ibuf, w, &picfile);
595  getpixelline(&bot_ibuf, w, &picfile);
596  }
597 
598  png4file_close(&picfile);
599 }
600 
601 
602 static char * pts2ts(fu32 pts, char * rvbuf, bool is_png_filename)
603 {
604  int h = pts / (3600 * 90000);
605  int m = pts / (60 * 90000) % 60;
606  int s = pts / 90000 % 60;
607  int hs = (pts + 450) / 900 % 100;
608 
609  if (is_png_filename)
610  sprintf(rvbuf, "%02d+%02d+%02d.%02d.png", h, m, s, hs);
611  else
612  sprintf(rvbuf, "%02d:%02d:%02d.%02d", h, m, s, hs);
613 
614  return rvbuf;
615 }
616 
617 /* *********** */
618 
619 typedef struct BoundStr
620 {
622  int m_l;
623 } BoundStr;
624 
625 static void boundstr_init(BoundStr * bs, eu8 * p, int l)
626 {
627  bs->m_p = p;
628  bs->m_l = l;
629 }
630 
631 static eu8 * boundstr_read(BoundStr * bs, int l)
632 {
633  if (l > bs->m_l)
634  exc_throw(IndexError, "XXX IndexError %p.", bs);
635  eu8 * rp = bs->m_p;
636  bs->m_p += l;
637  bs->m_l -= l;
638  return rp;
639 }
640 
641 /* *********** */
642 
643 
644 static void pxsubtitle(const char * supfile, FILE * ofh, eu8 palette[16][3],
645  bool createpics, int delay_ms,
646  char * fnbuf, char * fnbuf_fp)
647 {
648  char junk[32];
649  char sptsstr[32];
650  eu8 data[65536];
651  time_t volatile pt = 0;
652  bool volatile last = false;
653  /*char transparent[8]; */
654  FILE * sfh = xfopen(supfile, "rb");
655  if (memcmp(xxfread(sfh, data, 2), "SP", 2) != 0)
656  {
657  exc_throw(MiscError, "Syncword missing. XXX bailing out.");
658  }
659 
660  /*sprintf(transparent,
661  "%02x%02x%02x", palette[0][0], palette[0][1], palette[0][2]); */
662 
663  exc_try(1)
664  {
665  eu32 volatile lastendpts = 0;
666 
667  while (1)
668  {
669  /* X.java reads 5 bytes of pts, SubPicture.java writes 4. With
670  * 4 bytes 47721 seconds (13.25 hours) can be handled.
671  * Later, bytes 1,2,3,4 (instead of 0,1,2,3) could be used.
672  * 256/90000 = 1/351 sec -- way better than required resolution.
673  */
674  eu32 volatile pts = get_uint32_le(xxfread(sfh, data, 8));
675  eu16 size = get_uint16_be(xxfread(sfh, data, 2));
676  eu16 pack = get_uint16_be(xxfread(sfh, data, 2));
677  xxfread(sfh, data, pack - 4);
678  eu8 * ctrl = data + pack - 4;
679  xxfread(sfh, ctrl, size - pack);
680 
681  exc_try(2)
682  {
683  if (memcmp(xxfread(sfh, (unsigned char *)junk, 2),
684  "SP", 2) != 0)
685  exc_throw(MiscError, "Syncword missing. XXX bailing out.");
686  }
688  last = true;
689  exc_end(2);
690  {
691  BoundStr bs;
692  int x1 = -1;
693  int x2 = -1;
694  int y1 = -1;
695  int y2 = -1;
696  int top_field = -1;
697  int bot_field = -1;
698  int end = 0;
699  eu8 this_palette[4][3];
700  boundstr_init(&bs, ctrl, size - pack);
701 
702  int prev = 0;
703  while (1)
704  {
705  int date = get_uint16_be(boundstr_read(&bs, 2));
706  int next = get_uint16_be(boundstr_read(&bs, 2));
707 
708  while (1)
709  {
710  eu8 * p = 0;
711  eu8 cmd = boundstr_read(&bs, 1)[0];
712  int xpalette = 0;
713  int colcon_length = 0;
714  switch (cmd)
715  {
716  case 0x00: /* force display: */
717  case 0x01: /* start date (read above) */
718  continue;
719  case 0x02: /* stop date (read above) */
720  end = date;
721  continue;
722  case 0x03: /* palette */
723  xpalette = get_uint16_be(boundstr_read(&bs, 2));
724  for (int n = 0; n < 4; n++) {
725  int i = (xpalette >> (n * 4) & 0x0f);
726  this_palette[n][0] = palette[i][0];
727  this_palette[n][1] = palette[i][1];
728  this_palette[n][2] = palette[i][2];
729  }
730  continue;
731  case 0x04: /* alpha channel */
732  /*alpha =*/ boundstr_read(&bs, 2);
733  continue;
734  case 0x05: /* coordinates */
735  p = boundstr_read(&bs, 6);
736  x1 = (p[0] << 4) + (p[1] >> 4);
737  x2 = ((p[1] & 0xf) << 8) + p[2];
738  y1 = (p[3] << 4) + (p[4] >> 4);
739  y2 = ((p[4] & 0xf) << 8) + p[5];
740  continue;
741  case 0x06: /* rle offsets */
742  top_field = get_uint16_be(boundstr_read(&bs, 2));
743  bot_field = get_uint16_be(boundstr_read(&bs, 2));
744  continue;
745  case 0x07: /* */
746  colcon_length = get_uint16_be(boundstr_read(&bs, 2)-2);
747  boundstr_read(&bs, colcon_length);
748  continue;
749  }
750  if (cmd == 0xff) /* end command */
751  break;
752  exc_throw(MiscError, "%d: Unknown control sequence", cmd);
753  }
754 
755  if (prev == next)
756  break;
757  prev = next;
758  }
759 
760  /* check for overlapping subtitles */
761  eu32 tmppts = pts;
762  if (delay_ms)
763  tmppts += delay_ms * 90;
764 
765  /* hack to fix projectx bug adding wrong end times around a cut point*/
766  if (end > 500)
767  end = 500;
768 
769  eu32 endpts = tmppts + end * 1000; /* ProjectX ! (other: 900, 1024) */
770 
771  if (tmppts <= lastendpts)
772  {
773  if (lastendpts < endpts)
774  {
775  pts = lastendpts + 2 * (1000 / 30) * 90;/*??? */
776  tmppts = pts;
777  if (delay_ms)
778  tmppts += delay_ms * 90;
779  printf("Fixed overlapping subtitle!\n");
780  }
781  else
782  {
783  printf("Dropping overlapping subtitle\n");
784  continue;
785  }
786  }
787 
788  lastendpts = endpts;
789 
790  pts2ts(pts, fnbuf_fp, true);
791  pts2ts(tmppts, sptsstr + 1, false);
792 
793  time_t ct = 0;
794  if (pt != time(&ct))
795  {
796  pt = ct;
797  sptsstr[0] = '\r';
798  size_t len = write(1, sptsstr, strlen(sptsstr));
799  if (len != strlen(sptsstr))
800  printf("ERROR: write failed");
801  }
802 
803  fprintf(ofh, " <spu start=\"%s\" end=\"%s\" image=\"%s\""
804  " xoffset=\"%d\" yoffset=\"%d\" />\n",
805  sptsstr + 1, pts2ts(endpts, junk, false),
806  fnbuf, x1, y1);
807 
808  if (createpics)
809  makebitmap(data, x2 - x1 + 1, y2 - y1 + 1, top_field - 4,
810  bot_field - 4, fnbuf, this_palette);
811  if (last)
813  }
814  }
815  }
817  {
818  size_t len = write(1, sptsstr, strlen(sptsstr));
819  if (len != strlen(sptsstr))
820  printf("ERROR: write failed");
821  exc_cleanup();
822  return;
823  }
824  exc_end(1);
825 }
826 
827 #if 0
828 static void usage(const char * pn) /* protoadd GCCATTR_NORETURN */
829 {
830  exc_throw(MiscError, "\n"
831  "Usage: %s [--delay ms] <supfile> <ifofile>|<palette>" "\n"
832  "\n"
833  "\tExamples:" "\n"
834  "\n"
835  "\t ProjectX decoded recording.sup and recording.sup.IFO" "\n"
836  "\n"
837  "\t\t$ pxsup2dast recording.sup*" "\n"
838  "\n"
839  "\t Having test.sup and map.ifo" "\n"
840  "\n"
841  "\t\t$ pxsup2dast test.sup map.ifo" "\n"
842  "\n"
843  "\t No .IFO, so giving 3 colors (rgb components in hex)" "\n"
844  "\n"
845  "\t\t$ pxsup2dast titles.sup ff0000,00ff00,0000ff" "\n"
846  "\n"
847  "\t Trying to fix sync in recording" "\n"
848  "\n"
849  "\t\t$ pxsup2dast --delay 750 recording.sup*" "\n"
850  , pn);
851 }
852 #endif
853 
854 static bool samepalette(char * filename, eu8 palette[16][3])
855 {
856  if (!fexists(filename))
857  return false;
858 
859  FILE *fh = xfopen(filename, "rb");
860  for (int i = 0; i < 16; i++)
861  {
862  unsigned int r=0;
863  unsigned int g=0;
864  unsigned int b=0;
865  if (fscanf(fh, "%02x%02x%02x\n", &r, &g, &b) != 3 ||
866  r != palette[i][0] || g != palette[i][1] || b != palette[i][2])
867  {
868  fclose(fh);
869  return false;
870  }
871  }
872  fclose(fh);
873  return true;
874 }
875 
876 int sup2dast(const char *supfile, const char *ifofile ,int delay_ms)
877 {
878  exc_try(1)
879  {
880  int i = 0;
881  eu8 yuvpalette[16][3];
882  eu8 rgbpalette[16][3];
883  char fnbuf[1024];
884  char * p = NULL;
885 
886  bool createpics = false;
887 
888  memset(yuvpalette, 0, sizeof(yuvpalette));
889  memset(rgbpalette, 0, sizeof(rgbpalette));
890 
891  if (write(1, "\n", 1) != 1)
892  printf("ERROR: write failed");
893 
894  if (sizeof (char) != 1 || sizeof (int) < 2) /* very unlikely */
895  exc_throw(MiscError, "Incompatible variable sizes.");
896 
897  if ((p = strrchr(supfile, '.')) != NULL)
898  i = p - supfile;
899  else
900  i = strlen(supfile);
901  if (i > 950)
902  exc_throw(MiscError, "Can "
903  "not manage filenames longer than 950 characters.");
904  memcpy(fnbuf, supfile, i);
905  p = fnbuf + i;
906  strcpy(p, ".d/");
907  p += strlen(p);
908 
909  if (!dexists(fnbuf))
910  xmkdir(fnbuf, 0755);
911 
912  if (fexists(ifofile))
913  ifopalette(ifofile, yuvpalette, rgbpalette);
914  else
915  argpalette(ifofile, yuvpalette, rgbpalette);
916 
917  strcpy(p, "palette.ycrcb");
918  if (samepalette(fnbuf, yuvpalette))
919  {
920  createpics = false;
921  printf("Found palette.yuv having our palette information...\n");
922  printf("...Skipping image files, Writing spumux.tmp.\n");
923  }
924  else
925  {
926  createpics = true;
927  printf("Writing image files and spumux.tmp.\n");
928  }
929 
930  strcpy(p, "spumux.tmp");
931  FILE *fh = xfopen(fnbuf, "wb");
932 
933  xxfwriteCS(fh, "<subpictures>\n <stream>\n");
934  pxsubtitle(supfile, fh, rgbpalette, createpics, delay_ms, fnbuf, p);
935 
936  if (write(1, "\n", 1) != 1)
937  printf("ERROR: write failed");
938 
939  xxfwriteCS(fh, " </stream>\n</subpictures>\n");
940  fclose(fh);
941 
942  if (createpics)
943  {
944  printf("Writing palette.ycrcb and palette.rgb.\n");
945  strcpy(p, "palette.ycrcb");
946  FILE *yuvfh = xfopen(fnbuf, "wb");
947  strcpy(p, "palette.rgb");
948  FILE *rgbfh = xfopen(fnbuf, "wb");
949  for (i = 0; i < 16; i++)
950  {
951  fprintf(yuvfh, "%02x%02x%02x\n",
952  yuvpalette[i][0], yuvpalette[i][1], yuvpalette[i][2]);
953  fprintf(rgbfh, "%02x%02x%02x\n",
954  rgbpalette[i][0], rgbpalette[i][1], rgbpalette[i][2]);
955  }
956  fclose(yuvfh);
957  fclose(rgbfh);
958  }
959 
960  {
961  char buf[1024];
962  printf("Renaming spumux.tmp to spumux.xml.\n");
963  strcpy(p, "spumux.tmp");
964  i = strlen(fnbuf);
965  memcpy(buf, fnbuf, i);
966  strcpy(&buf[i - 3], "xml");
967  unlink(buf);
968  rename(fnbuf, buf);
969  }
970  p[0] = '\0';
971  printf("Output files reside in %s\n", fnbuf);
972  printf("All done.\n");
973  }
974 
975  exc_catchall
976  {
977  if (write(2, EXC.m_msgbuf, EXC.m_buflen) != EXC.m_buflen)
978  printf("ERROR: write failed");
979 
980  if (write(2, "\n", 1) != 1)
981  printf("ERROR: write failed");
982 
983  exc_cleanup();
984  return 1;
985  }
986  exc_endall(1);
987 
988  return 0;
989 }
BoundStr::m_p
eu8 * m_p
Definition: pxsup2dast.c:621
xfseek0
static void xfseek0(FILE *stream, long offset)
Definition: pxsup2dast.c:249
bool
bool
Definition: pxsup2dast.c:30
li16
int_least16_t li16
Definition: pxsup2dast.c:56
exc__state::m_prev
struct exc__state * m_prev
Definition: pxsup2dast.c:88
samepalette
static bool samepalette(char *filename, eu8 palette[16][3])
Definition: pxsup2dast.c:854
Png4File
struct Png4File Png4File
ei16
int16_t ei16
Definition: pxsup2dast.c:52
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:644
xxfwrite
static void xxfwrite(FILE *stream, const eu8 *ptr, size_t size)
Definition: pxsup2dast.c:187
x2
static int x2
Definition: mythsocket.cpp:61
Png4File::m_buffer
eu8 m_buffer[65536]
Definition: pxsup2dast.c:403
rgb2yuv
static void rgb2yuv(eu8 r, eu8 g, eu8 b, eu8 *y, eu8 *cr, eu8 *cb)
Definition: pxsup2dast.c:213
exc_end
#define exc_end(x)
Definition: pxsup2dast.c:112
mythburn.write
def write(text, progress=True)
Definition: mythburn.py:308
arg
arg(title).arg(filename).arg(doDelete))
set2palettes
static void set2palettes(int value, eu8 *yuvpalpart, eu8 *rgbpalpart)
Definition: pxsup2dast.c:358
ifopalette
static void ifopalette(const char *filename, eu8 yuvpalette[16][3], eu8 rgbpalette[16][3])
Definition: pxsup2dast.c:327
exc__state::m_env
jmp_buf m_env
Definition: pxsup2dast.c:89
fu32
uint_fast32_t fu32
Definition: pxsup2dast.c:61
pixel
static guint32 * pixel
--------------------------------------------------—**
Definition: goom_core.cpp:28
argpalette
static void argpalette(const char *arg, eu8 yuvpalette[16][3], eu8 rgbpalette[16][3])
Definition: pxsup2dast.c:372
Png4File::m_paletteChunk
eu8 m_paletteChunk[24]
Definition: pxsup2dast.c:402
exc_try
#define exc_try(x)
Definition: pxsup2dast.c:103
NULL
#define NULL
Definition: H2645Parser.h:67
bool8
unsigned char bool8
Definition: pxsup2dast.c:31
__exc_throw
static void __exc_throw(int type)
Definition: pxsup2dast.c:123
exc_endall
#define exc_endall(x)
Definition: pxsup2dast.c:119
dexists
static bool dexists(const char *filename)
Definition: pxsup2dast.c:271
fi8
int_fast8_t fi8
Definition: pxsup2dast.c:59
mythburn.FILE
int FILE
Definition: mythburn.py:139
xxfwrite_uint32_be
static void xxfwrite_uint32_be(FILE *fh, eu32 value)
Definition: pxsup2dast.c:320
BoundStr::m_l
int m_l
Definition: pxsup2dast.c:622
lu16
uint_least16_t lu16
Definition: pxsup2dast.c:56
Png4File::m_bufPos
int m_bufPos
Definition: pxsup2dast.c:398
li8
int_least8_t li8
Definition: pxsup2dast.c:55
CUS
#define CUS
Definition: pxsup2dast.c:78
set_uint32_be
static void set_uint32_be(eu8 *ptr, eu32 value)
Definition: pxsup2dast.c:302
m_msgbuf
char m_msgbuf[1024]
Definition: pxsup2dast.c:95
yuv2rgb
static void yuv2rgb(int y, int cr, int cb, eu8 *r, eu8 *g, eu8 *b)
Definition: pxsup2dast.c:200
Png4File
Definition: pxsup2dast.c:392
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:583
png4file_addpixel
static void png4file_addpixel(Png4File *self, eu8 pixel)
Definition: pxsup2dast.c:461
BoundStr
struct BoundStr BoundStr
Png4File::m_width
int m_width
Definition: pxsup2dast.c:395
x1
static int x1
Definition: mythsocket.cpp:60
m_buflen
int m_buflen
Definition: pxsup2dast.c:96
exc__state
Definition: pxsup2dast.c:86
boundstr_read
static eu8 * boundstr_read(BoundStr *bs, int l)
Definition: pxsup2dast.c:631
lu32
uint_least32_t lu32
Definition: pxsup2dast.c:57
li32
int_least32_t li32
Definition: pxsup2dast.c:57
filename
QString filename
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:634
xxfread
static eu8 * xxfread(FILE *stream, eu8 *ptr, size_t size)
Definition: pxsup2dast.c:176
fu16
uint_fast16_t fu16
Definition: pxsup2dast.c:60
Png4File::m_fh
FILE * m_fh
Definition: pxsup2dast.c:394
Png4File::m_nibble
int m_nibble
Definition: pxsup2dast.c:397
png4file_flush
static void png4file_flush(Png4File *self)
Definition: pxsup2dast.c:448
Png4File::m_adler
eu32 m_adler
Definition: pxsup2dast.c:401
usage
static void usage(char *progname)
Definition: replex.cpp:2400
eu32
uint32_t eu32
Definition: pxsup2dast.c:53
getnibble
static eu8 getnibble(eu8 **data, int *nibble)
Definition: pxsup2dast.c:510
exc_cleanup
#define exc_cleanup()
Definition: pxsup2dast.c:121
ei32
int32_t ei32
Definition: pxsup2dast.c:53
EXC
struct @82 EXC
png4file_open
static void png4file_open(Png4File *self, const char *filename, int height, int width)
Definition: pxsup2dast.c:415
EOFIndicator
@ EOFIndicator
Definition: pxsup2dast.c:80
pts2ts
static char * pts2ts(fu32 pts, char *rvbuf, bool is_png_filename)
Definition: pxsup2dast.c:602
xfopen
static FILE * xfopen(const char *filename, const char *mode)
Definition: pxsup2dast.c:241
next
PictureAttribute next(PictureAttributeSupported Supported, PictureAttribute Attribute)
Definition: videoouttypes.h:350
musicbrainzngs.compat.bytes
bytes
Definition: compat.py:49
fexists
static bool fexists(const char *filename)
Definition: pxsup2dast.c:262
BoundStr
Definition: pxsup2dast.c:619
xmkdir
static void xmkdir(const char *path, int mode)
Definition: pxsup2dast.c:255
exc_catch
#define exc_catch(x, t)
Definition: pxsup2dast.c:110
getpixelline
static void getpixelline(eu8 **data, int width, Png4File *picfile)
Definition: pxsup2dast.c:524
lu8
uint_least8_t lu8
Definition: pxsup2dast.c:55
png4file_endrow
static void png4file_endrow(Png4File *self)
Definition: pxsup2dast.c:474
fu8
uint_fast8_t fu8
Definition: pxsup2dast.c:59
get_uint32_be
static fu32 get_uint32_be(const eu8 *bytes)
Definition: pxsup2dast.c:281
Png4File::m_hleft
int m_hleft
Definition: pxsup2dast.c:396
Png4File::m_crc
eu32 m_crc
Definition: pxsup2dast.c:400
MiscError
@ MiscError
Definition: pxsup2dast.c:80
fi16
int_fast16_t fi16
Definition: pxsup2dast.c:60
eu8
uint8_t eu8
Definition: pxsup2dast.c:51
IndexError
@ IndexError
Definition: pxsup2dast.c:80
png4file_close
static void png4file_close(Png4File *self)
Definition: pxsup2dast.c:487
boundstr_init
static void boundstr_init(BoundStr *bs, eu8 *p, int l)
Definition: pxsup2dast.c:625
fi32
int_fast32_t fi32
Definition: pxsup2dast.c:61
exc_throw
static void exc_throw(int type, const char *format,...)
Definition: pxsup2dast.c:130
png4file_init
static void png4file_init(Png4File *self, eu8 palette[4][3])
Definition: pxsup2dast.c:406
uint16_t
unsigned short uint16_t
Definition: iso6937tables.h:1
xxfwriteCS
#define xxfwriteCS(f, s)
Definition: pxsup2dast.c:198
get_uint16_be
static fu16 get_uint16_be(const eu8 *bytes)
Definition: pxsup2dast.c:286
set_uint16_le
static void set_uint16_le(eu8 *ptr, eu16 value)
Definition: pxsup2dast.c:315
exc_catchall
#define exc_catchall
Definition: pxsup2dast.c:118
crc32
static uint32_t crc32(const unsigned char *data, int len)
Definition: dsmcc.cpp:613
get_uint32_le
static fu32 get_uint32_le(const eu8 *bytes)
Definition: pxsup2dast.c:291
ei8
int8_t ei8
Definition: pxsup2dast.c:51
Png4File::m_chunkLen
int m_chunkLen
Definition: pxsup2dast.c:399
sup2dast
int sup2dast(const char *supfile, const char *ifofile, int delay_ms)
Definition: pxsup2dast.c:876
eu16
uint16_t eu16
Definition: pxsup2dast.c:52
m_last
struct exc__state * m_last
Definition: pxsup2dast.c:94