Merge svn changes up to r30605
[mplayer/kovensky.git] / vobsub.c
blobeeb8592607c6fa22af2f28bd588414d55e09764a
1 /*
2 * Some code freely inspired from VobSub <URL:http://vobsub.edensrising.com>,
3 * with kind permission from Gabest <gabest@freemail.hu>
5 * This file is part of MPlayer.
7 * MPlayer is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * MPlayer is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <ctype.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <stddef.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
34 #include "config.h"
35 #include "version.h"
37 #include "vobsub.h"
38 #include "spudec.h"
39 #include "mp_msg.h"
40 #include "unrar_exec.h"
41 #include "libavutil/common.h"
43 extern int vobsub_id;
44 // Record the original -vobsubid set by commandline, since vobsub_id will be
45 // overridden if slang match any of vobsub streams.
46 static int vobsubid = -2;
48 /**********************************************************************
49 * RAR stream handling
50 * The RAR file must have the same basename as the file to open
51 **********************************************************************/
52 #ifdef CONFIG_UNRAR_EXEC
53 typedef struct {
54 FILE *file;
55 unsigned char *data;
56 unsigned long size;
57 unsigned long pos;
58 } rar_stream_t;
60 static rar_stream_t *rar_open(const char *const filename,
61 const char *const mode)
63 rar_stream_t *stream;
64 /* unrar_exec can only read */
65 if (strcmp("r", mode) && strcmp("rb", mode)) {
66 errno = EINVAL;
67 return NULL;
69 stream = malloc(sizeof(rar_stream_t));
70 if (stream == NULL)
71 return NULL;
72 /* first try normal access */
73 stream->file = fopen(filename, mode);
74 if (stream->file == NULL) {
75 char *rar_filename;
76 const char *p;
77 int rc;
78 /* Guess the RAR archive filename */
79 rar_filename = NULL;
80 p = strrchr(filename, '.');
81 if (p) {
82 ptrdiff_t l = p - filename;
83 rar_filename = malloc(l + 5);
84 if (rar_filename == NULL) {
85 free(stream);
86 return NULL;
88 strncpy(rar_filename, filename, l);
89 strcpy(rar_filename + l, ".rar");
90 } else {
91 rar_filename = malloc(strlen(filename) + 5);
92 if (rar_filename == NULL) {
93 free(stream);
94 return NULL;
96 strcpy(rar_filename, filename);
97 strcat(rar_filename, ".rar");
99 /* get rid of the path if there is any */
100 if ((p = strrchr(filename, '/')) == NULL) {
101 p = filename;
102 } else {
103 p++;
105 rc = unrar_exec_get(&stream->data, &stream->size, p, rar_filename);
106 if (!rc) {
107 /* There is no matching filename in the archive. However, sometimes
108 * the files we are looking for have been given arbitrary names in the archive.
109 * Let's look for a file with an exact match in the extension only. */
110 int i, num_files, name_len;
111 ArchiveList_struct *list, *lp;
112 num_files = unrar_exec_list(rar_filename, &list);
113 if (num_files > 0) {
114 char *demanded_ext;
115 demanded_ext = strrchr (p, '.');
116 if (demanded_ext) {
117 int demanded_ext_len = strlen (demanded_ext);
118 for (i = 0, lp = list; i < num_files; i++, lp = lp->next) {
119 name_len = strlen (lp->item.Name);
120 if (name_len >= demanded_ext_len && !strcasecmp (lp->item.Name + name_len - demanded_ext_len, demanded_ext)) {
121 rc = unrar_exec_get(&stream->data, &stream->size,
122 lp->item.Name, rar_filename);
123 if (rc)
124 break;
128 unrar_exec_freelist(list);
130 if (!rc) {
131 free(rar_filename);
132 free(stream);
133 return NULL;
137 free(rar_filename);
138 stream->pos = 0;
140 return stream;
143 static int rar_close(rar_stream_t *stream)
145 if (stream->file)
146 return fclose(stream->file);
147 free(stream->data);
148 return 0;
151 static int rar_eof(rar_stream_t *stream)
153 if (stream->file)
154 return feof(stream->file);
155 return stream->pos >= stream->size;
158 static long rar_tell(rar_stream_t *stream)
160 if (stream->file)
161 return ftell(stream->file);
162 return stream->pos;
165 static int rar_seek(rar_stream_t *stream, long offset, int whence)
167 if (stream->file)
168 return fseek(stream->file, offset, whence);
169 switch (whence) {
170 case SEEK_SET:
171 if (offset < 0) {
172 errno = EINVAL;
173 return -1;
175 stream->pos = offset;
176 break;
177 case SEEK_CUR:
178 if (offset < 0 && stream->pos < (unsigned long) -offset) {
179 errno = EINVAL;
180 return -1;
182 stream->pos += offset;
183 break;
184 case SEEK_END:
185 if (offset < 0 && stream->size < (unsigned long) -offset) {
186 errno = EINVAL;
187 return -1;
189 stream->pos = stream->size + offset;
190 break;
191 default:
192 errno = EINVAL;
193 return -1;
195 return 0;
198 static int rar_getc(rar_stream_t *stream)
200 if (stream->file)
201 return getc(stream->file);
202 if (rar_eof(stream))
203 return EOF;
204 return stream->data[stream->pos++];
207 static size_t rar_read(void *ptr, size_t size, size_t nmemb,
208 rar_stream_t *stream)
210 size_t res;
211 unsigned long remain;
212 if (stream->file)
213 return fread(ptr, size, nmemb, stream->file);
214 if (rar_eof(stream))
215 return 0;
216 res = size * nmemb;
217 remain = stream->size - stream->pos;
218 if (res > remain)
219 res = remain / size * size;
220 memcpy(ptr, stream->data + stream->pos, res);
221 stream->pos += res;
222 res /= size;
223 return res;
226 #else
227 typedef FILE rar_stream_t;
228 #define rar_open fopen
229 #define rar_close fclose
230 #define rar_eof feof
231 #define rar_tell ftell
232 #define rar_seek fseek
233 #define rar_getc getc
234 #define rar_read fread
235 #endif
237 /**********************************************************************/
239 static ssize_t vobsub_getline(char **lineptr, size_t *n, rar_stream_t *stream)
241 size_t res = 0;
242 int c;
243 if (*lineptr == NULL) {
244 *lineptr = malloc(4096);
245 if (*lineptr)
246 *n = 4096;
247 } else if (*n == 0) {
248 char *tmp = realloc(*lineptr, 4096);
249 if (tmp) {
250 *lineptr = tmp;
251 *n = 4096;
254 if (*lineptr == NULL || *n == 0)
255 return -1;
257 for (c = rar_getc(stream); c != EOF; c = rar_getc(stream)) {
258 if (res + 1 >= *n) {
259 char *tmp = realloc(*lineptr, *n * 2);
260 if (tmp == NULL)
261 return -1;
262 *lineptr = tmp;
263 *n *= 2;
265 (*lineptr)[res++] = c;
266 if (c == '\n') {
267 (*lineptr)[res] = 0;
268 return res;
271 if (res == 0)
272 return -1;
273 (*lineptr)[res] = 0;
274 return res;
277 /**********************************************************************
278 * MPEG parsing
279 **********************************************************************/
281 typedef struct {
282 rar_stream_t *stream;
283 unsigned int pts;
284 int aid;
285 unsigned char *packet;
286 unsigned int packet_reserve;
287 unsigned int packet_size;
288 } mpeg_t;
290 static mpeg_t *mpeg_open(const char *filename)
292 mpeg_t *res = malloc(sizeof(mpeg_t));
293 int err = res == NULL;
294 if (!err) {
295 res->pts = 0;
296 res->aid = -1;
297 res->packet = NULL;
298 res->packet_size = 0;
299 res->packet_reserve = 0;
300 res->stream = rar_open(filename, "rb");
301 err = res->stream == NULL;
302 if (err)
303 perror("fopen Vobsub file failed");
304 if (err)
305 free(res);
307 return err ? NULL : res;
310 static void mpeg_free(mpeg_t *mpeg)
312 if (mpeg->packet)
313 free(mpeg->packet);
314 if (mpeg->stream)
315 rar_close(mpeg->stream);
316 free(mpeg);
319 static int mpeg_eof(mpeg_t *mpeg)
321 return rar_eof(mpeg->stream);
324 static off_t mpeg_tell(mpeg_t *mpeg)
326 return rar_tell(mpeg->stream);
329 static int mpeg_run(mpeg_t *mpeg)
331 unsigned int len, idx, version;
332 int c;
333 /* Goto start of a packet, it starts with 0x000001?? */
334 const unsigned char wanted[] = { 0, 0, 1 };
335 unsigned char buf[5];
337 mpeg->aid = -1;
338 mpeg->packet_size = 0;
339 if (rar_read(buf, 4, 1, mpeg->stream) != 1)
340 return -1;
341 while (memcmp(buf, wanted, sizeof(wanted)) != 0) {
342 c = rar_getc(mpeg->stream);
343 if (c < 0)
344 return -1;
345 memmove(buf, buf + 1, 3);
346 buf[3] = c;
348 switch (buf[3]) {
349 case 0xb9: /* System End Code */
350 break;
351 case 0xba: /* Packet start code */
352 c = rar_getc(mpeg->stream);
353 if (c < 0)
354 return -1;
355 if ((c & 0xc0) == 0x40)
356 version = 4;
357 else if ((c & 0xf0) == 0x20)
358 version = 2;
359 else {
360 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Unsupported MPEG version: 0x%02x\n", c);
361 return -1;
363 if (version == 4) {
364 if (rar_seek(mpeg->stream, 9, SEEK_CUR))
365 return -1;
366 } else if (version == 2) {
367 if (rar_seek(mpeg->stream, 7, SEEK_CUR))
368 return -1;
369 } else
370 abort();
371 break;
372 case 0xbd: /* packet */
373 if (rar_read(buf, 2, 1, mpeg->stream) != 1)
374 return -1;
375 len = buf[0] << 8 | buf[1];
376 idx = mpeg_tell(mpeg);
377 c = rar_getc(mpeg->stream);
378 if (c < 0)
379 return -1;
380 if ((c & 0xC0) == 0x40) { /* skip STD scale & size */
381 if (rar_getc(mpeg->stream) < 0)
382 return -1;
383 c = rar_getc(mpeg->stream);
384 if (c < 0)
385 return -1;
387 if ((c & 0xf0) == 0x20) { /* System-1 stream timestamp */
388 /* Do we need this? */
389 abort();
390 } else if ((c & 0xf0) == 0x30) {
391 /* Do we need this? */
392 abort();
393 } else if ((c & 0xc0) == 0x80) { /* System-2 (.VOB) stream */
394 unsigned int pts_flags, hdrlen, dataidx;
395 c = rar_getc(mpeg->stream);
396 if (c < 0)
397 return -1;
398 pts_flags = c;
399 c = rar_getc(mpeg->stream);
400 if (c < 0)
401 return -1;
402 hdrlen = c;
403 dataidx = mpeg_tell(mpeg) + hdrlen;
404 if (dataidx > idx + len) {
405 mp_msg(MSGT_VOBSUB, MSGL_ERR, "Invalid header length: %d (total length: %d, idx: %d, dataidx: %d)\n",
406 hdrlen, len, idx, dataidx);
407 return -1;
409 if ((pts_flags & 0xc0) == 0x80) {
410 if (rar_read(buf, 5, 1, mpeg->stream) != 1)
411 return -1;
412 if (!(((buf[0] & 0xf0) == 0x20) && (buf[0] & 1) && (buf[2] & 1) && (buf[4] & 1))) {
413 mp_msg(MSGT_VOBSUB, MSGL_ERR, "vobsub PTS error: 0x%02x %02x%02x %02x%02x \n",
414 buf[0], buf[1], buf[2], buf[3], buf[4]);
415 mpeg->pts = 0;
416 } else
417 mpeg->pts = ((buf[0] & 0x0e) << 29 | buf[1] << 22 | (buf[2] & 0xfe) << 14
418 | buf[3] << 7 | (buf[4] >> 1));
419 } else /* if ((pts_flags & 0xc0) == 0xc0) */ {
420 /* what's this? */
421 /* abort(); */
423 rar_seek(mpeg->stream, dataidx, SEEK_SET);
424 mpeg->aid = rar_getc(mpeg->stream);
425 if (mpeg->aid < 0) {
426 mp_msg(MSGT_VOBSUB, MSGL_ERR, "Bogus aid %d\n", mpeg->aid);
427 return -1;
429 mpeg->packet_size = len - ((unsigned int) mpeg_tell(mpeg) - idx);
430 if (mpeg->packet_reserve < mpeg->packet_size) {
431 if (mpeg->packet)
432 free(mpeg->packet);
433 mpeg->packet = malloc(mpeg->packet_size);
434 if (mpeg->packet)
435 mpeg->packet_reserve = mpeg->packet_size;
437 if (mpeg->packet == NULL) {
438 mp_msg(MSGT_VOBSUB, MSGL_FATAL, "malloc failure");
439 mpeg->packet_reserve = 0;
440 mpeg->packet_size = 0;
441 return -1;
443 if (rar_read(mpeg->packet, mpeg->packet_size, 1, mpeg->stream) != 1) {
444 mp_msg(MSGT_VOBSUB, MSGL_ERR, "fread failure");
445 mpeg->packet_size = 0;
446 return -1;
448 idx = len;
450 break;
451 case 0xbe: /* Padding */
452 if (rar_read(buf, 2, 1, mpeg->stream) != 1)
453 return -1;
454 len = buf[0] << 8 | buf[1];
455 if (len > 0 && rar_seek(mpeg->stream, len, SEEK_CUR))
456 return -1;
457 break;
458 default:
459 if (0xc0 <= buf[3] && buf[3] < 0xf0) {
460 /* MPEG audio or video */
461 if (rar_read(buf, 2, 1, mpeg->stream) != 1)
462 return -1;
463 len = buf[0] << 8 | buf[1];
464 if (len > 0 && rar_seek(mpeg->stream, len, SEEK_CUR))
465 return -1;
466 } else {
467 mp_msg(MSGT_VOBSUB, MSGL_ERR, "unknown header 0x%02X%02X%02X%02X\n",
468 buf[0], buf[1], buf[2], buf[3]);
469 return -1;
472 return 0;
475 /**********************************************************************
476 * Packet queue
477 **********************************************************************/
479 typedef struct {
480 unsigned int pts100;
481 off_t filepos;
482 unsigned int size;
483 unsigned char *data;
484 } packet_t;
486 typedef struct {
487 char *id;
488 packet_t *packets;
489 unsigned int packets_reserve;
490 unsigned int packets_size;
491 unsigned int current_index;
492 } packet_queue_t;
494 static void packet_construct(packet_t *pkt)
496 pkt->pts100 = 0;
497 pkt->filepos = 0;
498 pkt->size = 0;
499 pkt->data = NULL;
502 static void packet_destroy(packet_t *pkt)
504 if (pkt->data)
505 free(pkt->data);
508 static void packet_queue_construct(packet_queue_t *queue)
510 queue->id = NULL;
511 queue->packets = NULL;
512 queue->packets_reserve = 0;
513 queue->packets_size = 0;
514 queue->current_index = 0;
517 static void packet_queue_destroy(packet_queue_t *queue)
519 if (queue->packets) {
520 while (queue->packets_size--)
521 packet_destroy(queue->packets + queue->packets_size);
522 free(queue->packets);
524 return;
527 /* Make sure there is enough room for needed_size packets in the
528 packet queue. */
529 static int packet_queue_ensure(packet_queue_t *queue, unsigned int needed_size)
531 if (queue->packets_reserve < needed_size) {
532 if (queue->packets) {
533 packet_t *tmp = realloc(queue->packets, 2 * queue->packets_reserve * sizeof(packet_t));
534 if (tmp == NULL) {
535 mp_msg(MSGT_VOBSUB, MSGL_FATAL, "realloc failure");
536 return -1;
538 queue->packets = tmp;
539 queue->packets_reserve *= 2;
540 } else {
541 queue->packets = malloc(sizeof(packet_t));
542 if (queue->packets == NULL) {
543 mp_msg(MSGT_VOBSUB, MSGL_FATAL, "malloc failure");
544 return -1;
546 queue->packets_reserve = 1;
549 return 0;
552 /* add one more packet */
553 static int packet_queue_grow(packet_queue_t *queue)
555 if (packet_queue_ensure(queue, queue->packets_size + 1) < 0)
556 return -1;
557 packet_construct(queue->packets + queue->packets_size);
558 ++queue->packets_size;
559 return 0;
562 /* insert a new packet, duplicating pts from the current one */
563 static int packet_queue_insert(packet_queue_t *queue)
565 packet_t *pkts;
566 if (packet_queue_ensure(queue, queue->packets_size + 1) < 0)
567 return -1;
568 /* XXX packet_size does not reflect the real thing here, it will be updated a bit later */
569 memmove(queue->packets + queue->current_index + 2,
570 queue->packets + queue->current_index + 1,
571 sizeof(packet_t) * (queue->packets_size - queue->current_index - 1));
572 pkts = queue->packets + queue->current_index;
573 ++queue->packets_size;
574 ++queue->current_index;
575 packet_construct(pkts + 1);
576 pkts[1].pts100 = pkts[0].pts100;
577 pkts[1].filepos = pkts[0].filepos;
578 return 0;
581 /**********************************************************************
582 * Vobsub
583 **********************************************************************/
585 typedef struct {
586 unsigned int palette[16];
587 int delay;
588 unsigned int have_palette;
589 unsigned int orig_frame_width, orig_frame_height;
590 unsigned int origin_x, origin_y;
591 /* index */
592 packet_queue_t *spu_streams;
593 unsigned int spu_streams_size;
594 unsigned int spu_streams_current;
595 unsigned int spu_valid_streams_size;
596 } vobsub_t;
598 /* Make sure that the spu stream idx exists. */
599 static int vobsub_ensure_spu_stream(vobsub_t *vob, unsigned int index)
601 if (index >= vob->spu_streams_size) {
602 /* This is a new stream */
603 if (vob->spu_streams) {
604 packet_queue_t *tmp = realloc(vob->spu_streams, (index + 1) * sizeof(packet_queue_t));
605 if (tmp == NULL) {
606 mp_msg(MSGT_VOBSUB, MSGL_ERR, "vobsub_ensure_spu_stream: realloc failure");
607 return -1;
609 vob->spu_streams = tmp;
610 } else {
611 vob->spu_streams = malloc((index + 1) * sizeof(packet_queue_t));
612 if (vob->spu_streams == NULL) {
613 mp_msg(MSGT_VOBSUB, MSGL_ERR, "vobsub_ensure_spu_stream: malloc failure");
614 return -1;
617 while (vob->spu_streams_size <= index) {
618 packet_queue_construct(vob->spu_streams + vob->spu_streams_size);
619 ++vob->spu_streams_size;
622 return 0;
625 static int vobsub_add_id(vobsub_t *vob, const char *id, size_t idlen,
626 const unsigned int index)
628 if (vobsub_ensure_spu_stream(vob, index) < 0)
629 return -1;
630 if (id && idlen) {
631 if (vob->spu_streams[index].id)
632 free(vob->spu_streams[index].id);
633 vob->spu_streams[index].id = malloc(idlen + 1);
634 if (vob->spu_streams[index].id == NULL) {
635 mp_msg(MSGT_VOBSUB, MSGL_FATAL, "vobsub_add_id: malloc failure");
636 return -1;
638 vob->spu_streams[index].id[idlen] = 0;
639 memcpy(vob->spu_streams[index].id, id, idlen);
641 vob->spu_streams_current = index;
642 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VOBSUB_ID=%d\n", index);
643 if (id && idlen)
644 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VSID_%d_LANG=%s\n", index, vob->spu_streams[index].id);
645 mp_msg(MSGT_VOBSUB, MSGL_V, "[vobsub] subtitle (vobsubid): %d language %s\n",
646 index, vob->spu_streams[index].id);
647 return 0;
650 static int vobsub_add_timestamp(vobsub_t *vob, off_t filepos, int ms)
652 packet_queue_t *queue;
653 packet_t *pkt;
654 if (vob->spu_streams == 0) {
655 mp_msg(MSGT_VOBSUB, MSGL_WARN, "[vobsub] warning, binning some index entries. Check your index file\n");
656 return -1;
658 queue = vob->spu_streams + vob->spu_streams_current;
659 if (packet_queue_grow(queue) >= 0) {
660 pkt = queue->packets + (queue->packets_size - 1);
661 pkt->filepos = filepos;
662 pkt->pts100 = ms < 0 ? UINT_MAX : (unsigned int)ms * 90;
663 return 0;
665 return -1;
668 static int vobsub_parse_id(vobsub_t *vob, const char *line)
670 // id: xx, index: n
671 size_t idlen;
672 const char *p, *q;
673 p = line;
674 while (isspace(*p))
675 ++p;
676 q = p;
677 while (isalpha(*q))
678 ++q;
679 idlen = q - p;
680 if (idlen == 0)
681 return -1;
682 ++q;
683 while (isspace(*q))
684 ++q;
685 if (strncmp("index:", q, 6))
686 return -1;
687 q += 6;
688 while (isspace(*q))
689 ++q;
690 if (!isdigit(*q))
691 return -1;
692 return vobsub_add_id(vob, p, idlen, atoi(q));
695 static int vobsub_parse_timestamp(vobsub_t *vob, const char *line)
697 // timestamp: HH:MM:SS.mmm, filepos: 0nnnnnnnnn
698 const char *p;
699 int h, m, s, ms;
700 off_t filepos;
701 while (isspace(*line))
702 ++line;
703 p = line;
704 while (isdigit(*p))
705 ++p;
706 if (p - line != 2)
707 return -1;
708 h = atoi(line);
709 if (*p != ':')
710 return -1;
711 line = ++p;
712 while (isdigit(*p))
713 ++p;
714 if (p - line != 2)
715 return -1;
716 m = atoi(line);
717 if (*p != ':')
718 return -1;
719 line = ++p;
720 while (isdigit(*p))
721 ++p;
722 if (p - line != 2)
723 return -1;
724 s = atoi(line);
725 if (*p != ':')
726 return -1;
727 line = ++p;
728 while (isdigit(*p))
729 ++p;
730 if (p - line != 3)
731 return -1;
732 ms = atoi(line);
733 if (*p != ',')
734 return -1;
735 line = p + 1;
736 while (isspace(*line))
737 ++line;
738 if (strncmp("filepos:", line, 8))
739 return -1;
740 line += 8;
741 while (isspace(*line))
742 ++line;
743 if (! isxdigit(*line))
744 return -1;
745 filepos = strtol(line, NULL, 16);
746 return vobsub_add_timestamp(vob, filepos, vob->delay + ms + 1000 * (s + 60 * (m + 60 * h)));
749 static int vobsub_parse_origin(vobsub_t *vob, const char *line)
751 // org: X,Y
752 char *p;
753 while (isspace(*line))
754 ++line;
755 if (!isdigit(*line))
756 return -1;
757 vob->origin_x = strtoul(line, &p, 10);
758 if (*p != ',')
759 return -1;
760 ++p;
761 vob->origin_y = strtoul(p, NULL, 10);
762 return 0;
765 unsigned int vobsub_palette_to_yuv(unsigned int pal)
767 int r, g, b, y, u, v;
768 // Palette in idx file is not rgb value, it was calculated by wrong formula.
769 // Here's reversed formula of the one used to generate palette in idx file.
770 r = pal >> 16 & 0xff;
771 g = pal >> 8 & 0xff;
772 b = pal & 0xff;
773 y = av_clip_uint8( 0.1494 * r + 0.6061 * g + 0.2445 * b);
774 u = av_clip_uint8( 0.6066 * r - 0.4322 * g - 0.1744 * b + 128);
775 v = av_clip_uint8(-0.08435 * r - 0.3422 * g + 0.4266 * b + 128);
776 y = y * 219 / 255 + 16;
777 return y << 16 | u << 8 | v;
780 unsigned int vobsub_rgb_to_yuv(unsigned int rgb)
782 int r, g, b, y, u, v;
783 r = rgb >> 16 & 0xff;
784 g = rgb >> 8 & 0xff;
785 b = rgb & 0xff;
786 y = ( 0.299 * r + 0.587 * g + 0.114 * b) * 219 / 255 + 16.5;
787 u = (-0.16874 * r - 0.33126 * g + 0.5 * b) * 224 / 255 + 128.5;
788 v = ( 0.5 * r - 0.41869 * g - 0.08131 * b) * 224 / 255 + 128.5;
789 return y << 16 | u << 8 | v;
792 static int vobsub_parse_delay(vobsub_t *vob, const char *line)
794 int h, m, s, ms;
795 int forward = 1;
796 if (*(line + 7) == '+') {
797 forward = 1;
798 line++;
799 } else if (*(line + 7) == '-') {
800 forward = -1;
801 line++;
803 mp_msg(MSGT_SPUDEC, MSGL_V, "forward=%d", forward);
804 h = atoi(line + 7);
805 mp_msg(MSGT_VOBSUB, MSGL_V, "h=%d,", h);
806 m = atoi(line + 10);
807 mp_msg(MSGT_VOBSUB, MSGL_V, "m=%d,", m);
808 s = atoi(line + 13);
809 mp_msg(MSGT_VOBSUB, MSGL_V, "s=%d,", s);
810 ms = atoi(line + 16);
811 mp_msg(MSGT_VOBSUB, MSGL_V, "ms=%d", ms);
812 vob->delay = (ms + 1000 * (s + 60 * (m + 60 * h))) * forward;
813 return 0;
816 static int vobsub_set_lang(const char *line)
818 if (vobsub_id == -1)
819 vobsub_id = atoi(line + 8);
820 return 0;
823 static int vobsub_parse_one_line(vobsub_t *vob, rar_stream_t *fd,
824 unsigned char **extradata,
825 unsigned int *extradata_len)
827 ssize_t line_size;
828 int res = -1;
829 size_t line_reserve = 0;
830 char *line = NULL;
831 do {
832 line_size = vobsub_getline(&line, &line_reserve, fd);
833 if (line_size < 0 || line_size > 1000000 ||
834 *extradata_len+line_size > 10000000) {
835 break;
838 *extradata = realloc(*extradata, *extradata_len+line_size+1);
839 memcpy(*extradata+*extradata_len, line, line_size);
840 *extradata_len += line_size;
841 (*extradata)[*extradata_len] = 0;
843 if (*line == 0 || *line == '\r' || *line == '\n' || *line == '#')
844 continue;
845 else if (strncmp("langidx:", line, 8) == 0)
846 res = vobsub_set_lang(line);
847 else if (strncmp("delay:", line, 6) == 0)
848 res = vobsub_parse_delay(vob, line);
849 else if (strncmp("id:", line, 3) == 0)
850 res = vobsub_parse_id(vob, line + 3);
851 else if (strncmp("org:", line, 4) == 0)
852 res = vobsub_parse_origin(vob, line + 4);
853 else if (strncmp("timestamp:", line, 10) == 0)
854 res = vobsub_parse_timestamp(vob, line + 10);
855 else {
856 mp_msg(MSGT_VOBSUB, MSGL_V, "vobsub: ignoring %s", line);
857 continue;
859 if (res < 0)
860 mp_msg(MSGT_VOBSUB, MSGL_ERR, "ERROR in %s", line);
861 break;
862 } while (1);
863 if (line)
864 free(line);
865 return res;
868 int vobsub_parse_ifo(void* this, const char *const name, unsigned int *palette,
869 unsigned int *width, unsigned int *height, int force,
870 int sid, char *langid)
872 vobsub_t *vob = (vobsub_t*)this;
873 int res = -1;
874 rar_stream_t *fd = rar_open(name, "rb");
875 if (fd == NULL) {
876 if (force)
877 mp_msg(MSGT_VOBSUB, MSGL_WARN, "VobSub: Can't open IFO file\n");
878 } else {
879 // parse IFO header
880 unsigned char block[0x800];
881 const char *const ifo_magic = "DVDVIDEO-VTS";
882 if (rar_read(block, sizeof(block), 1, fd) != 1) {
883 if (force)
884 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Can't read IFO header\n");
885 } else if (memcmp(block, ifo_magic, strlen(ifo_magic) + 1))
886 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Bad magic in IFO header\n");
887 else {
888 unsigned long pgci_sector = block[0xcc] << 24 | block[0xcd] << 16
889 | block[0xce] << 8 | block[0xcf];
890 int standard = (block[0x200] & 0x30) >> 4;
891 int resolution = (block[0x201] & 0x0c) >> 2;
892 *height = standard ? 576 : 480;
893 *width = 0;
894 switch (resolution) {
895 case 0x0:
896 *width = 720;
897 break;
898 case 0x1:
899 *width = 704;
900 break;
901 case 0x2:
902 *width = 352;
903 break;
904 case 0x3:
905 *width = 352;
906 *height /= 2;
907 break;
908 default:
909 mp_msg(MSGT_VOBSUB, MSGL_WARN, "Vobsub: Unknown resolution %d \n", resolution);
911 if (langid && 0 <= sid && sid < 32) {
912 unsigned char *tmp = block + 0x256 + sid * 6 + 2;
913 langid[0] = tmp[0];
914 langid[1] = tmp[1];
915 langid[2] = 0;
917 if (rar_seek(fd, pgci_sector * sizeof(block), SEEK_SET)
918 || rar_read(block, sizeof(block), 1, fd) != 1)
919 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Can't read IFO PGCI\n");
920 else {
921 unsigned long idx;
922 unsigned long pgc_offset = block[0xc] << 24 | block[0xd] << 16
923 | block[0xe] << 8 | block[0xf];
924 for (idx = 0; idx < 16; ++idx) {
925 unsigned char *p = block + pgc_offset + 0xa4 + 4 * idx;
926 palette[idx] = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
928 if (vob)
929 vob->have_palette = 1;
930 res = 0;
933 rar_close(fd);
935 return res;
938 void *vobsub_open(const char *const name, const char *const ifo,
939 const int force, void** spu)
941 unsigned char *extradata = NULL;
942 unsigned int extradata_len = 0;
943 vobsub_t *vob = calloc(1, sizeof(vobsub_t));
944 if (spu)
945 *spu = NULL;
946 if (vobsubid == -2)
947 vobsubid = vobsub_id;
948 if (vob) {
949 char *buf;
950 buf = malloc(strlen(name) + 5);
951 if (buf) {
952 rar_stream_t *fd;
953 mpeg_t *mpg;
954 /* read in the info file */
955 if (!ifo) {
956 strcpy(buf, name);
957 strcat(buf, ".ifo");
958 vobsub_parse_ifo(vob, buf, vob->palette, &vob->orig_frame_width, &vob->orig_frame_height, force, -1, NULL);
959 } else
960 vobsub_parse_ifo(vob, ifo, vob->palette, &vob->orig_frame_width, &vob->orig_frame_height, force, -1, NULL);
961 /* read in the index */
962 strcpy(buf, name);
963 strcat(buf, ".idx");
964 fd = rar_open(buf, "rb");
965 if (fd == NULL) {
966 if (force)
967 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Can't open IDX file\n");
968 else {
969 free(buf);
970 free(vob);
971 return NULL;
973 } else {
974 while (vobsub_parse_one_line(vob, fd, &extradata, &extradata_len) >= 0)
975 /* NOOP */ ;
976 rar_close(fd);
978 if (spu)
979 *spu = spudec_new_scaled(vob->palette, vob->orig_frame_width, vob->orig_frame_height, extradata, extradata_len);
980 if (extradata)
981 free(extradata);
983 /* read the indexed mpeg_stream */
984 strcpy(buf, name);
985 strcat(buf, ".sub");
986 mpg = mpeg_open(buf);
987 if (mpg == NULL) {
988 if (force)
989 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Can't open SUB file\n");
990 else {
991 free(buf);
992 free(vob);
993 return NULL;
995 } else {
996 long last_pts_diff = 0;
997 while (!mpeg_eof(mpg)) {
998 off_t pos = mpeg_tell(mpg);
999 if (mpeg_run(mpg) < 0) {
1000 if (!mpeg_eof(mpg))
1001 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: mpeg_run error\n");
1002 break;
1004 if (mpg->packet_size) {
1005 if ((mpg->aid & 0xe0) == 0x20) {
1006 unsigned int sid = mpg->aid & 0x1f;
1007 if (vobsub_ensure_spu_stream(vob, sid) >= 0) {
1008 packet_queue_t *queue = vob->spu_streams + sid;
1009 /* get the packet to fill */
1010 if (queue->packets_size == 0 && packet_queue_grow(queue) < 0)
1011 abort();
1012 while (queue->current_index + 1 < queue->packets_size
1013 && queue->packets[queue->current_index + 1].filepos <= pos)
1014 ++queue->current_index;
1015 if (queue->current_index < queue->packets_size) {
1016 packet_t *pkt;
1017 if (queue->packets[queue->current_index].data) {
1018 /* insert a new packet and fix the PTS ! */
1019 packet_queue_insert(queue);
1020 queue->packets[queue->current_index].pts100 =
1021 mpg->pts + last_pts_diff;
1023 pkt = queue->packets + queue->current_index;
1024 if (pkt->pts100 != UINT_MAX) {
1025 if (queue->packets_size > 1)
1026 last_pts_diff = pkt->pts100 - mpg->pts;
1027 else
1028 pkt->pts100 = mpg->pts;
1029 /* FIXME: should not use mpg_sub internal informations, make a copy */
1030 pkt->data = mpg->packet;
1031 pkt->size = mpg->packet_size;
1032 mpg->packet = NULL;
1033 mpg->packet_reserve = 0;
1034 mpg->packet_size = 0;
1037 } else
1038 mp_msg(MSGT_VOBSUB, MSGL_WARN, "don't know what to do with subtitle #%u\n", sid);
1042 vob->spu_streams_current = vob->spu_streams_size;
1043 while (vob->spu_streams_current-- > 0) {
1044 vob->spu_streams[vob->spu_streams_current].current_index = 0;
1045 if (vobsubid == vob->spu_streams_current ||
1046 vob->spu_streams[vob->spu_streams_current].packets_size > 0)
1047 ++vob->spu_valid_streams_size;
1049 mpeg_free(mpg);
1051 free(buf);
1054 return vob;
1057 void vobsub_close(void *this)
1059 vobsub_t *vob = (vobsub_t *)this;
1060 if (vob->spu_streams) {
1061 while (vob->spu_streams_size--)
1062 packet_queue_destroy(vob->spu_streams + vob->spu_streams_size);
1063 free(vob->spu_streams);
1065 free(vob);
1068 unsigned int vobsub_get_indexes_count(void *vobhandle)
1070 vobsub_t *vob = (vobsub_t *) vobhandle;
1071 return vob->spu_valid_streams_size;
1074 char *vobsub_get_id(void *vobhandle, unsigned int index)
1076 vobsub_t *vob = (vobsub_t *) vobhandle;
1077 return (index < vob->spu_streams_size) ? vob->spu_streams[index].id : NULL;
1080 int vobsub_get_id_by_index(void *vobhandle, unsigned int index)
1082 vobsub_t *vob = vobhandle;
1083 int i, j;
1084 if (vob == NULL)
1085 return -1;
1086 for (i = 0, j = 0; i < vob->spu_streams_size; ++i)
1087 if (i == vobsubid || vob->spu_streams[i].packets_size > 0) {
1088 if (j == index)
1089 return i;
1090 ++j;
1092 return -1;
1095 int vobsub_get_index_by_id(void *vobhandle, int id)
1097 vobsub_t *vob = vobhandle;
1098 int i, j;
1099 if (vob == NULL || id < 0 || id >= vob->spu_streams_size)
1100 return -1;
1101 if (id != vobsubid && !vob->spu_streams[id].packets_size)
1102 return -1;
1103 for (i = 0, j = 0; i < id; ++i)
1104 if (i == vobsubid || vob->spu_streams[i].packets_size > 0)
1105 ++j;
1106 return j;
1109 int vobsub_set_from_lang(void *vobhandle, unsigned char * lang)
1111 int i;
1112 vobsub_t *vob= (vobsub_t *) vobhandle;
1113 while (lang && strlen(lang) >= 2) {
1114 for (i = 0; i < vob->spu_streams_size; i++)
1115 if (vob->spu_streams[i].id)
1116 if ((strncmp(vob->spu_streams[i].id, lang, 2) == 0)) {
1117 vobsub_id = i;
1118 mp_msg(MSGT_VOBSUB, MSGL_INFO, "Selected VOBSUB language: %d language: %s\n", i, vob->spu_streams[i].id);
1119 return 0;
1121 lang+=2;while (lang[0]==',' || lang[0]==' ') ++lang;
1123 mp_msg(MSGT_VOBSUB, MSGL_WARN, "No matching VOBSUB language found!\n");
1124 return -1;
1127 /// make sure we seek to the first packet of packets having same pts values.
1128 static void vobsub_queue_reseek(packet_queue_t *queue, unsigned int pts100)
1130 int reseek_count = 0;
1131 unsigned int lastpts = 0;
1133 if (queue->current_index > 0
1134 && (queue->packets[queue->current_index].pts100 == UINT_MAX
1135 || queue->packets[queue->current_index].pts100 > pts100)) {
1136 // possible pts seek previous, try to check it.
1137 int i = 1;
1138 while (queue->current_index >= i
1139 && queue->packets[queue->current_index-i].pts100 == UINT_MAX)
1140 ++i;
1141 if (queue->current_index >= i
1142 && queue->packets[queue->current_index-i].pts100 > pts100)
1143 // pts seek previous confirmed, reseek from beginning
1144 queue->current_index = 0;
1146 while (queue->current_index < queue->packets_size
1147 && queue->packets[queue->current_index].pts100 <= pts100) {
1148 lastpts = queue->packets[queue->current_index].pts100;
1149 ++queue->current_index;
1150 ++reseek_count;
1152 while (reseek_count-- && --queue->current_index) {
1153 if (queue->packets[queue->current_index-1].pts100 != UINT_MAX &&
1154 queue->packets[queue->current_index-1].pts100 != lastpts)
1155 break;
1159 int vobsub_get_packet(void *vobhandle, float pts, void** data, int* timestamp)
1161 vobsub_t *vob = (vobsub_t *)vobhandle;
1162 unsigned int pts100 = 90000 * pts;
1163 if (vob->spu_streams && 0 <= vobsub_id && (unsigned) vobsub_id < vob->spu_streams_size) {
1164 packet_queue_t *queue = vob->spu_streams + vobsub_id;
1166 vobsub_queue_reseek(queue, pts100);
1168 while (queue->current_index < queue->packets_size) {
1169 packet_t *pkt = queue->packets + queue->current_index;
1170 if (pkt->pts100 != UINT_MAX)
1171 if (pkt->pts100 <= pts100) {
1172 ++queue->current_index;
1173 *data = pkt->data;
1174 *timestamp = pkt->pts100;
1175 return pkt->size;
1176 } else
1177 break;
1178 else
1179 ++queue->current_index;
1182 return -1;
1185 int vobsub_get_next_packet(void *vobhandle, void** data, int* timestamp)
1187 vobsub_t *vob = (vobsub_t *)vobhandle;
1188 if (vob->spu_streams && 0 <= vobsub_id && (unsigned) vobsub_id < vob->spu_streams_size) {
1189 packet_queue_t *queue = vob->spu_streams + vobsub_id;
1190 if (queue->current_index < queue->packets_size) {
1191 packet_t *pkt = queue->packets + queue->current_index;
1192 ++queue->current_index;
1193 *data = pkt->data;
1194 *timestamp = pkt->pts100;
1195 return pkt->size;
1198 return -1;
1201 void vobsub_seek(void * vobhandle, float pts)
1203 vobsub_t * vob = (vobsub_t *)vobhandle;
1204 packet_queue_t * queue;
1205 int seek_pts100 = pts * 90000;
1207 if (vob->spu_streams && 0 <= vobsub_id && (unsigned) vobsub_id < vob->spu_streams_size) {
1208 /* do not seek if we don't know the id */
1209 if (vobsub_get_id(vob, vobsub_id) == NULL)
1210 return;
1211 queue = vob->spu_streams + vobsub_id;
1212 queue->current_index = 0;
1213 vobsub_queue_reseek(queue, seek_pts100);
1217 void vobsub_reset(void *vobhandle)
1219 vobsub_t *vob = (vobsub_t *)vobhandle;
1220 if (vob->spu_streams) {
1221 unsigned int n = vob->spu_streams_size;
1222 while (n-- > 0)
1223 vob->spu_streams[n].current_index = 0;
1227 /**********************************************************************
1228 * Vobsub output
1229 **********************************************************************/
1231 typedef struct {
1232 FILE *fsub;
1233 FILE *fidx;
1234 unsigned int aid;
1235 } vobsub_out_t;
1237 static void create_idx(vobsub_out_t *me, const unsigned int *palette,
1238 unsigned int orig_width, unsigned int orig_height)
1240 int i;
1241 fprintf(me->fidx,
1242 "# VobSub index file, v7 (do not modify this line!)\n"
1243 "#\n"
1244 "# Generated by MPlayer " VERSION "\n"
1245 "# See <URL:http://www.mplayerhq.hu/> for more information about MPlayer\n"
1246 "# See <URL:http://wiki.multimedia.cx/index.php?title=VOBsub> for more information about Vobsub\n"
1247 "#\n"
1248 "size: %ux%u\n",
1249 orig_width, orig_height);
1250 if (palette) {
1251 fputs("palette:", me->fidx);
1252 for (i = 0; i < 16; ++i) {
1253 const double y = palette[i] >> 16 & 0xff,
1254 u = (palette[i] >> 8 & 0xff) - 128.0,
1255 v = (palette[i] & 0xff) - 128.0;
1256 if (i)
1257 putc(',', me->fidx);
1258 fprintf(me->fidx, " %02x%02x%02x",
1259 av_clip_uint8(y + 1.4022 * u),
1260 av_clip_uint8(y - 0.3456 * u - 0.7145 * v),
1261 av_clip_uint8(y + 1.7710 * v));
1263 putc('\n', me->fidx);
1266 fprintf(me->fidx, "# ON: displays only forced subtitles, OFF: shows everything\n"
1267 "forced subs: OFF\n");
1270 void *vobsub_out_open(const char *basename, const unsigned int *palette,
1271 unsigned int orig_width, unsigned int orig_height,
1272 const char *id, unsigned int index)
1274 vobsub_out_t *result = NULL;
1275 char *filename;
1276 filename = malloc(strlen(basename) + 5);
1277 if (filename) {
1278 result = malloc(sizeof(vobsub_out_t));
1279 if (result) {
1280 result->aid = index;
1281 strcpy(filename, basename);
1282 strcat(filename, ".sub");
1283 result->fsub = fopen(filename, "ab");
1284 if (result->fsub == NULL)
1285 perror("Error: vobsub_out_open subtitle file open failed");
1286 strcpy(filename, basename);
1287 strcat(filename, ".idx");
1288 result->fidx = fopen(filename, "ab");
1289 if (result->fidx) {
1290 if (ftell(result->fidx) == 0) {
1291 create_idx(result, palette, orig_width, orig_height);
1292 /* Make the selected language the default language */
1293 fprintf(result->fidx, "\n# Language index in use\nlangidx: %u\n", index);
1295 fprintf(result->fidx, "\nid: %s, index: %u\n", id ? id : "xx", index);
1296 /* So that we can check the file now */
1297 fflush(result->fidx);
1298 } else
1299 perror("Error: vobsub_out_open index file open failed");
1300 free(filename);
1303 return result;
1306 void vobsub_out_close(void *me)
1308 vobsub_out_t *vob = (vobsub_out_t*)me;
1309 if (vob->fidx)
1310 fclose(vob->fidx);
1311 if (vob->fsub)
1312 fclose(vob->fsub);
1313 free(vob);
1316 void vobsub_out_output(void *me, const unsigned char *packet,
1317 int len, double pts)
1319 static double last_pts;
1320 static int last_pts_set = 0;
1321 vobsub_out_t *vob = (vobsub_out_t*)me;
1322 if (vob->fsub) {
1323 /* Windows' Vobsub require that every packet is exactly 2kB long */
1324 unsigned char buffer[2048];
1325 unsigned char *p;
1326 int remain = 2048;
1327 /* Do not output twice a line with the same timestamp, this
1328 breaks Windows' Vobsub */
1329 if (vob->fidx && (!last_pts_set || last_pts != pts)) {
1330 static unsigned int last_h = 9999, last_m = 9999, last_s = 9999, last_ms = 9999;
1331 unsigned int h, m, ms;
1332 double s;
1333 s = pts;
1334 h = s / 3600;
1335 s -= h * 3600;
1336 m = s / 60;
1337 s -= m * 60;
1338 ms = (s - (unsigned int) s) * 1000;
1339 if (ms >= 1000) /* prevent overfolws or bad float stuff */
1340 ms = 0;
1341 if (h != last_h || m != last_m || (unsigned int) s != last_s || ms != last_ms) {
1342 fprintf(vob->fidx, "timestamp: %02u:%02u:%02u:%03u, filepos: %09lx\n",
1343 h, m, (unsigned int) s, ms, ftell(vob->fsub));
1344 last_h = h;
1345 last_m = m;
1346 last_s = (unsigned int) s;
1347 last_ms = ms;
1350 last_pts = pts;
1351 last_pts_set = 1;
1353 /* Packet start code: Windows' Vobsub needs this */
1354 p = buffer;
1355 *p++ = 0; /* 0x00 */
1356 *p++ = 0;
1357 *p++ = 1;
1358 *p++ = 0xba;
1359 *p++ = 0x40;
1360 memset(p, 0, 9);
1361 p += 9;
1362 { /* Packet */
1363 static unsigned char last_pts[5] = { 0, 0, 0, 0, 0};
1364 unsigned char now_pts[5];
1365 int pts_len, pad_len, datalen = len;
1366 pts *= 90000;
1367 now_pts[0] = 0x21 | (((unsigned long)pts >> 29) & 0x0e);
1368 now_pts[1] = ((unsigned long)pts >> 22) & 0xff;
1369 now_pts[2] = 0x01 | (((unsigned long)pts >> 14) & 0xfe);
1370 now_pts[3] = ((unsigned long)pts >> 7) & 0xff;
1371 now_pts[4] = 0x01 | (((unsigned long)pts << 1) & 0xfe);
1372 pts_len = memcmp(last_pts, now_pts, sizeof(now_pts)) ? sizeof(now_pts) : 0;
1373 memcpy(last_pts, now_pts, sizeof(now_pts));
1375 datalen += 3; /* Version, PTS_flags, pts_len */
1376 datalen += pts_len;
1377 datalen += 1; /* AID */
1378 pad_len = 2048 - (p - buffer) - 4 /* MPEG ID */ - 2 /* payload len */ - datalen;
1379 /* XXX - Go figure what should go here! In any case the
1380 packet has to be completly filled. If I can fill it
1381 with padding (0x000001be) latter I'll do that. But if
1382 there is only room for 6 bytes then I can not write a
1383 padding packet. So I add some padding in the PTS
1384 field. This looks like a dirty kludge. Oh well... */
1385 if (pad_len < 0) {
1386 /* Packet is too big. Let's try ommiting the PTS field */
1387 datalen -= pts_len;
1388 pts_len = 0;
1389 pad_len = 0;
1390 } else if (pad_len > 6)
1391 pad_len = 0;
1392 datalen += pad_len;
1394 *p++ = 0; /* 0x0e */
1395 *p++ = 0;
1396 *p++ = 1;
1397 *p++ = 0xbd;
1399 *p++ = (datalen >> 8) & 0xff; /* length of payload */
1400 *p++ = datalen & 0xff;
1401 *p++ = 0x80; /* System-2 (.VOB) stream */
1402 *p++ = pts_len ? 0x80 : 0x00; /* pts_flags */
1403 *p++ = pts_len + pad_len;
1404 memcpy(p, now_pts, pts_len);
1405 p += pts_len;
1406 memset(p, 0, pad_len);
1407 p += pad_len;
1409 *p++ = 0x20 | vob->aid; /* aid */
1410 if (fwrite(buffer, p - buffer, 1, vob->fsub) != 1
1411 || fwrite(packet, len, 1, vob->fsub) != 1)
1412 perror("ERROR: vobsub write failed");
1413 else
1414 remain -= p - buffer + len;
1416 /* Padding */
1417 if (remain >= 6) {
1418 p = buffer;
1419 *p++ = 0x00;
1420 *p++ = 0x00;
1421 *p++ = 0x01;
1422 *p++ = 0xbe;
1423 *p++ = (remain - 6) >> 8;
1424 *p++ = (remain - 6) & 0xff;
1425 /* for better compression, blank this */
1426 memset(buffer + 6, 0, remain - (p - buffer));
1427 if (fwrite(buffer, remain, 1, vob->fsub) != 1)
1428 perror("ERROR: vobsub padding write failed");
1429 } else if (remain > 0) {
1430 /* I don't know what to output. But anyway the block
1431 needs to be 2KB big */
1432 memset(buffer, 0, remain);
1433 if (fwrite(buffer, remain, 1, vob->fsub) != 1)
1434 perror("ERROR: vobsub blank padding write failed");
1435 } else if (remain < 0)
1436 fprintf(stderr,
1437 "\nERROR: wrong thing happenned...\n"
1438 " I wrote a %i data bytes spu packet and that's too long\n", len);