Skip svn changes up to r31248
[mplayer/glamo.git] / vobsub.c
blobf14e72f7d3e3835743e11dfdd2d65d06b2e4d727
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 int padding_was_here;
289 int merge;
290 } mpeg_t;
292 static mpeg_t *mpeg_open(const char *filename)
294 mpeg_t *res = malloc(sizeof(mpeg_t));
295 int err = res == NULL;
296 if (!err) {
297 res->pts = 0;
298 res->aid = -1;
299 res->packet = NULL;
300 res->packet_size = 0;
301 res->packet_reserve = 0;
302 res->padding_was_here = 1;
303 res->merge = 0;
304 res->stream = rar_open(filename, "rb");
305 err = res->stream == NULL;
306 if (err)
307 perror("fopen Vobsub file failed");
308 if (err)
309 free(res);
311 return err ? NULL : res;
314 static void mpeg_free(mpeg_t *mpeg)
316 if (mpeg->packet)
317 free(mpeg->packet);
318 if (mpeg->stream)
319 rar_close(mpeg->stream);
320 free(mpeg);
323 static int mpeg_eof(mpeg_t *mpeg)
325 return rar_eof(mpeg->stream);
328 static off_t mpeg_tell(mpeg_t *mpeg)
330 return rar_tell(mpeg->stream);
333 static int mpeg_run(mpeg_t *mpeg)
335 unsigned int len, idx, version;
336 int c;
337 /* Goto start of a packet, it starts with 0x000001?? */
338 const unsigned char wanted[] = { 0, 0, 1 };
339 unsigned char buf[5];
341 mpeg->aid = -1;
342 mpeg->packet_size = 0;
343 if (rar_read(buf, 4, 1, mpeg->stream) != 1)
344 return -1;
345 while (memcmp(buf, wanted, sizeof(wanted)) != 0) {
346 c = rar_getc(mpeg->stream);
347 if (c < 0)
348 return -1;
349 memmove(buf, buf + 1, 3);
350 buf[3] = c;
352 switch (buf[3]) {
353 case 0xb9: /* System End Code */
354 break;
355 case 0xba: /* Packet start code */
356 c = rar_getc(mpeg->stream);
357 if (c < 0)
358 return -1;
359 if ((c & 0xc0) == 0x40)
360 version = 4;
361 else if ((c & 0xf0) == 0x20)
362 version = 2;
363 else {
364 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Unsupported MPEG version: 0x%02x\n", c);
365 return -1;
367 if (version == 4) {
368 if (rar_seek(mpeg->stream, 9, SEEK_CUR))
369 return -1;
370 } else if (version == 2) {
371 if (rar_seek(mpeg->stream, 7, SEEK_CUR))
372 return -1;
373 } else
374 abort();
375 if (!mpeg->padding_was_here)
376 mpeg->merge = 1;
377 break;
378 case 0xbd: /* packet */
379 if (rar_read(buf, 2, 1, mpeg->stream) != 1)
380 return -1;
381 mpeg->padding_was_here = 0;
382 len = buf[0] << 8 | buf[1];
383 idx = mpeg_tell(mpeg);
384 c = rar_getc(mpeg->stream);
385 if (c < 0)
386 return -1;
387 if ((c & 0xC0) == 0x40) { /* skip STD scale & size */
388 if (rar_getc(mpeg->stream) < 0)
389 return -1;
390 c = rar_getc(mpeg->stream);
391 if (c < 0)
392 return -1;
394 if ((c & 0xf0) == 0x20) { /* System-1 stream timestamp */
395 /* Do we need this? */
396 abort();
397 } else if ((c & 0xf0) == 0x30) {
398 /* Do we need this? */
399 abort();
400 } else if ((c & 0xc0) == 0x80) { /* System-2 (.VOB) stream */
401 unsigned int pts_flags, hdrlen, dataidx;
402 c = rar_getc(mpeg->stream);
403 if (c < 0)
404 return -1;
405 pts_flags = c;
406 c = rar_getc(mpeg->stream);
407 if (c < 0)
408 return -1;
409 hdrlen = c;
410 dataidx = mpeg_tell(mpeg) + hdrlen;
411 if (dataidx > idx + len) {
412 mp_msg(MSGT_VOBSUB, MSGL_ERR, "Invalid header length: %d (total length: %d, idx: %d, dataidx: %d)\n",
413 hdrlen, len, idx, dataidx);
414 return -1;
416 if ((pts_flags & 0xc0) == 0x80) {
417 if (rar_read(buf, 5, 1, mpeg->stream) != 1)
418 return -1;
419 if (!(((buf[0] & 0xf0) == 0x20) && (buf[0] & 1) && (buf[2] & 1) && (buf[4] & 1))) {
420 mp_msg(MSGT_VOBSUB, MSGL_ERR, "vobsub PTS error: 0x%02x %02x%02x %02x%02x \n",
421 buf[0], buf[1], buf[2], buf[3], buf[4]);
422 mpeg->pts = 0;
423 } else
424 mpeg->pts = ((buf[0] & 0x0e) << 29 | buf[1] << 22 | (buf[2] & 0xfe) << 14
425 | buf[3] << 7 | (buf[4] >> 1));
426 } else /* if ((pts_flags & 0xc0) == 0xc0) */ {
427 /* what's this? */
428 /* abort(); */
430 rar_seek(mpeg->stream, dataidx, SEEK_SET);
431 mpeg->aid = rar_getc(mpeg->stream);
432 if (mpeg->aid < 0) {
433 mp_msg(MSGT_VOBSUB, MSGL_ERR, "Bogus aid %d\n", mpeg->aid);
434 return -1;
436 mpeg->packet_size = len - ((unsigned int) mpeg_tell(mpeg) - idx);
437 if (mpeg->packet_reserve < mpeg->packet_size) {
438 if (mpeg->packet)
439 free(mpeg->packet);
440 mpeg->packet = malloc(mpeg->packet_size);
441 if (mpeg->packet)
442 mpeg->packet_reserve = mpeg->packet_size;
444 if (mpeg->packet == NULL) {
445 mp_msg(MSGT_VOBSUB, MSGL_FATAL, "malloc failure");
446 mpeg->packet_reserve = 0;
447 mpeg->packet_size = 0;
448 return -1;
450 if (rar_read(mpeg->packet, mpeg->packet_size, 1, mpeg->stream) != 1) {
451 mp_msg(MSGT_VOBSUB, MSGL_ERR, "fread failure");
452 mpeg->packet_size = 0;
453 return -1;
455 idx = len;
457 break;
458 case 0xbe: /* Padding */
459 if (rar_read(buf, 2, 1, mpeg->stream) != 1)
460 return -1;
461 len = buf[0] << 8 | buf[1];
462 if (len > 0 && rar_seek(mpeg->stream, len, SEEK_CUR))
463 return -1;
464 mpeg->padding_was_here = 1;
465 break;
466 default:
467 if (0xc0 <= buf[3] && buf[3] < 0xf0) {
468 /* MPEG audio or video */
469 if (rar_read(buf, 2, 1, mpeg->stream) != 1)
470 return -1;
471 len = buf[0] << 8 | buf[1];
472 if (len > 0 && rar_seek(mpeg->stream, len, SEEK_CUR))
473 return -1;
474 } else {
475 mp_msg(MSGT_VOBSUB, MSGL_ERR, "unknown header 0x%02X%02X%02X%02X\n",
476 buf[0], buf[1], buf[2], buf[3]);
477 return -1;
480 return 0;
483 /**********************************************************************
484 * Packet queue
485 **********************************************************************/
487 typedef struct {
488 unsigned int pts100;
489 off_t filepos;
490 unsigned int size;
491 unsigned char *data;
492 } packet_t;
494 typedef struct {
495 char *id;
496 packet_t *packets;
497 unsigned int packets_reserve;
498 unsigned int packets_size;
499 unsigned int current_index;
500 } packet_queue_t;
502 static void packet_construct(packet_t *pkt)
504 pkt->pts100 = 0;
505 pkt->filepos = 0;
506 pkt->size = 0;
507 pkt->data = NULL;
510 static void packet_destroy(packet_t *pkt)
512 if (pkt->data)
513 free(pkt->data);
516 static void packet_queue_construct(packet_queue_t *queue)
518 queue->id = NULL;
519 queue->packets = NULL;
520 queue->packets_reserve = 0;
521 queue->packets_size = 0;
522 queue->current_index = 0;
525 static void packet_queue_destroy(packet_queue_t *queue)
527 if (queue->packets) {
528 while (queue->packets_size--)
529 packet_destroy(queue->packets + queue->packets_size);
530 free(queue->packets);
532 return;
535 /* Make sure there is enough room for needed_size packets in the
536 packet queue. */
537 static int packet_queue_ensure(packet_queue_t *queue, unsigned int needed_size)
539 if (queue->packets_reserve < needed_size) {
540 if (queue->packets) {
541 packet_t *tmp = realloc(queue->packets, 2 * queue->packets_reserve * sizeof(packet_t));
542 if (tmp == NULL) {
543 mp_msg(MSGT_VOBSUB, MSGL_FATAL, "realloc failure");
544 return -1;
546 queue->packets = tmp;
547 queue->packets_reserve *= 2;
548 } else {
549 queue->packets = malloc(sizeof(packet_t));
550 if (queue->packets == NULL) {
551 mp_msg(MSGT_VOBSUB, MSGL_FATAL, "malloc failure");
552 return -1;
554 queue->packets_reserve = 1;
557 return 0;
560 /* add one more packet */
561 static int packet_queue_grow(packet_queue_t *queue)
563 if (packet_queue_ensure(queue, queue->packets_size + 1) < 0)
564 return -1;
565 packet_construct(queue->packets + queue->packets_size);
566 ++queue->packets_size;
567 return 0;
570 /* insert a new packet, duplicating pts from the current one */
571 static int packet_queue_insert(packet_queue_t *queue)
573 packet_t *pkts;
574 if (packet_queue_ensure(queue, queue->packets_size + 1) < 0)
575 return -1;
576 /* XXX packet_size does not reflect the real thing here, it will be updated a bit later */
577 memmove(queue->packets + queue->current_index + 2,
578 queue->packets + queue->current_index + 1,
579 sizeof(packet_t) * (queue->packets_size - queue->current_index - 1));
580 pkts = queue->packets + queue->current_index;
581 ++queue->packets_size;
582 ++queue->current_index;
583 packet_construct(pkts + 1);
584 pkts[1].pts100 = pkts[0].pts100;
585 pkts[1].filepos = pkts[0].filepos;
586 return 0;
589 /**********************************************************************
590 * Vobsub
591 **********************************************************************/
593 typedef struct {
594 unsigned int palette[16];
595 int delay;
596 unsigned int have_palette;
597 unsigned int orig_frame_width, orig_frame_height;
598 unsigned int origin_x, origin_y;
599 /* index */
600 packet_queue_t *spu_streams;
601 unsigned int spu_streams_size;
602 unsigned int spu_streams_current;
603 unsigned int spu_valid_streams_size;
604 } vobsub_t;
606 /* Make sure that the spu stream idx exists. */
607 static int vobsub_ensure_spu_stream(vobsub_t *vob, unsigned int index)
609 if (index >= vob->spu_streams_size) {
610 /* This is a new stream */
611 if (vob->spu_streams) {
612 packet_queue_t *tmp = realloc(vob->spu_streams, (index + 1) * sizeof(packet_queue_t));
613 if (tmp == NULL) {
614 mp_msg(MSGT_VOBSUB, MSGL_ERR, "vobsub_ensure_spu_stream: realloc failure");
615 return -1;
617 vob->spu_streams = tmp;
618 } else {
619 vob->spu_streams = malloc((index + 1) * sizeof(packet_queue_t));
620 if (vob->spu_streams == NULL) {
621 mp_msg(MSGT_VOBSUB, MSGL_ERR, "vobsub_ensure_spu_stream: malloc failure");
622 return -1;
625 while (vob->spu_streams_size <= index) {
626 packet_queue_construct(vob->spu_streams + vob->spu_streams_size);
627 ++vob->spu_streams_size;
630 return 0;
633 static int vobsub_add_id(vobsub_t *vob, const char *id, size_t idlen,
634 const unsigned int index)
636 if (vobsub_ensure_spu_stream(vob, index) < 0)
637 return -1;
638 if (id && idlen) {
639 if (vob->spu_streams[index].id)
640 free(vob->spu_streams[index].id);
641 vob->spu_streams[index].id = malloc(idlen + 1);
642 if (vob->spu_streams[index].id == NULL) {
643 mp_msg(MSGT_VOBSUB, MSGL_FATAL, "vobsub_add_id: malloc failure");
644 return -1;
646 vob->spu_streams[index].id[idlen] = 0;
647 memcpy(vob->spu_streams[index].id, id, idlen);
649 vob->spu_streams_current = index;
650 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VOBSUB_ID=%d\n", index);
651 if (id && idlen)
652 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VSID_%d_LANG=%s\n", index, vob->spu_streams[index].id);
653 mp_msg(MSGT_VOBSUB, MSGL_V, "[vobsub] subtitle (vobsubid): %d language %s\n",
654 index, vob->spu_streams[index].id);
655 return 0;
658 static int vobsub_add_timestamp(vobsub_t *vob, off_t filepos, int ms)
660 packet_queue_t *queue;
661 packet_t *pkt;
662 if (vob->spu_streams == 0) {
663 mp_msg(MSGT_VOBSUB, MSGL_WARN, "[vobsub] warning, binning some index entries. Check your index file\n");
664 return -1;
666 queue = vob->spu_streams + vob->spu_streams_current;
667 if (packet_queue_grow(queue) >= 0) {
668 pkt = queue->packets + (queue->packets_size - 1);
669 pkt->filepos = filepos;
670 pkt->pts100 = ms < 0 ? UINT_MAX : (unsigned int)ms * 90;
671 return 0;
673 return -1;
676 static int vobsub_parse_id(vobsub_t *vob, const char *line)
678 // id: xx, index: n
679 size_t idlen;
680 const char *p, *q;
681 p = line;
682 while (isspace(*p))
683 ++p;
684 q = p;
685 while (isalpha(*q))
686 ++q;
687 idlen = q - p;
688 if (idlen == 0)
689 return -1;
690 ++q;
691 while (isspace(*q))
692 ++q;
693 if (strncmp("index:", q, 6))
694 return -1;
695 q += 6;
696 while (isspace(*q))
697 ++q;
698 if (!isdigit(*q))
699 return -1;
700 return vobsub_add_id(vob, p, idlen, atoi(q));
703 static int vobsub_parse_timestamp(vobsub_t *vob, const char *line)
705 // timestamp: HH:MM:SS.mmm, filepos: 0nnnnnnnnn
706 const char *p;
707 int h, m, s, ms;
708 off_t filepos;
709 while (isspace(*line))
710 ++line;
711 p = line;
712 while (isdigit(*p))
713 ++p;
714 if (p - line != 2)
715 return -1;
716 h = 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 m = atoi(line);
725 if (*p != ':')
726 return -1;
727 line = ++p;
728 while (isdigit(*p))
729 ++p;
730 if (p - line != 2)
731 return -1;
732 s = atoi(line);
733 if (*p != ':')
734 return -1;
735 line = ++p;
736 while (isdigit(*p))
737 ++p;
738 if (p - line != 3)
739 return -1;
740 ms = atoi(line);
741 if (*p != ',')
742 return -1;
743 line = p + 1;
744 while (isspace(*line))
745 ++line;
746 if (strncmp("filepos:", line, 8))
747 return -1;
748 line += 8;
749 while (isspace(*line))
750 ++line;
751 if (! isxdigit(*line))
752 return -1;
753 filepos = strtol(line, NULL, 16);
754 return vobsub_add_timestamp(vob, filepos, vob->delay + ms + 1000 * (s + 60 * (m + 60 * h)));
757 static int vobsub_parse_origin(vobsub_t *vob, const char *line)
759 // org: X,Y
760 char *p;
761 while (isspace(*line))
762 ++line;
763 if (!isdigit(*line))
764 return -1;
765 vob->origin_x = strtoul(line, &p, 10);
766 if (*p != ',')
767 return -1;
768 ++p;
769 vob->origin_y = strtoul(p, NULL, 10);
770 return 0;
773 unsigned int vobsub_palette_to_yuv(unsigned int pal)
775 int r, g, b, y, u, v;
776 // Palette in idx file is not rgb value, it was calculated by wrong formula.
777 // Here's reversed formula of the one used to generate palette in idx file.
778 r = pal >> 16 & 0xff;
779 g = pal >> 8 & 0xff;
780 b = pal & 0xff;
781 y = av_clip_uint8( 0.1494 * r + 0.6061 * g + 0.2445 * b);
782 u = av_clip_uint8( 0.6066 * r - 0.4322 * g - 0.1744 * b + 128);
783 v = av_clip_uint8(-0.08435 * r - 0.3422 * g + 0.4266 * b + 128);
784 y = y * 219 / 255 + 16;
785 return y << 16 | u << 8 | v;
788 unsigned int vobsub_rgb_to_yuv(unsigned int rgb)
790 int r, g, b, y, u, v;
791 r = rgb >> 16 & 0xff;
792 g = rgb >> 8 & 0xff;
793 b = rgb & 0xff;
794 y = ( 0.299 * r + 0.587 * g + 0.114 * b) * 219 / 255 + 16.5;
795 u = (-0.16874 * r - 0.33126 * g + 0.5 * b) * 224 / 255 + 128.5;
796 v = ( 0.5 * r - 0.41869 * g - 0.08131 * b) * 224 / 255 + 128.5;
797 return y << 16 | u << 8 | v;
800 static int vobsub_parse_delay(vobsub_t *vob, const char *line)
802 int h, m, s, ms;
803 int forward = 1;
804 if (*(line + 7) == '+') {
805 forward = 1;
806 line++;
807 } else if (*(line + 7) == '-') {
808 forward = -1;
809 line++;
811 mp_msg(MSGT_SPUDEC, MSGL_V, "forward=%d", forward);
812 h = atoi(line + 7);
813 mp_msg(MSGT_VOBSUB, MSGL_V, "h=%d,", h);
814 m = atoi(line + 10);
815 mp_msg(MSGT_VOBSUB, MSGL_V, "m=%d,", m);
816 s = atoi(line + 13);
817 mp_msg(MSGT_VOBSUB, MSGL_V, "s=%d,", s);
818 ms = atoi(line + 16);
819 mp_msg(MSGT_VOBSUB, MSGL_V, "ms=%d", ms);
820 vob->delay = (ms + 1000 * (s + 60 * (m + 60 * h))) * forward;
821 return 0;
824 static int vobsub_set_lang(const char *line)
826 if (vobsub_id == -1)
827 vobsub_id = atoi(line + 8);
828 return 0;
831 static int vobsub_parse_one_line(vobsub_t *vob, rar_stream_t *fd,
832 unsigned char **extradata,
833 unsigned int *extradata_len)
835 ssize_t line_size;
836 int res = -1;
837 size_t line_reserve = 0;
838 char *line = NULL;
839 do {
840 line_size = vobsub_getline(&line, &line_reserve, fd);
841 if (line_size < 0 || line_size > 1000000 ||
842 *extradata_len+line_size > 10000000) {
843 break;
846 *extradata = realloc(*extradata, *extradata_len+line_size+1);
847 memcpy(*extradata+*extradata_len, line, line_size);
848 *extradata_len += line_size;
849 (*extradata)[*extradata_len] = 0;
851 if (*line == 0 || *line == '\r' || *line == '\n' || *line == '#')
852 continue;
853 else if (strncmp("langidx:", line, 8) == 0)
854 res = vobsub_set_lang(line);
855 else if (strncmp("delay:", line, 6) == 0)
856 res = vobsub_parse_delay(vob, line);
857 else if (strncmp("id:", line, 3) == 0)
858 res = vobsub_parse_id(vob, line + 3);
859 else if (strncmp("org:", line, 4) == 0)
860 res = vobsub_parse_origin(vob, line + 4);
861 else if (strncmp("timestamp:", line, 10) == 0)
862 res = vobsub_parse_timestamp(vob, line + 10);
863 else {
864 mp_msg(MSGT_VOBSUB, MSGL_V, "vobsub: ignoring %s", line);
865 continue;
867 if (res < 0)
868 mp_msg(MSGT_VOBSUB, MSGL_ERR, "ERROR in %s", line);
869 break;
870 } while (1);
871 if (line)
872 free(line);
873 return res;
876 int vobsub_parse_ifo(void* this, const char *const name, unsigned int *palette,
877 unsigned int *width, unsigned int *height, int force,
878 int sid, char *langid)
880 vobsub_t *vob = (vobsub_t*)this;
881 int res = -1;
882 rar_stream_t *fd = rar_open(name, "rb");
883 if (fd == NULL) {
884 if (force)
885 mp_msg(MSGT_VOBSUB, MSGL_WARN, "VobSub: Can't open IFO file\n");
886 } else {
887 // parse IFO header
888 unsigned char block[0x800];
889 const char *const ifo_magic = "DVDVIDEO-VTS";
890 if (rar_read(block, sizeof(block), 1, fd) != 1) {
891 if (force)
892 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Can't read IFO header\n");
893 } else if (memcmp(block, ifo_magic, strlen(ifo_magic) + 1))
894 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Bad magic in IFO header\n");
895 else {
896 unsigned long pgci_sector = block[0xcc] << 24 | block[0xcd] << 16
897 | block[0xce] << 8 | block[0xcf];
898 int standard = (block[0x200] & 0x30) >> 4;
899 int resolution = (block[0x201] & 0x0c) >> 2;
900 *height = standard ? 576 : 480;
901 *width = 0;
902 switch (resolution) {
903 case 0x0:
904 *width = 720;
905 break;
906 case 0x1:
907 *width = 704;
908 break;
909 case 0x2:
910 *width = 352;
911 break;
912 case 0x3:
913 *width = 352;
914 *height /= 2;
915 break;
916 default:
917 mp_msg(MSGT_VOBSUB, MSGL_WARN, "Vobsub: Unknown resolution %d \n", resolution);
919 if (langid && 0 <= sid && sid < 32) {
920 unsigned char *tmp = block + 0x256 + sid * 6 + 2;
921 langid[0] = tmp[0];
922 langid[1] = tmp[1];
923 langid[2] = 0;
925 if (rar_seek(fd, pgci_sector * sizeof(block), SEEK_SET)
926 || rar_read(block, sizeof(block), 1, fd) != 1)
927 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Can't read IFO PGCI\n");
928 else {
929 unsigned long idx;
930 unsigned long pgc_offset = block[0xc] << 24 | block[0xd] << 16
931 | block[0xe] << 8 | block[0xf];
932 for (idx = 0; idx < 16; ++idx) {
933 unsigned char *p = block + pgc_offset + 0xa4 + 4 * idx;
934 palette[idx] = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
936 if (vob)
937 vob->have_palette = 1;
938 res = 0;
941 rar_close(fd);
943 return res;
946 void *vobsub_open(const char *const name, const char *const ifo,
947 const int force, void** spu)
949 unsigned char *extradata = NULL;
950 unsigned int extradata_len = 0;
951 vobsub_t *vob = calloc(1, sizeof(vobsub_t));
952 if (spu)
953 *spu = NULL;
954 if (vobsubid == -2)
955 vobsubid = vobsub_id;
956 if (vob) {
957 char *buf;
958 buf = malloc(strlen(name) + 5);
959 if (buf) {
960 rar_stream_t *fd;
961 mpeg_t *mpg;
962 /* read in the info file */
963 if (!ifo) {
964 strcpy(buf, name);
965 strcat(buf, ".ifo");
966 vobsub_parse_ifo(vob, buf, vob->palette, &vob->orig_frame_width, &vob->orig_frame_height, force, -1, NULL);
967 } else
968 vobsub_parse_ifo(vob, ifo, vob->palette, &vob->orig_frame_width, &vob->orig_frame_height, force, -1, NULL);
969 /* read in the index */
970 strcpy(buf, name);
971 strcat(buf, ".idx");
972 fd = rar_open(buf, "rb");
973 if (fd == NULL) {
974 if (force)
975 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Can't open IDX file\n");
976 else {
977 free(buf);
978 free(vob);
979 return NULL;
981 } else {
982 while (vobsub_parse_one_line(vob, fd, &extradata, &extradata_len) >= 0)
983 /* NOOP */ ;
984 rar_close(fd);
986 if (spu)
987 *spu = spudec_new_scaled(vob->palette, vob->orig_frame_width, vob->orig_frame_height, extradata, extradata_len);
988 if (extradata)
989 free(extradata);
991 /* read the indexed mpeg_stream */
992 strcpy(buf, name);
993 strcat(buf, ".sub");
994 mpg = mpeg_open(buf);
995 if (mpg == NULL) {
996 if (force)
997 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Can't open SUB file\n");
998 else {
999 free(buf);
1000 free(vob);
1001 return NULL;
1003 } else {
1004 long last_pts_diff = 0;
1005 while (!mpeg_eof(mpg)) {
1006 off_t pos = mpeg_tell(mpg);
1007 if (mpeg_run(mpg) < 0) {
1008 if (!mpeg_eof(mpg))
1009 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: mpeg_run error\n");
1010 break;
1012 if (mpg->packet_size) {
1013 if ((mpg->aid & 0xe0) == 0x20) {
1014 unsigned int sid = mpg->aid & 0x1f;
1015 if (vobsub_ensure_spu_stream(vob, sid) >= 0) {
1016 packet_queue_t *queue = vob->spu_streams + sid;
1017 /* get the packet to fill */
1018 if (queue->packets_size == 0 && packet_queue_grow(queue) < 0)
1019 abort();
1020 while (queue->current_index + 1 < queue->packets_size
1021 && queue->packets[queue->current_index + 1].filepos <= pos)
1022 ++queue->current_index;
1023 if (queue->current_index < queue->packets_size) {
1024 packet_t *pkt;
1025 if (queue->packets[queue->current_index].data) {
1026 /* insert a new packet and fix the PTS ! */
1027 packet_queue_insert(queue);
1028 queue->packets[queue->current_index].pts100 =
1029 mpg->pts + last_pts_diff;
1031 pkt = queue->packets + queue->current_index;
1032 if (pkt->pts100 != UINT_MAX) {
1033 if (queue->packets_size > 1)
1034 last_pts_diff = pkt->pts100 - mpg->pts;
1035 else
1036 pkt->pts100 = mpg->pts;
1037 if (mpg->merge) {
1038 packet_t *last = &queue->packets[queue->current_index - 1];
1039 pkt->pts100 = last->pts100;
1040 mpg->merge = 0;
1042 /* FIXME: should not use mpg_sub internal informations, make a copy */
1043 pkt->data = mpg->packet;
1044 pkt->size = mpg->packet_size;
1045 mpg->packet = NULL;
1046 mpg->packet_reserve = 0;
1047 mpg->packet_size = 0;
1050 } else
1051 mp_msg(MSGT_VOBSUB, MSGL_WARN, "don't know what to do with subtitle #%u\n", sid);
1055 vob->spu_streams_current = vob->spu_streams_size;
1056 while (vob->spu_streams_current-- > 0) {
1057 vob->spu_streams[vob->spu_streams_current].current_index = 0;
1058 if (vobsubid == vob->spu_streams_current ||
1059 vob->spu_streams[vob->spu_streams_current].packets_size > 0)
1060 ++vob->spu_valid_streams_size;
1062 mpeg_free(mpg);
1064 free(buf);
1067 return vob;
1070 void vobsub_close(void *this)
1072 vobsub_t *vob = (vobsub_t *)this;
1073 if (vob->spu_streams) {
1074 while (vob->spu_streams_size--)
1075 packet_queue_destroy(vob->spu_streams + vob->spu_streams_size);
1076 free(vob->spu_streams);
1078 free(vob);
1081 unsigned int vobsub_get_indexes_count(void *vobhandle)
1083 vobsub_t *vob = (vobsub_t *) vobhandle;
1084 return vob->spu_valid_streams_size;
1087 char *vobsub_get_id(void *vobhandle, unsigned int index)
1089 vobsub_t *vob = (vobsub_t *) vobhandle;
1090 return (index < vob->spu_streams_size) ? vob->spu_streams[index].id : NULL;
1093 int vobsub_get_id_by_index(void *vobhandle, unsigned int index)
1095 vobsub_t *vob = vobhandle;
1096 int i, j;
1097 if (vob == NULL)
1098 return -1;
1099 for (i = 0, j = 0; i < vob->spu_streams_size; ++i)
1100 if (i == vobsubid || vob->spu_streams[i].packets_size > 0) {
1101 if (j == index)
1102 return i;
1103 ++j;
1105 return -1;
1108 int vobsub_get_index_by_id(void *vobhandle, int id)
1110 vobsub_t *vob = vobhandle;
1111 int i, j;
1112 if (vob == NULL || id < 0 || id >= vob->spu_streams_size)
1113 return -1;
1114 if (id != vobsubid && !vob->spu_streams[id].packets_size)
1115 return -1;
1116 for (i = 0, j = 0; i < id; ++i)
1117 if (i == vobsubid || vob->spu_streams[i].packets_size > 0)
1118 ++j;
1119 return j;
1122 int vobsub_set_from_lang(void *vobhandle, unsigned char * lang)
1124 int i;
1125 vobsub_t *vob= (vobsub_t *) vobhandle;
1126 while (lang && strlen(lang) >= 2) {
1127 for (i = 0; i < vob->spu_streams_size; i++)
1128 if (vob->spu_streams[i].id)
1129 if ((strncmp(vob->spu_streams[i].id, lang, 2) == 0)) {
1130 vobsub_id = i;
1131 mp_msg(MSGT_VOBSUB, MSGL_INFO, "Selected VOBSUB language: %d language: %s\n", i, vob->spu_streams[i].id);
1132 return 0;
1134 lang+=2;while (lang[0]==',' || lang[0]==' ') ++lang;
1136 mp_msg(MSGT_VOBSUB, MSGL_WARN, "No matching VOBSUB language found!\n");
1137 return -1;
1140 /// make sure we seek to the first packet of packets having same pts values.
1141 static void vobsub_queue_reseek(packet_queue_t *queue, unsigned int pts100)
1143 int reseek_count = 0;
1144 unsigned int lastpts = 0;
1146 if (queue->current_index > 0
1147 && (queue->packets[queue->current_index].pts100 == UINT_MAX
1148 || queue->packets[queue->current_index].pts100 > pts100)) {
1149 // possible pts seek previous, try to check it.
1150 int i = 1;
1151 while (queue->current_index >= i
1152 && queue->packets[queue->current_index-i].pts100 == UINT_MAX)
1153 ++i;
1154 if (queue->current_index >= i
1155 && queue->packets[queue->current_index-i].pts100 > pts100)
1156 // pts seek previous confirmed, reseek from beginning
1157 queue->current_index = 0;
1159 while (queue->current_index < queue->packets_size
1160 && queue->packets[queue->current_index].pts100 <= pts100) {
1161 lastpts = queue->packets[queue->current_index].pts100;
1162 ++queue->current_index;
1163 ++reseek_count;
1165 while (reseek_count-- && --queue->current_index) {
1166 if (queue->packets[queue->current_index-1].pts100 != UINT_MAX &&
1167 queue->packets[queue->current_index-1].pts100 != lastpts)
1168 break;
1172 int vobsub_get_packet(void *vobhandle, float pts, void** data, int* timestamp)
1174 vobsub_t *vob = (vobsub_t *)vobhandle;
1175 unsigned int pts100 = 90000 * pts;
1176 if (vob->spu_streams && 0 <= vobsub_id && (unsigned) vobsub_id < vob->spu_streams_size) {
1177 packet_queue_t *queue = vob->spu_streams + vobsub_id;
1179 vobsub_queue_reseek(queue, pts100);
1181 while (queue->current_index < queue->packets_size) {
1182 packet_t *pkt = queue->packets + queue->current_index;
1183 if (pkt->pts100 != UINT_MAX)
1184 if (pkt->pts100 <= pts100) {
1185 ++queue->current_index;
1186 *data = pkt->data;
1187 *timestamp = pkt->pts100;
1188 return pkt->size;
1189 } else
1190 break;
1191 else
1192 ++queue->current_index;
1195 return -1;
1198 int vobsub_get_next_packet(void *vobhandle, void** data, int* timestamp)
1200 vobsub_t *vob = (vobsub_t *)vobhandle;
1201 if (vob->spu_streams && 0 <= vobsub_id && (unsigned) vobsub_id < vob->spu_streams_size) {
1202 packet_queue_t *queue = vob->spu_streams + vobsub_id;
1203 if (queue->current_index < queue->packets_size) {
1204 packet_t *pkt = queue->packets + queue->current_index;
1205 ++queue->current_index;
1206 *data = pkt->data;
1207 *timestamp = pkt->pts100;
1208 return pkt->size;
1211 return -1;
1214 void vobsub_seek(void * vobhandle, float pts)
1216 vobsub_t * vob = (vobsub_t *)vobhandle;
1217 packet_queue_t * queue;
1218 int seek_pts100 = pts * 90000;
1220 if (vob->spu_streams && 0 <= vobsub_id && (unsigned) vobsub_id < vob->spu_streams_size) {
1221 /* do not seek if we don't know the id */
1222 if (vobsub_get_id(vob, vobsub_id) == NULL)
1223 return;
1224 queue = vob->spu_streams + vobsub_id;
1225 queue->current_index = 0;
1226 vobsub_queue_reseek(queue, seek_pts100);
1230 void vobsub_reset(void *vobhandle)
1232 vobsub_t *vob = (vobsub_t *)vobhandle;
1233 if (vob->spu_streams) {
1234 unsigned int n = vob->spu_streams_size;
1235 while (n-- > 0)
1236 vob->spu_streams[n].current_index = 0;
1240 /**********************************************************************
1241 * Vobsub output
1242 **********************************************************************/
1244 typedef struct {
1245 FILE *fsub;
1246 FILE *fidx;
1247 unsigned int aid;
1248 } vobsub_out_t;
1250 static void create_idx(vobsub_out_t *me, const unsigned int *palette,
1251 unsigned int orig_width, unsigned int orig_height)
1253 int i;
1254 fprintf(me->fidx,
1255 "# VobSub index file, v7 (do not modify this line!)\n"
1256 "#\n"
1257 "# Generated by MPlayer " VERSION "\n"
1258 "# See <URL:http://www.mplayerhq.hu/> for more information about MPlayer\n"
1259 "# See <URL:http://wiki.multimedia.cx/index.php?title=VOBsub> for more information about Vobsub\n"
1260 "#\n"
1261 "size: %ux%u\n",
1262 orig_width, orig_height);
1263 if (palette) {
1264 fputs("palette:", me->fidx);
1265 for (i = 0; i < 16; ++i) {
1266 const double y = palette[i] >> 16 & 0xff,
1267 u = (palette[i] >> 8 & 0xff) - 128.0,
1268 v = (palette[i] & 0xff) - 128.0;
1269 if (i)
1270 putc(',', me->fidx);
1271 fprintf(me->fidx, " %02x%02x%02x",
1272 av_clip_uint8(y + 1.4022 * u),
1273 av_clip_uint8(y - 0.3456 * u - 0.7145 * v),
1274 av_clip_uint8(y + 1.7710 * v));
1276 putc('\n', me->fidx);
1279 fprintf(me->fidx, "# ON: displays only forced subtitles, OFF: shows everything\n"
1280 "forced subs: OFF\n");
1283 void *vobsub_out_open(const char *basename, const unsigned int *palette,
1284 unsigned int orig_width, unsigned int orig_height,
1285 const char *id, unsigned int index)
1287 vobsub_out_t *result = NULL;
1288 char *filename;
1289 filename = malloc(strlen(basename) + 5);
1290 if (filename) {
1291 result = malloc(sizeof(vobsub_out_t));
1292 if (result) {
1293 result->aid = index;
1294 strcpy(filename, basename);
1295 strcat(filename, ".sub");
1296 result->fsub = fopen(filename, "ab");
1297 if (result->fsub == NULL)
1298 perror("Error: vobsub_out_open subtitle file open failed");
1299 strcpy(filename, basename);
1300 strcat(filename, ".idx");
1301 result->fidx = fopen(filename, "ab");
1302 if (result->fidx) {
1303 if (ftell(result->fidx) == 0) {
1304 create_idx(result, palette, orig_width, orig_height);
1305 /* Make the selected language the default language */
1306 fprintf(result->fidx, "\n# Language index in use\nlangidx: %u\n", index);
1308 fprintf(result->fidx, "\nid: %s, index: %u\n", id ? id : "xx", index);
1309 /* So that we can check the file now */
1310 fflush(result->fidx);
1311 } else
1312 perror("Error: vobsub_out_open index file open failed");
1313 free(filename);
1316 return result;
1319 void vobsub_out_close(void *me)
1321 vobsub_out_t *vob = (vobsub_out_t*)me;
1322 if (vob->fidx)
1323 fclose(vob->fidx);
1324 if (vob->fsub)
1325 fclose(vob->fsub);
1326 free(vob);
1329 void vobsub_out_output(void *me, const unsigned char *packet,
1330 int len, double pts)
1332 static double last_pts;
1333 static int last_pts_set = 0;
1334 vobsub_out_t *vob = (vobsub_out_t*)me;
1335 if (vob->fsub) {
1336 /* Windows' Vobsub require that every packet is exactly 2kB long */
1337 unsigned char buffer[2048];
1338 unsigned char *p;
1339 int remain = 2048;
1340 /* Do not output twice a line with the same timestamp, this
1341 breaks Windows' Vobsub */
1342 if (vob->fidx && (!last_pts_set || last_pts != pts)) {
1343 static unsigned int last_h = 9999, last_m = 9999, last_s = 9999, last_ms = 9999;
1344 unsigned int h, m, ms;
1345 double s;
1346 s = pts;
1347 h = s / 3600;
1348 s -= h * 3600;
1349 m = s / 60;
1350 s -= m * 60;
1351 ms = (s - (unsigned int) s) * 1000;
1352 if (ms >= 1000) /* prevent overfolws or bad float stuff */
1353 ms = 0;
1354 if (h != last_h || m != last_m || (unsigned int) s != last_s || ms != last_ms) {
1355 fprintf(vob->fidx, "timestamp: %02u:%02u:%02u:%03u, filepos: %09lx\n",
1356 h, m, (unsigned int) s, ms, ftell(vob->fsub));
1357 last_h = h;
1358 last_m = m;
1359 last_s = (unsigned int) s;
1360 last_ms = ms;
1363 last_pts = pts;
1364 last_pts_set = 1;
1366 /* Packet start code: Windows' Vobsub needs this */
1367 p = buffer;
1368 *p++ = 0; /* 0x00 */
1369 *p++ = 0;
1370 *p++ = 1;
1371 *p++ = 0xba;
1372 *p++ = 0x40;
1373 memset(p, 0, 9);
1374 p += 9;
1375 { /* Packet */
1376 static unsigned char last_pts[5] = { 0, 0, 0, 0, 0};
1377 unsigned char now_pts[5];
1378 int pts_len, pad_len, datalen = len;
1379 pts *= 90000;
1380 now_pts[0] = 0x21 | (((unsigned long)pts >> 29) & 0x0e);
1381 now_pts[1] = ((unsigned long)pts >> 22) & 0xff;
1382 now_pts[2] = 0x01 | (((unsigned long)pts >> 14) & 0xfe);
1383 now_pts[3] = ((unsigned long)pts >> 7) & 0xff;
1384 now_pts[4] = 0x01 | (((unsigned long)pts << 1) & 0xfe);
1385 pts_len = memcmp(last_pts, now_pts, sizeof(now_pts)) ? sizeof(now_pts) : 0;
1386 memcpy(last_pts, now_pts, sizeof(now_pts));
1388 datalen += 3; /* Version, PTS_flags, pts_len */
1389 datalen += pts_len;
1390 datalen += 1; /* AID */
1391 pad_len = 2048 - (p - buffer) - 4 /* MPEG ID */ - 2 /* payload len */ - datalen;
1392 /* XXX - Go figure what should go here! In any case the
1393 packet has to be completly filled. If I can fill it
1394 with padding (0x000001be) latter I'll do that. But if
1395 there is only room for 6 bytes then I can not write a
1396 padding packet. So I add some padding in the PTS
1397 field. This looks like a dirty kludge. Oh well... */
1398 if (pad_len < 0) {
1399 /* Packet is too big. Let's try ommiting the PTS field */
1400 datalen -= pts_len;
1401 pts_len = 0;
1402 pad_len = 0;
1403 } else if (pad_len > 6)
1404 pad_len = 0;
1405 datalen += pad_len;
1407 *p++ = 0; /* 0x0e */
1408 *p++ = 0;
1409 *p++ = 1;
1410 *p++ = 0xbd;
1412 *p++ = (datalen >> 8) & 0xff; /* length of payload */
1413 *p++ = datalen & 0xff;
1414 *p++ = 0x80; /* System-2 (.VOB) stream */
1415 *p++ = pts_len ? 0x80 : 0x00; /* pts_flags */
1416 *p++ = pts_len + pad_len;
1417 memcpy(p, now_pts, pts_len);
1418 p += pts_len;
1419 memset(p, 0, pad_len);
1420 p += pad_len;
1422 *p++ = 0x20 | vob->aid; /* aid */
1423 if (fwrite(buffer, p - buffer, 1, vob->fsub) != 1
1424 || fwrite(packet, len, 1, vob->fsub) != 1)
1425 perror("ERROR: vobsub write failed");
1426 else
1427 remain -= p - buffer + len;
1429 /* Padding */
1430 if (remain >= 6) {
1431 p = buffer;
1432 *p++ = 0x00;
1433 *p++ = 0x00;
1434 *p++ = 0x01;
1435 *p++ = 0xbe;
1436 *p++ = (remain - 6) >> 8;
1437 *p++ = (remain - 6) & 0xff;
1438 /* for better compression, blank this */
1439 memset(buffer + 6, 0, remain - (p - buffer));
1440 if (fwrite(buffer, remain, 1, vob->fsub) != 1)
1441 perror("ERROR: vobsub padding write failed");
1442 } else if (remain > 0) {
1443 /* I don't know what to output. But anyway the block
1444 needs to be 2KB big */
1445 memset(buffer, 0, remain);
1446 if (fwrite(buffer, remain, 1, vob->fsub) != 1)
1447 perror("ERROR: vobsub blank padding write failed");
1448 } else if (remain < 0)
1449 fprintf(stderr,
1450 "\nERROR: wrong thing happenned...\n"
1451 " I wrote a %i data bytes spu packet and that's too long\n", len);