Ignore svn changes up to r30255
[mplayer/glamo.git] / vobsub.c
blobdf4b2d0a540b36fc477dfd0e1998c2c47b877d76
1 /*
2 * Some code freely inspired from VobSub <URL:http://vobsub.edensrising.com>,
3 * with kind permission from Gabest <gabest@freemail.hu>
4 */
5 #include <ctype.h>
6 #include <errno.h>
7 #include <limits.h>
8 #include <stddef.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <fcntl.h>
13 #include <unistd.h>
14 #include <sys/stat.h>
15 #include <sys/types.h>
17 #include "config.h"
18 #include "version.h"
20 #include "vobsub.h"
21 #include "spudec.h"
22 #include "mp_msg.h"
23 #include "unrar_exec.h"
24 #include "libavutil/common.h"
26 extern int vobsub_id;
27 // Record the original -vobsubid set by commandline, since vobsub_id will be
28 // overridden if slang match any of vobsub streams.
29 static int vobsubid = -2;
31 /**********************************************************************
32 * RAR stream handling
33 * The RAR file must have the same basename as the file to open
34 **********************************************************************/
35 #ifdef CONFIG_UNRAR_EXEC
36 typedef struct {
37 FILE *file;
38 unsigned char *data;
39 unsigned long size;
40 unsigned long pos;
41 } rar_stream_t;
43 static rar_stream_t *rar_open(const char *const filename,
44 const char *const mode)
46 rar_stream_t *stream;
47 /* unrar_exec can only read */
48 if (strcmp("r", mode) && strcmp("rb", mode)) {
49 errno = EINVAL;
50 return NULL;
52 stream = malloc(sizeof(rar_stream_t));
53 if (stream == NULL)
54 return NULL;
55 /* first try normal access */
56 stream->file = fopen(filename, mode);
57 if (stream->file == NULL) {
58 char *rar_filename;
59 const char *p;
60 int rc;
61 /* Guess the RAR archive filename */
62 rar_filename = NULL;
63 p = strrchr(filename, '.');
64 if (p) {
65 ptrdiff_t l = p - filename;
66 rar_filename = malloc(l + 5);
67 if (rar_filename == NULL) {
68 free(stream);
69 return NULL;
71 strncpy(rar_filename, filename, l);
72 strcpy(rar_filename + l, ".rar");
73 } else {
74 rar_filename = malloc(strlen(filename) + 5);
75 if (rar_filename == NULL) {
76 free(stream);
77 return NULL;
79 strcpy(rar_filename, filename);
80 strcat(rar_filename, ".rar");
82 /* get rid of the path if there is any */
83 if ((p = strrchr(filename, '/')) == NULL) {
84 p = filename;
85 } else {
86 p++;
88 rc = unrar_exec_get(&stream->data, &stream->size, p, rar_filename);
89 if (!rc) {
90 /* There is no matching filename in the archive. However, sometimes
91 * the files we are looking for have been given arbitrary names in the archive.
92 * Let's look for a file with an exact match in the extension only. */
93 int i, num_files, name_len;
94 ArchiveList_struct *list, *lp;
95 num_files = unrar_exec_list(rar_filename, &list);
96 if (num_files > 0) {
97 char *demanded_ext;
98 demanded_ext = strrchr (p, '.');
99 if (demanded_ext) {
100 int demanded_ext_len = strlen (demanded_ext);
101 for (i = 0, lp = list; i < num_files; i++, lp = lp->next) {
102 name_len = strlen (lp->item.Name);
103 if (name_len >= demanded_ext_len && !strcasecmp (lp->item.Name + name_len - demanded_ext_len, demanded_ext)) {
104 rc = unrar_exec_get(&stream->data, &stream->size,
105 lp->item.Name, rar_filename);
106 if (rc)
107 break;
111 unrar_exec_freelist(list);
113 if (!rc) {
114 free(rar_filename);
115 free(stream);
116 return NULL;
120 free(rar_filename);
121 stream->pos = 0;
123 return stream;
126 static int rar_close(rar_stream_t *stream)
128 if (stream->file)
129 return fclose(stream->file);
130 free(stream->data);
131 return 0;
134 static int rar_eof(rar_stream_t *stream)
136 if (stream->file)
137 return feof(stream->file);
138 return stream->pos >= stream->size;
141 static long rar_tell(rar_stream_t *stream)
143 if (stream->file)
144 return ftell(stream->file);
145 return stream->pos;
148 static int rar_seek(rar_stream_t *stream, long offset, int whence)
150 if (stream->file)
151 return fseek(stream->file, offset, whence);
152 switch (whence) {
153 case SEEK_SET:
154 if (offset < 0) {
155 errno = EINVAL;
156 return -1;
158 stream->pos = offset;
159 break;
160 case SEEK_CUR:
161 if (offset < 0 && stream->pos < (unsigned long) -offset) {
162 errno = EINVAL;
163 return -1;
165 stream->pos += offset;
166 break;
167 case SEEK_END:
168 if (offset < 0 && stream->size < (unsigned long) -offset) {
169 errno = EINVAL;
170 return -1;
172 stream->pos = stream->size + offset;
173 break;
174 default:
175 errno = EINVAL;
176 return -1;
178 return 0;
181 static int rar_getc(rar_stream_t *stream)
183 if (stream->file)
184 return getc(stream->file);
185 if (rar_eof(stream))
186 return EOF;
187 return stream->data[stream->pos++];
190 static size_t rar_read(void *ptr, size_t size, size_t nmemb,
191 rar_stream_t *stream)
193 size_t res;
194 unsigned long remain;
195 if (stream->file)
196 return fread(ptr, size, nmemb, stream->file);
197 if (rar_eof(stream))
198 return 0;
199 res = size * nmemb;
200 remain = stream->size - stream->pos;
201 if (res > remain)
202 res = remain / size * size;
203 memcpy(ptr, stream->data + stream->pos, res);
204 stream->pos += res;
205 res /= size;
206 return res;
209 #else
210 typedef FILE rar_stream_t;
211 #define rar_open fopen
212 #define rar_close fclose
213 #define rar_eof feof
214 #define rar_tell ftell
215 #define rar_seek fseek
216 #define rar_getc getc
217 #define rar_read fread
218 #endif
220 /**********************************************************************/
222 static ssize_t vobsub_getline(char **lineptr, size_t *n, rar_stream_t *stream)
224 size_t res = 0;
225 int c;
226 if (*lineptr == NULL) {
227 *lineptr = malloc(4096);
228 if (*lineptr)
229 *n = 4096;
230 } else if (*n == 0) {
231 char *tmp = realloc(*lineptr, 4096);
232 if (tmp) {
233 *lineptr = tmp;
234 *n = 4096;
237 if (*lineptr == NULL || *n == 0)
238 return -1;
240 for (c = rar_getc(stream); c != EOF; c = rar_getc(stream)) {
241 if (res + 1 >= *n) {
242 char *tmp = realloc(*lineptr, *n * 2);
243 if (tmp == NULL)
244 return -1;
245 *lineptr = tmp;
246 *n *= 2;
248 (*lineptr)[res++] = c;
249 if (c == '\n') {
250 (*lineptr)[res] = 0;
251 return res;
254 if (res == 0)
255 return -1;
256 (*lineptr)[res] = 0;
257 return res;
260 /**********************************************************************
261 * MPEG parsing
262 **********************************************************************/
264 typedef struct {
265 rar_stream_t *stream;
266 unsigned int pts;
267 int aid;
268 unsigned char *packet;
269 unsigned int packet_reserve;
270 unsigned int packet_size;
271 } mpeg_t;
273 static mpeg_t *mpeg_open(const char *filename)
275 mpeg_t *res = malloc(sizeof(mpeg_t));
276 int err = res == NULL;
277 if (!err) {
278 res->pts = 0;
279 res->aid = -1;
280 res->packet = NULL;
281 res->packet_size = 0;
282 res->packet_reserve = 0;
283 res->stream = rar_open(filename, "rb");
284 err = res->stream == NULL;
285 if (err)
286 perror("fopen Vobsub file failed");
287 if (err)
288 free(res);
290 return err ? NULL : res;
293 static void mpeg_free(mpeg_t *mpeg)
295 if (mpeg->packet)
296 free(mpeg->packet);
297 if (mpeg->stream)
298 rar_close(mpeg->stream);
299 free(mpeg);
302 static int mpeg_eof(mpeg_t *mpeg)
304 return rar_eof(mpeg->stream);
307 static off_t mpeg_tell(mpeg_t *mpeg)
309 return rar_tell(mpeg->stream);
312 static int mpeg_run(mpeg_t *mpeg)
314 unsigned int len, idx, version;
315 int c;
316 /* Goto start of a packet, it starts with 0x000001?? */
317 const unsigned char wanted[] = { 0, 0, 1 };
318 unsigned char buf[5];
320 mpeg->aid = -1;
321 mpeg->packet_size = 0;
322 if (rar_read(buf, 4, 1, mpeg->stream) != 1)
323 return -1;
324 while (memcmp(buf, wanted, sizeof(wanted)) != 0) {
325 c = rar_getc(mpeg->stream);
326 if (c < 0)
327 return -1;
328 memmove(buf, buf + 1, 3);
329 buf[3] = c;
331 switch (buf[3]) {
332 case 0xb9: /* System End Code */
333 break;
334 case 0xba: /* Packet start code */
335 c = rar_getc(mpeg->stream);
336 if (c < 0)
337 return -1;
338 if ((c & 0xc0) == 0x40)
339 version = 4;
340 else if ((c & 0xf0) == 0x20)
341 version = 2;
342 else {
343 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Unsupported MPEG version: 0x%02x\n", c);
344 return -1;
346 if (version == 4) {
347 if (rar_seek(mpeg->stream, 9, SEEK_CUR))
348 return -1;
349 } else if (version == 2) {
350 if (rar_seek(mpeg->stream, 7, SEEK_CUR))
351 return -1;
352 } else
353 abort();
354 break;
355 case 0xbd: /* packet */
356 if (rar_read(buf, 2, 1, mpeg->stream) != 1)
357 return -1;
358 len = buf[0] << 8 | buf[1];
359 idx = mpeg_tell(mpeg);
360 c = rar_getc(mpeg->stream);
361 if (c < 0)
362 return -1;
363 if ((c & 0xC0) == 0x40) { /* skip STD scale & size */
364 if (rar_getc(mpeg->stream) < 0)
365 return -1;
366 c = rar_getc(mpeg->stream);
367 if (c < 0)
368 return -1;
370 if ((c & 0xf0) == 0x20) { /* System-1 stream timestamp */
371 /* Do we need this? */
372 abort();
373 } else if ((c & 0xf0) == 0x30) {
374 /* Do we need this? */
375 abort();
376 } else if ((c & 0xc0) == 0x80) { /* System-2 (.VOB) stream */
377 unsigned int pts_flags, hdrlen, dataidx;
378 c = rar_getc(mpeg->stream);
379 if (c < 0)
380 return -1;
381 pts_flags = c;
382 c = rar_getc(mpeg->stream);
383 if (c < 0)
384 return -1;
385 hdrlen = c;
386 dataidx = mpeg_tell(mpeg) + hdrlen;
387 if (dataidx > idx + len) {
388 mp_msg(MSGT_VOBSUB, MSGL_ERR, "Invalid header length: %d (total length: %d, idx: %d, dataidx: %d)\n",
389 hdrlen, len, idx, dataidx);
390 return -1;
392 if ((pts_flags & 0xc0) == 0x80) {
393 if (rar_read(buf, 5, 1, mpeg->stream) != 1)
394 return -1;
395 if (!(((buf[0] & 0xf0) == 0x20) && (buf[0] & 1) && (buf[2] & 1) && (buf[4] & 1))) {
396 mp_msg(MSGT_VOBSUB, MSGL_ERR, "vobsub PTS error: 0x%02x %02x%02x %02x%02x \n",
397 buf[0], buf[1], buf[2], buf[3], buf[4]);
398 mpeg->pts = 0;
399 } else
400 mpeg->pts = ((buf[0] & 0x0e) << 29 | buf[1] << 22 | (buf[2] & 0xfe) << 14
401 | buf[3] << 7 | (buf[4] >> 1));
402 } else /* if ((pts_flags & 0xc0) == 0xc0) */ {
403 /* what's this? */
404 /* abort(); */
406 rar_seek(mpeg->stream, dataidx, SEEK_SET);
407 mpeg->aid = rar_getc(mpeg->stream);
408 if (mpeg->aid < 0) {
409 mp_msg(MSGT_VOBSUB, MSGL_ERR, "Bogus aid %d\n", mpeg->aid);
410 return -1;
412 mpeg->packet_size = len - ((unsigned int) mpeg_tell(mpeg) - idx);
413 if (mpeg->packet_reserve < mpeg->packet_size) {
414 if (mpeg->packet)
415 free(mpeg->packet);
416 mpeg->packet = malloc(mpeg->packet_size);
417 if (mpeg->packet)
418 mpeg->packet_reserve = mpeg->packet_size;
420 if (mpeg->packet == NULL) {
421 mp_msg(MSGT_VOBSUB, MSGL_FATAL, "malloc failure");
422 mpeg->packet_reserve = 0;
423 mpeg->packet_size = 0;
424 return -1;
426 if (rar_read(mpeg->packet, mpeg->packet_size, 1, mpeg->stream) != 1) {
427 mp_msg(MSGT_VOBSUB, MSGL_ERR, "fread failure");
428 mpeg->packet_size = 0;
429 return -1;
431 idx = len;
433 break;
434 case 0xbe: /* Padding */
435 if (rar_read(buf, 2, 1, mpeg->stream) != 1)
436 return -1;
437 len = buf[0] << 8 | buf[1];
438 if (len > 0 && rar_seek(mpeg->stream, len, SEEK_CUR))
439 return -1;
440 break;
441 default:
442 if (0xc0 <= buf[3] && buf[3] < 0xf0) {
443 /* MPEG audio or video */
444 if (rar_read(buf, 2, 1, mpeg->stream) != 1)
445 return -1;
446 len = buf[0] << 8 | buf[1];
447 if (len > 0 && rar_seek(mpeg->stream, len, SEEK_CUR))
448 return -1;
449 } else {
450 mp_msg(MSGT_VOBSUB, MSGL_ERR, "unknown header 0x%02X%02X%02X%02X\n",
451 buf[0], buf[1], buf[2], buf[3]);
452 return -1;
455 return 0;
458 /**********************************************************************
459 * Packet queue
460 **********************************************************************/
462 typedef struct {
463 unsigned int pts100;
464 off_t filepos;
465 unsigned int size;
466 unsigned char *data;
467 } packet_t;
469 typedef struct {
470 char *id;
471 packet_t *packets;
472 unsigned int packets_reserve;
473 unsigned int packets_size;
474 unsigned int current_index;
475 } packet_queue_t;
477 static void packet_construct(packet_t *pkt)
479 pkt->pts100 = 0;
480 pkt->filepos = 0;
481 pkt->size = 0;
482 pkt->data = NULL;
485 static void packet_destroy(packet_t *pkt)
487 if (pkt->data)
488 free(pkt->data);
491 static void packet_queue_construct(packet_queue_t *queue)
493 queue->id = NULL;
494 queue->packets = NULL;
495 queue->packets_reserve = 0;
496 queue->packets_size = 0;
497 queue->current_index = 0;
500 static void packet_queue_destroy(packet_queue_t *queue)
502 if (queue->packets) {
503 while (queue->packets_size--)
504 packet_destroy(queue->packets + queue->packets_size);
505 free(queue->packets);
507 return;
510 /* Make sure there is enough room for needed_size packets in the
511 packet queue. */
512 static int packet_queue_ensure(packet_queue_t *queue, unsigned int needed_size)
514 if (queue->packets_reserve < needed_size) {
515 if (queue->packets) {
516 packet_t *tmp = realloc(queue->packets, 2 * queue->packets_reserve * sizeof(packet_t));
517 if (tmp == NULL) {
518 mp_msg(MSGT_VOBSUB, MSGL_FATAL, "realloc failure");
519 return -1;
521 queue->packets = tmp;
522 queue->packets_reserve *= 2;
523 } else {
524 queue->packets = malloc(sizeof(packet_t));
525 if (queue->packets == NULL) {
526 mp_msg(MSGT_VOBSUB, MSGL_FATAL, "malloc failure");
527 return -1;
529 queue->packets_reserve = 1;
532 return 0;
535 /* add one more packet */
536 static int packet_queue_grow(packet_queue_t *queue)
538 if (packet_queue_ensure(queue, queue->packets_size + 1) < 0)
539 return -1;
540 packet_construct(queue->packets + queue->packets_size);
541 ++queue->packets_size;
542 return 0;
545 /* insert a new packet, duplicating pts from the current one */
546 static int packet_queue_insert(packet_queue_t *queue)
548 packet_t *pkts;
549 if (packet_queue_ensure(queue, queue->packets_size + 1) < 0)
550 return -1;
551 /* XXX packet_size does not reflect the real thing here, it will be updated a bit later */
552 memmove(queue->packets + queue->current_index + 2,
553 queue->packets + queue->current_index + 1,
554 sizeof(packet_t) * (queue->packets_size - queue->current_index - 1));
555 pkts = queue->packets + queue->current_index;
556 ++queue->packets_size;
557 ++queue->current_index;
558 packet_construct(pkts + 1);
559 pkts[1].pts100 = pkts[0].pts100;
560 pkts[1].filepos = pkts[0].filepos;
561 return 0;
564 /**********************************************************************
565 * Vobsub
566 **********************************************************************/
568 typedef struct {
569 unsigned int palette[16];
570 int delay;
571 unsigned int have_palette;
572 unsigned int orig_frame_width, orig_frame_height;
573 unsigned int origin_x, origin_y;
574 /* index */
575 packet_queue_t *spu_streams;
576 unsigned int spu_streams_size;
577 unsigned int spu_streams_current;
578 unsigned int spu_valid_streams_size;
579 } vobsub_t;
581 /* Make sure that the spu stream idx exists. */
582 static int vobsub_ensure_spu_stream(vobsub_t *vob, unsigned int index)
584 if (index >= vob->spu_streams_size) {
585 /* This is a new stream */
586 if (vob->spu_streams) {
587 packet_queue_t *tmp = realloc(vob->spu_streams, (index + 1) * sizeof(packet_queue_t));
588 if (tmp == NULL) {
589 mp_msg(MSGT_VOBSUB, MSGL_ERR, "vobsub_ensure_spu_stream: realloc failure");
590 return -1;
592 vob->spu_streams = tmp;
593 } else {
594 vob->spu_streams = malloc((index + 1) * sizeof(packet_queue_t));
595 if (vob->spu_streams == NULL) {
596 mp_msg(MSGT_VOBSUB, MSGL_ERR, "vobsub_ensure_spu_stream: malloc failure");
597 return -1;
600 while (vob->spu_streams_size <= index) {
601 packet_queue_construct(vob->spu_streams + vob->spu_streams_size);
602 ++vob->spu_streams_size;
605 return 0;
608 static int vobsub_add_id(vobsub_t *vob, const char *id, size_t idlen,
609 const unsigned int index)
611 if (vobsub_ensure_spu_stream(vob, index) < 0)
612 return -1;
613 if (id && idlen) {
614 if (vob->spu_streams[index].id)
615 free(vob->spu_streams[index].id);
616 vob->spu_streams[index].id = malloc(idlen + 1);
617 if (vob->spu_streams[index].id == NULL) {
618 mp_msg(MSGT_VOBSUB, MSGL_FATAL, "vobsub_add_id: malloc failure");
619 return -1;
621 vob->spu_streams[index].id[idlen] = 0;
622 memcpy(vob->spu_streams[index].id, id, idlen);
624 vob->spu_streams_current = index;
625 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VOBSUB_ID=%d\n", index);
626 if (id && idlen)
627 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VSID_%d_LANG=%s\n", index, vob->spu_streams[index].id);
628 mp_msg(MSGT_VOBSUB, MSGL_V, "[vobsub] subtitle (vobsubid): %d language %s\n",
629 index, vob->spu_streams[index].id);
630 return 0;
633 static int vobsub_add_timestamp(vobsub_t *vob, off_t filepos, int ms)
635 packet_queue_t *queue;
636 packet_t *pkt;
637 if (vob->spu_streams == 0) {
638 mp_msg(MSGT_VOBSUB, MSGL_WARN, "[vobsub] warning, binning some index entries. Check your index file\n");
639 return -1;
641 queue = vob->spu_streams + vob->spu_streams_current;
642 if (packet_queue_grow(queue) >= 0) {
643 pkt = queue->packets + (queue->packets_size - 1);
644 pkt->filepos = filepos;
645 pkt->pts100 = ms < 0 ? UINT_MAX : (unsigned int)ms * 90;
646 return 0;
648 return -1;
651 static int vobsub_parse_id(vobsub_t *vob, const char *line)
653 // id: xx, index: n
654 size_t idlen;
655 const char *p, *q;
656 p = line;
657 while (isspace(*p))
658 ++p;
659 q = p;
660 while (isalpha(*q))
661 ++q;
662 idlen = q - p;
663 if (idlen == 0)
664 return -1;
665 ++q;
666 while (isspace(*q))
667 ++q;
668 if (strncmp("index:", q, 6))
669 return -1;
670 q += 6;
671 while (isspace(*q))
672 ++q;
673 if (!isdigit(*q))
674 return -1;
675 return vobsub_add_id(vob, p, idlen, atoi(q));
678 static int vobsub_parse_timestamp(vobsub_t *vob, const char *line)
680 // timestamp: HH:MM:SS.mmm, filepos: 0nnnnnnnnn
681 const char *p;
682 int h, m, s, ms;
683 off_t filepos;
684 while (isspace(*line))
685 ++line;
686 p = line;
687 while (isdigit(*p))
688 ++p;
689 if (p - line != 2)
690 return -1;
691 h = atoi(line);
692 if (*p != ':')
693 return -1;
694 line = ++p;
695 while (isdigit(*p))
696 ++p;
697 if (p - line != 2)
698 return -1;
699 m = atoi(line);
700 if (*p != ':')
701 return -1;
702 line = ++p;
703 while (isdigit(*p))
704 ++p;
705 if (p - line != 2)
706 return -1;
707 s = atoi(line);
708 if (*p != ':')
709 return -1;
710 line = ++p;
711 while (isdigit(*p))
712 ++p;
713 if (p - line != 3)
714 return -1;
715 ms = atoi(line);
716 if (*p != ',')
717 return -1;
718 line = p + 1;
719 while (isspace(*line))
720 ++line;
721 if (strncmp("filepos:", line, 8))
722 return -1;
723 line += 8;
724 while (isspace(*line))
725 ++line;
726 if (! isxdigit(*line))
727 return -1;
728 filepos = strtol(line, NULL, 16);
729 return vobsub_add_timestamp(vob, filepos, vob->delay + ms + 1000 * (s + 60 * (m + 60 * h)));
732 static int vobsub_parse_origin(vobsub_t *vob, const char *line)
734 // org: X,Y
735 char *p;
736 while (isspace(*line))
737 ++line;
738 if (!isdigit(*line))
739 return -1;
740 vob->origin_x = strtoul(line, &p, 10);
741 if (*p != ',')
742 return -1;
743 ++p;
744 vob->origin_y = strtoul(p, NULL, 10);
745 return 0;
748 unsigned int vobsub_palette_to_yuv(unsigned int pal)
750 int r, g, b, y, u, v;
751 // Palette in idx file is not rgb value, it was calculated by wrong formula.
752 // Here's reversed formula of the one used to generate palette in idx file.
753 r = pal >> 16 & 0xff;
754 g = pal >> 8 & 0xff;
755 b = pal & 0xff;
756 y = av_clip_uint8( 0.1494 * r + 0.6061 * g + 0.2445 * b);
757 u = av_clip_uint8( 0.6066 * r - 0.4322 * g - 0.1744 * b + 128);
758 v = av_clip_uint8(-0.08435 * r - 0.3422 * g + 0.4266 * b + 128);
759 y = y * 219 / 255 + 16;
760 return y << 16 | u << 8 | v;
763 unsigned int vobsub_rgb_to_yuv(unsigned int rgb)
765 int r, g, b, y, u, v;
766 r = rgb >> 16 & 0xff;
767 g = rgb >> 8 & 0xff;
768 b = rgb & 0xff;
769 y = ( 0.299 * r + 0.587 * g + 0.114 * b) * 219 / 255 + 16.5;
770 u = (-0.16874 * r - 0.33126 * g + 0.5 * b) * 224 / 255 + 128.5;
771 v = ( 0.5 * r - 0.41869 * g - 0.08131 * b) * 224 / 255 + 128.5;
772 return y << 16 | u << 8 | v;
775 static int vobsub_parse_delay(vobsub_t *vob, const char *line)
777 int h, m, s, ms;
778 int forward = 1;
779 if (*(line + 7) == '+') {
780 forward = 1;
781 line++;
782 } else if (*(line + 7) == '-') {
783 forward = -1;
784 line++;
786 mp_msg(MSGT_SPUDEC, MSGL_V, "forward=%d", forward);
787 h = atoi(line + 7);
788 mp_msg(MSGT_VOBSUB, MSGL_V, "h=%d,", h);
789 m = atoi(line + 10);
790 mp_msg(MSGT_VOBSUB, MSGL_V, "m=%d,", m);
791 s = atoi(line + 13);
792 mp_msg(MSGT_VOBSUB, MSGL_V, "s=%d,", s);
793 ms = atoi(line + 16);
794 mp_msg(MSGT_VOBSUB, MSGL_V, "ms=%d", ms);
795 vob->delay = (ms + 1000 * (s + 60 * (m + 60 * h))) * forward;
796 return 0;
799 static int vobsub_set_lang(const char *line)
801 if (vobsub_id == -1)
802 vobsub_id = atoi(line + 8);
803 return 0;
806 static int vobsub_parse_one_line(vobsub_t *vob, rar_stream_t *fd,
807 unsigned char **extradata,
808 unsigned int *extradata_len)
810 ssize_t line_size;
811 int res = -1;
812 size_t line_reserve = 0;
813 char *line = NULL;
814 do {
815 line_size = vobsub_getline(&line, &line_reserve, fd);
816 if (line_size < 0 || line_size > 1000000 ||
817 *extradata_len+line_size > 10000000) {
818 break;
821 *extradata = realloc(*extradata, *extradata_len+line_size+1);
822 memcpy(*extradata+*extradata_len, line, line_size);
823 *extradata_len += line_size;
824 (*extradata)[*extradata_len] = 0;
826 if (*line == 0 || *line == '\r' || *line == '\n' || *line == '#')
827 continue;
828 else if (strncmp("langidx:", line, 8) == 0)
829 res = vobsub_set_lang(line);
830 else if (strncmp("delay:", line, 6) == 0)
831 res = vobsub_parse_delay(vob, line);
832 else if (strncmp("id:", line, 3) == 0)
833 res = vobsub_parse_id(vob, line + 3);
834 else if (strncmp("org:", line, 4) == 0)
835 res = vobsub_parse_origin(vob, line + 4);
836 else if (strncmp("timestamp:", line, 10) == 0)
837 res = vobsub_parse_timestamp(vob, line + 10);
838 else {
839 mp_msg(MSGT_VOBSUB, MSGL_V, "vobsub: ignoring %s", line);
840 continue;
842 if (res < 0)
843 mp_msg(MSGT_VOBSUB, MSGL_ERR, "ERROR in %s", line);
844 break;
845 } while (1);
846 if (line)
847 free(line);
848 return res;
851 int vobsub_parse_ifo(void* this, const char *const name, unsigned int *palette,
852 unsigned int *width, unsigned int *height, int force,
853 int sid, char *langid)
855 vobsub_t *vob = (vobsub_t*)this;
856 int res = -1;
857 rar_stream_t *fd = rar_open(name, "rb");
858 if (fd == NULL) {
859 if (force)
860 mp_msg(MSGT_VOBSUB, MSGL_WARN, "VobSub: Can't open IFO file\n");
861 } else {
862 // parse IFO header
863 unsigned char block[0x800];
864 const char *const ifo_magic = "DVDVIDEO-VTS";
865 if (rar_read(block, sizeof(block), 1, fd) != 1) {
866 if (force)
867 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Can't read IFO header\n");
868 } else if (memcmp(block, ifo_magic, strlen(ifo_magic) + 1))
869 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Bad magic in IFO header\n");
870 else {
871 unsigned long pgci_sector = block[0xcc] << 24 | block[0xcd] << 16
872 | block[0xce] << 8 | block[0xcf];
873 int standard = (block[0x200] & 0x30) >> 4;
874 int resolution = (block[0x201] & 0x0c) >> 2;
875 *height = standard ? 576 : 480;
876 *width = 0;
877 switch (resolution) {
878 case 0x0:
879 *width = 720;
880 break;
881 case 0x1:
882 *width = 704;
883 break;
884 case 0x2:
885 *width = 352;
886 break;
887 case 0x3:
888 *width = 352;
889 *height /= 2;
890 break;
891 default:
892 mp_msg(MSGT_VOBSUB, MSGL_WARN, "Vobsub: Unknown resolution %d \n", resolution);
894 if (langid && 0 <= sid && sid < 32) {
895 unsigned char *tmp = block + 0x256 + sid * 6 + 2;
896 langid[0] = tmp[0];
897 langid[1] = tmp[1];
898 langid[2] = 0;
900 if (rar_seek(fd, pgci_sector * sizeof(block), SEEK_SET)
901 || rar_read(block, sizeof(block), 1, fd) != 1)
902 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Can't read IFO PGCI\n");
903 else {
904 unsigned long idx;
905 unsigned long pgc_offset = block[0xc] << 24 | block[0xd] << 16
906 | block[0xe] << 8 | block[0xf];
907 for (idx = 0; idx < 16; ++idx) {
908 unsigned char *p = block + pgc_offset + 0xa4 + 4 * idx;
909 palette[idx] = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
911 if (vob)
912 vob->have_palette = 1;
913 res = 0;
916 rar_close(fd);
918 return res;
921 void *vobsub_open(const char *const name, const char *const ifo,
922 const int force, void** spu)
924 unsigned char *extradata = NULL;
925 unsigned int extradata_len = 0;
926 vobsub_t *vob = calloc(1, sizeof(vobsub_t));
927 if (spu)
928 *spu = NULL;
929 if (vobsubid == -2)
930 vobsubid = vobsub_id;
931 if (vob) {
932 char *buf;
933 buf = malloc(strlen(name) + 5);
934 if (buf) {
935 rar_stream_t *fd;
936 mpeg_t *mpg;
937 /* read in the info file */
938 if (!ifo) {
939 strcpy(buf, name);
940 strcat(buf, ".ifo");
941 vobsub_parse_ifo(vob, buf, vob->palette, &vob->orig_frame_width, &vob->orig_frame_height, force, -1, NULL);
942 } else
943 vobsub_parse_ifo(vob, ifo, vob->palette, &vob->orig_frame_width, &vob->orig_frame_height, force, -1, NULL);
944 /* read in the index */
945 strcpy(buf, name);
946 strcat(buf, ".idx");
947 fd = rar_open(buf, "rb");
948 if (fd == NULL) {
949 if (force)
950 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Can't open IDX file\n");
951 else {
952 free(buf);
953 free(vob);
954 return NULL;
956 } else {
957 while (vobsub_parse_one_line(vob, fd, &extradata, &extradata_len) >= 0)
958 /* NOOP */ ;
959 rar_close(fd);
961 if (spu)
962 *spu = spudec_new_scaled(vob->palette, vob->orig_frame_width, vob->orig_frame_height, extradata, extradata_len);
963 if (extradata)
964 free(extradata);
966 /* read the indexed mpeg_stream */
967 strcpy(buf, name);
968 strcat(buf, ".sub");
969 mpg = mpeg_open(buf);
970 if (mpg == NULL) {
971 if (force)
972 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Can't open SUB file\n");
973 else {
974 free(buf);
975 free(vob);
976 return NULL;
978 } else {
979 long last_pts_diff = 0;
980 while (!mpeg_eof(mpg)) {
981 off_t pos = mpeg_tell(mpg);
982 if (mpeg_run(mpg) < 0) {
983 if (!mpeg_eof(mpg))
984 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: mpeg_run error\n");
985 break;
987 if (mpg->packet_size) {
988 if ((mpg->aid & 0xe0) == 0x20) {
989 unsigned int sid = mpg->aid & 0x1f;
990 if (vobsub_ensure_spu_stream(vob, sid) >= 0) {
991 packet_queue_t *queue = vob->spu_streams + sid;
992 /* get the packet to fill */
993 if (queue->packets_size == 0 && packet_queue_grow(queue) < 0)
994 abort();
995 while (queue->current_index + 1 < queue->packets_size
996 && queue->packets[queue->current_index + 1].filepos <= pos)
997 ++queue->current_index;
998 if (queue->current_index < queue->packets_size) {
999 packet_t *pkt;
1000 if (queue->packets[queue->current_index].data) {
1001 /* insert a new packet and fix the PTS ! */
1002 packet_queue_insert(queue);
1003 queue->packets[queue->current_index].pts100 =
1004 mpg->pts + last_pts_diff;
1006 pkt = queue->packets + queue->current_index;
1007 if (pkt->pts100 != UINT_MAX) {
1008 if (queue->packets_size > 1)
1009 last_pts_diff = pkt->pts100 - mpg->pts;
1010 else
1011 pkt->pts100 = mpg->pts;
1012 /* FIXME: should not use mpg_sub internal informations, make a copy */
1013 pkt->data = mpg->packet;
1014 pkt->size = mpg->packet_size;
1015 mpg->packet = NULL;
1016 mpg->packet_reserve = 0;
1017 mpg->packet_size = 0;
1020 } else
1021 mp_msg(MSGT_VOBSUB, MSGL_WARN, "don't know what to do with subtitle #%u\n", sid);
1025 vob->spu_streams_current = vob->spu_streams_size;
1026 while (vob->spu_streams_current-- > 0) {
1027 vob->spu_streams[vob->spu_streams_current].current_index = 0;
1028 if (vobsubid == vob->spu_streams_current ||
1029 vob->spu_streams[vob->spu_streams_current].packets_size > 0)
1030 ++vob->spu_valid_streams_size;
1032 mpeg_free(mpg);
1034 free(buf);
1037 return vob;
1040 void vobsub_close(void *this)
1042 vobsub_t *vob = (vobsub_t *)this;
1043 if (vob->spu_streams) {
1044 while (vob->spu_streams_size--)
1045 packet_queue_destroy(vob->spu_streams + vob->spu_streams_size);
1046 free(vob->spu_streams);
1048 free(vob);
1051 unsigned int vobsub_get_indexes_count(void *vobhandle)
1053 vobsub_t *vob = (vobsub_t *) vobhandle;
1054 return vob->spu_valid_streams_size;
1057 char *vobsub_get_id(void *vobhandle, unsigned int index)
1059 vobsub_t *vob = (vobsub_t *) vobhandle;
1060 return (index < vob->spu_streams_size) ? vob->spu_streams[index].id : NULL;
1063 int vobsub_get_id_by_index(void *vobhandle, unsigned int index)
1065 vobsub_t *vob = vobhandle;
1066 int i, j;
1067 if (vob == NULL)
1068 return -1;
1069 for (i = 0, j = 0; i < vob->spu_streams_size; ++i)
1070 if (i == vobsubid || vob->spu_streams[i].packets_size > 0) {
1071 if (j == index)
1072 return i;
1073 ++j;
1075 return -1;
1078 int vobsub_get_index_by_id(void *vobhandle, int id)
1080 vobsub_t *vob = vobhandle;
1081 int i, j;
1082 if (vob == NULL || id < 0 || id >= vob->spu_streams_size)
1083 return -1;
1084 if (id != vobsubid && !vob->spu_streams[id].packets_size)
1085 return -1;
1086 for (i = 0, j = 0; i < id; ++i)
1087 if (i == vobsubid || vob->spu_streams[i].packets_size > 0)
1088 ++j;
1089 return j;
1092 int vobsub_set_from_lang(void *vobhandle, unsigned char * lang)
1094 int i;
1095 vobsub_t *vob= (vobsub_t *) vobhandle;
1096 while (lang && strlen(lang) >= 2) {
1097 for (i = 0; i < vob->spu_streams_size; i++)
1098 if (vob->spu_streams[i].id)
1099 if ((strncmp(vob->spu_streams[i].id, lang, 2) == 0)) {
1100 vobsub_id = i;
1101 mp_msg(MSGT_VOBSUB, MSGL_INFO, "Selected VOBSUB language: %d language: %s\n", i, vob->spu_streams[i].id);
1102 return 0;
1104 lang+=2;while (lang[0]==',' || lang[0]==' ') ++lang;
1106 mp_msg(MSGT_VOBSUB, MSGL_WARN, "No matching VOBSUB language found!\n");
1107 return -1;
1110 /// make sure we seek to the first packet of packets having same pts values.
1111 static void vobsub_queue_reseek(packet_queue_t *queue, unsigned int pts100)
1113 int reseek_count = 0;
1114 unsigned int lastpts = 0;
1116 if (queue->current_index > 0
1117 && (queue->packets[queue->current_index].pts100 == UINT_MAX
1118 || queue->packets[queue->current_index].pts100 > pts100)) {
1119 // possible pts seek previous, try to check it.
1120 int i = 1;
1121 while (queue->current_index >= i
1122 && queue->packets[queue->current_index-i].pts100 == UINT_MAX)
1123 ++i;
1124 if (queue->current_index >= i
1125 && queue->packets[queue->current_index-i].pts100 > pts100)
1126 // pts seek previous confirmed, reseek from beginning
1127 queue->current_index = 0;
1129 while (queue->current_index < queue->packets_size
1130 && queue->packets[queue->current_index].pts100 <= pts100) {
1131 lastpts = queue->packets[queue->current_index].pts100;
1132 ++queue->current_index;
1133 ++reseek_count;
1135 while (reseek_count-- && --queue->current_index) {
1136 if (queue->packets[queue->current_index-1].pts100 != UINT_MAX &&
1137 queue->packets[queue->current_index-1].pts100 != lastpts)
1138 break;
1142 int vobsub_get_packet(void *vobhandle, float pts, void** data, int* timestamp)
1144 vobsub_t *vob = (vobsub_t *)vobhandle;
1145 unsigned int pts100 = 90000 * pts;
1146 if (vob->spu_streams && 0 <= vobsub_id && (unsigned) vobsub_id < vob->spu_streams_size) {
1147 packet_queue_t *queue = vob->spu_streams + vobsub_id;
1149 vobsub_queue_reseek(queue, pts100);
1151 while (queue->current_index < queue->packets_size) {
1152 packet_t *pkt = queue->packets + queue->current_index;
1153 if (pkt->pts100 != UINT_MAX)
1154 if (pkt->pts100 <= pts100) {
1155 ++queue->current_index;
1156 *data = pkt->data;
1157 *timestamp = pkt->pts100;
1158 return pkt->size;
1159 } else
1160 break;
1161 else
1162 ++queue->current_index;
1165 return -1;
1168 int vobsub_get_next_packet(void *vobhandle, void** data, int* timestamp)
1170 vobsub_t *vob = (vobsub_t *)vobhandle;
1171 if (vob->spu_streams && 0 <= vobsub_id && (unsigned) vobsub_id < vob->spu_streams_size) {
1172 packet_queue_t *queue = vob->spu_streams + vobsub_id;
1173 if (queue->current_index < queue->packets_size) {
1174 packet_t *pkt = queue->packets + queue->current_index;
1175 ++queue->current_index;
1176 *data = pkt->data;
1177 *timestamp = pkt->pts100;
1178 return pkt->size;
1181 return -1;
1184 void vobsub_seek(void * vobhandle, float pts)
1186 vobsub_t * vob = (vobsub_t *)vobhandle;
1187 packet_queue_t * queue;
1188 int seek_pts100 = pts * 90000;
1190 if (vob->spu_streams && 0 <= vobsub_id && (unsigned) vobsub_id < vob->spu_streams_size) {
1191 /* do not seek if we don't know the id */
1192 if (vobsub_get_id(vob, vobsub_id) == NULL)
1193 return;
1194 queue = vob->spu_streams + vobsub_id;
1195 queue->current_index = 0;
1196 vobsub_queue_reseek(queue, seek_pts100);
1200 void vobsub_reset(void *vobhandle)
1202 vobsub_t *vob = (vobsub_t *)vobhandle;
1203 if (vob->spu_streams) {
1204 unsigned int n = vob->spu_streams_size;
1205 while (n-- > 0)
1206 vob->spu_streams[n].current_index = 0;
1210 /**********************************************************************
1211 * Vobsub output
1212 **********************************************************************/
1214 typedef struct {
1215 FILE *fsub;
1216 FILE *fidx;
1217 unsigned int aid;
1218 } vobsub_out_t;
1220 static void create_idx(vobsub_out_t *me, const unsigned int *palette,
1221 unsigned int orig_width, unsigned int orig_height)
1223 int i;
1224 fprintf(me->fidx,
1225 "# VobSub index file, v7 (do not modify this line!)\n"
1226 "#\n"
1227 "# Generated by MPlayer " VERSION "\n"
1228 "# See <URL:http://www.mplayerhq.hu/> for more information about MPlayer\n"
1229 "# See <URL:http://wiki.multimedia.cx/index.php?title=VOBsub> for more information about Vobsub\n"
1230 "#\n"
1231 "size: %ux%u\n",
1232 orig_width, orig_height);
1233 if (palette) {
1234 fputs("palette:", me->fidx);
1235 for (i = 0; i < 16; ++i) {
1236 const double y = palette[i] >> 16 & 0xff,
1237 u = (palette[i] >> 8 & 0xff) - 128.0,
1238 v = (palette[i] & 0xff) - 128.0;
1239 if (i)
1240 putc(',', me->fidx);
1241 fprintf(me->fidx, " %02x%02x%02x",
1242 av_clip_uint8(y + 1.4022 * u),
1243 av_clip_uint8(y - 0.3456 * u - 0.7145 * v),
1244 av_clip_uint8(y + 1.7710 * v));
1246 putc('\n', me->fidx);
1249 fprintf(me->fidx, "# ON: displays only forced subtitles, OFF: shows everything\n"
1250 "forced subs: OFF\n");
1253 void *vobsub_out_open(const char *basename, const unsigned int *palette,
1254 unsigned int orig_width, unsigned int orig_height,
1255 const char *id, unsigned int index)
1257 vobsub_out_t *result = NULL;
1258 char *filename;
1259 filename = malloc(strlen(basename) + 5);
1260 if (filename) {
1261 result = malloc(sizeof(vobsub_out_t));
1262 if (result) {
1263 result->aid = index;
1264 strcpy(filename, basename);
1265 strcat(filename, ".sub");
1266 result->fsub = fopen(filename, "ab");
1267 if (result->fsub == NULL)
1268 perror("Error: vobsub_out_open subtitle file open failed");
1269 strcpy(filename, basename);
1270 strcat(filename, ".idx");
1271 result->fidx = fopen(filename, "ab");
1272 if (result->fidx) {
1273 if (ftell(result->fidx) == 0) {
1274 create_idx(result, palette, orig_width, orig_height);
1275 /* Make the selected language the default language */
1276 fprintf(result->fidx, "\n# Language index in use\nlangidx: %u\n", index);
1278 fprintf(result->fidx, "\nid: %s, index: %u\n", id ? id : "xx", index);
1279 /* So that we can check the file now */
1280 fflush(result->fidx);
1281 } else
1282 perror("Error: vobsub_out_open index file open failed");
1283 free(filename);
1286 return result;
1289 void vobsub_out_close(void *me)
1291 vobsub_out_t *vob = (vobsub_out_t*)me;
1292 if (vob->fidx)
1293 fclose(vob->fidx);
1294 if (vob->fsub)
1295 fclose(vob->fsub);
1296 free(vob);
1299 void vobsub_out_output(void *me, const unsigned char *packet,
1300 int len, double pts)
1302 static double last_pts;
1303 static int last_pts_set = 0;
1304 vobsub_out_t *vob = (vobsub_out_t*)me;
1305 if (vob->fsub) {
1306 /* Windows' Vobsub require that every packet is exactly 2kB long */
1307 unsigned char buffer[2048];
1308 unsigned char *p;
1309 int remain = 2048;
1310 /* Do not output twice a line with the same timestamp, this
1311 breaks Windows' Vobsub */
1312 if (vob->fidx && (!last_pts_set || last_pts != pts)) {
1313 static unsigned int last_h = 9999, last_m = 9999, last_s = 9999, last_ms = 9999;
1314 unsigned int h, m, ms;
1315 double s;
1316 s = pts;
1317 h = s / 3600;
1318 s -= h * 3600;
1319 m = s / 60;
1320 s -= m * 60;
1321 ms = (s - (unsigned int) s) * 1000;
1322 if (ms >= 1000) /* prevent overfolws or bad float stuff */
1323 ms = 0;
1324 if (h != last_h || m != last_m || (unsigned int) s != last_s || ms != last_ms) {
1325 fprintf(vob->fidx, "timestamp: %02u:%02u:%02u:%03u, filepos: %09lx\n",
1326 h, m, (unsigned int) s, ms, ftell(vob->fsub));
1327 last_h = h;
1328 last_m = m;
1329 last_s = (unsigned int) s;
1330 last_ms = ms;
1333 last_pts = pts;
1334 last_pts_set = 1;
1336 /* Packet start code: Windows' Vobsub needs this */
1337 p = buffer;
1338 *p++ = 0; /* 0x00 */
1339 *p++ = 0;
1340 *p++ = 1;
1341 *p++ = 0xba;
1342 *p++ = 0x40;
1343 memset(p, 0, 9);
1344 p += 9;
1345 { /* Packet */
1346 static unsigned char last_pts[5] = { 0, 0, 0, 0, 0};
1347 unsigned char now_pts[5];
1348 int pts_len, pad_len, datalen = len;
1349 pts *= 90000;
1350 now_pts[0] = 0x21 | (((unsigned long)pts >> 29) & 0x0e);
1351 now_pts[1] = ((unsigned long)pts >> 22) & 0xff;
1352 now_pts[2] = 0x01 | (((unsigned long)pts >> 14) & 0xfe);
1353 now_pts[3] = ((unsigned long)pts >> 7) & 0xff;
1354 now_pts[4] = 0x01 | (((unsigned long)pts << 1) & 0xfe);
1355 pts_len = memcmp(last_pts, now_pts, sizeof(now_pts)) ? sizeof(now_pts) : 0;
1356 memcpy(last_pts, now_pts, sizeof(now_pts));
1358 datalen += 3; /* Version, PTS_flags, pts_len */
1359 datalen += pts_len;
1360 datalen += 1; /* AID */
1361 pad_len = 2048 - (p - buffer) - 4 /* MPEG ID */ - 2 /* payload len */ - datalen;
1362 /* XXX - Go figure what should go here! In any case the
1363 packet has to be completly filled. If I can fill it
1364 with padding (0x000001be) latter I'll do that. But if
1365 there is only room for 6 bytes then I can not write a
1366 padding packet. So I add some padding in the PTS
1367 field. This looks like a dirty kludge. Oh well... */
1368 if (pad_len < 0) {
1369 /* Packet is too big. Let's try ommiting the PTS field */
1370 datalen -= pts_len;
1371 pts_len = 0;
1372 pad_len = 0;
1373 } else if (pad_len > 6)
1374 pad_len = 0;
1375 datalen += pad_len;
1377 *p++ = 0; /* 0x0e */
1378 *p++ = 0;
1379 *p++ = 1;
1380 *p++ = 0xbd;
1382 *p++ = (datalen >> 8) & 0xff; /* length of payload */
1383 *p++ = datalen & 0xff;
1384 *p++ = 0x80; /* System-2 (.VOB) stream */
1385 *p++ = pts_len ? 0x80 : 0x00; /* pts_flags */
1386 *p++ = pts_len + pad_len;
1387 memcpy(p, now_pts, pts_len);
1388 p += pts_len;
1389 memset(p, 0, pad_len);
1390 p += pad_len;
1392 *p++ = 0x20 | vob->aid; /* aid */
1393 if (fwrite(buffer, p - buffer, 1, vob->fsub) != 1
1394 || fwrite(packet, len, 1, vob->fsub) != 1)
1395 perror("ERROR: vobsub write failed");
1396 else
1397 remain -= p - buffer + len;
1399 /* Padding */
1400 if (remain >= 6) {
1401 p = buffer;
1402 *p++ = 0x00;
1403 *p++ = 0x00;
1404 *p++ = 0x01;
1405 *p++ = 0xbe;
1406 *p++ = (remain - 6) >> 8;
1407 *p++ = (remain - 6) & 0xff;
1408 /* for better compression, blank this */
1409 memset(buffer + 6, 0, remain - (p - buffer));
1410 if (fwrite(buffer, remain, 1, vob->fsub) != 1)
1411 perror("ERROR: vobsub padding write failed");
1412 } else if (remain > 0) {
1413 /* I don't know what to output. But anyway the block
1414 needs to be 2KB big */
1415 memset(buffer, 0, remain);
1416 if (fwrite(buffer, remain, 1, vob->fsub) != 1)
1417 perror("ERROR: vobsub blank padding write failed");
1418 } else if (remain < 0)
1419 fprintf(stderr,
1420 "\nERROR: wrong thing happenned...\n"
1421 " I wrote a %i data bytes spu packet and that's too long\n", len);