core: fix audio-only + framestep weird behavior
[mplayer/glamo.git] / vobsub.c
blob08efa3aceceeeb11bd4592b19c9c76e40c7f4924
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 "mpcommon.h"
36 #include "vobsub.h"
37 #include "spudec.h"
38 #include "mp_msg.h"
39 #include "path.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 p = mp_basename(filename);
101 rc = unrar_exec_get(&stream->data, &stream->size, p, rar_filename);
102 if (!rc) {
103 /* There is no matching filename in the archive. However, sometimes
104 * the files we are looking for have been given arbitrary names in the archive.
105 * Let's look for a file with an exact match in the extension only. */
106 int i, num_files, name_len;
107 ArchiveList_struct *list, *lp;
108 num_files = unrar_exec_list(rar_filename, &list);
109 if (num_files > 0) {
110 char *demanded_ext;
111 demanded_ext = strrchr (p, '.');
112 if (demanded_ext) {
113 int demanded_ext_len = strlen (demanded_ext);
114 for (i = 0, lp = list; i < num_files; i++, lp = lp->next) {
115 name_len = strlen (lp->item.Name);
116 if (name_len >= demanded_ext_len && !strcasecmp (lp->item.Name + name_len - demanded_ext_len, demanded_ext)) {
117 rc = unrar_exec_get(&stream->data, &stream->size,
118 lp->item.Name, rar_filename);
119 if (rc)
120 break;
124 unrar_exec_freelist(list);
126 if (!rc) {
127 free(rar_filename);
128 free(stream);
129 return NULL;
133 free(rar_filename);
134 stream->pos = 0;
136 return stream;
139 static int rar_close(rar_stream_t *stream)
141 if (stream->file)
142 return fclose(stream->file);
143 free(stream->data);
144 return 0;
147 static int rar_eof(rar_stream_t *stream)
149 if (stream->file)
150 return feof(stream->file);
151 return stream->pos >= stream->size;
154 static long rar_tell(rar_stream_t *stream)
156 if (stream->file)
157 return ftell(stream->file);
158 return stream->pos;
161 static int rar_seek(rar_stream_t *stream, long offset, int whence)
163 if (stream->file)
164 return fseek(stream->file, offset, whence);
165 switch (whence) {
166 case SEEK_SET:
167 if (offset < 0) {
168 errno = EINVAL;
169 return -1;
171 stream->pos = offset;
172 break;
173 case SEEK_CUR:
174 if (offset < 0 && stream->pos < (unsigned long) -offset) {
175 errno = EINVAL;
176 return -1;
178 stream->pos += offset;
179 break;
180 case SEEK_END:
181 if (offset < 0 && stream->size < (unsigned long) -offset) {
182 errno = EINVAL;
183 return -1;
185 stream->pos = stream->size + offset;
186 break;
187 default:
188 errno = EINVAL;
189 return -1;
191 return 0;
194 static int rar_getc(rar_stream_t *stream)
196 if (stream->file)
197 return getc(stream->file);
198 if (rar_eof(stream))
199 return EOF;
200 return stream->data[stream->pos++];
203 static size_t rar_read(void *ptr, size_t size, size_t nmemb,
204 rar_stream_t *stream)
206 size_t res;
207 unsigned long remain;
208 if (stream->file)
209 return fread(ptr, size, nmemb, stream->file);
210 if (rar_eof(stream))
211 return 0;
212 res = size * nmemb;
213 remain = stream->size - stream->pos;
214 if (res > remain)
215 res = remain / size * size;
216 memcpy(ptr, stream->data + stream->pos, res);
217 stream->pos += res;
218 res /= size;
219 return res;
222 #else
223 typedef FILE rar_stream_t;
224 #define rar_open fopen
225 #define rar_close fclose
226 #define rar_eof feof
227 #define rar_tell ftell
228 #define rar_seek fseek
229 #define rar_getc getc
230 #define rar_read fread
231 #endif
233 /**********************************************************************/
235 static ssize_t vobsub_getline(char **lineptr, size_t *n, rar_stream_t *stream)
237 size_t res = 0;
238 int c;
239 if (*lineptr == NULL) {
240 *lineptr = malloc(4096);
241 if (*lineptr)
242 *n = 4096;
243 } else if (*n == 0) {
244 char *tmp = realloc(*lineptr, 4096);
245 if (tmp) {
246 *lineptr = tmp;
247 *n = 4096;
250 if (*lineptr == NULL || *n == 0)
251 return -1;
253 for (c = rar_getc(stream); c != EOF; c = rar_getc(stream)) {
254 if (res + 1 >= *n) {
255 char *tmp = realloc(*lineptr, *n * 2);
256 if (tmp == NULL)
257 return -1;
258 *lineptr = tmp;
259 *n *= 2;
261 (*lineptr)[res++] = c;
262 if (c == '\n') {
263 (*lineptr)[res] = 0;
264 return res;
267 if (res == 0)
268 return -1;
269 (*lineptr)[res] = 0;
270 return res;
273 /**********************************************************************
274 * MPEG parsing
275 **********************************************************************/
277 typedef struct {
278 rar_stream_t *stream;
279 unsigned int pts;
280 int aid;
281 unsigned char *packet;
282 unsigned int packet_reserve;
283 unsigned int packet_size;
284 int padding_was_here;
285 int merge;
286 } mpeg_t;
288 static mpeg_t *mpeg_open(const char *filename)
290 mpeg_t *res = malloc(sizeof(mpeg_t));
291 int err = res == NULL;
292 if (!err) {
293 res->pts = 0;
294 res->aid = -1;
295 res->packet = NULL;
296 res->packet_size = 0;
297 res->packet_reserve = 0;
298 res->padding_was_here = 1;
299 res->merge = 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 free(mpeg->packet);
313 if (mpeg->stream)
314 rar_close(mpeg->stream);
315 free(mpeg);
318 static int mpeg_eof(mpeg_t *mpeg)
320 return rar_eof(mpeg->stream);
323 static off_t mpeg_tell(mpeg_t *mpeg)
325 return rar_tell(mpeg->stream);
328 static int mpeg_run(mpeg_t *mpeg)
330 unsigned int len, idx, version;
331 int c;
332 /* Goto start of a packet, it starts with 0x000001?? */
333 const unsigned char wanted[] = { 0, 0, 1 };
334 unsigned char buf[5];
336 mpeg->aid = -1;
337 mpeg->packet_size = 0;
338 if (rar_read(buf, 4, 1, mpeg->stream) != 1)
339 return -1;
340 while (memcmp(buf, wanted, sizeof(wanted)) != 0) {
341 c = rar_getc(mpeg->stream);
342 if (c < 0)
343 return -1;
344 memmove(buf, buf + 1, 3);
345 buf[3] = c;
347 switch (buf[3]) {
348 case 0xb9: /* System End Code */
349 break;
350 case 0xba: /* Packet start code */
351 c = rar_getc(mpeg->stream);
352 if (c < 0)
353 return -1;
354 if ((c & 0xc0) == 0x40)
355 version = 4;
356 else if ((c & 0xf0) == 0x20)
357 version = 2;
358 else {
359 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Unsupported MPEG version: 0x%02x\n", c);
360 return -1;
362 if (version == 4) {
363 if (rar_seek(mpeg->stream, 9, SEEK_CUR))
364 return -1;
365 } else if (version == 2) {
366 if (rar_seek(mpeg->stream, 7, SEEK_CUR))
367 return -1;
368 } else
369 abort();
370 if (!mpeg->padding_was_here)
371 mpeg->merge = 1;
372 break;
373 case 0xbd: /* packet */
374 if (rar_read(buf, 2, 1, mpeg->stream) != 1)
375 return -1;
376 mpeg->padding_was_here = 0;
377 len = buf[0] << 8 | buf[1];
378 idx = mpeg_tell(mpeg);
379 c = rar_getc(mpeg->stream);
380 if (c < 0)
381 return -1;
382 if ((c & 0xC0) == 0x40) { /* skip STD scale & size */
383 if (rar_getc(mpeg->stream) < 0)
384 return -1;
385 c = rar_getc(mpeg->stream);
386 if (c < 0)
387 return -1;
389 if ((c & 0xf0) == 0x20) { /* System-1 stream timestamp */
390 /* Do we need this? */
391 abort();
392 } else if ((c & 0xf0) == 0x30) {
393 /* Do we need this? */
394 abort();
395 } else if ((c & 0xc0) == 0x80) { /* System-2 (.VOB) stream */
396 unsigned int pts_flags, hdrlen, dataidx;
397 c = rar_getc(mpeg->stream);
398 if (c < 0)
399 return -1;
400 pts_flags = c;
401 c = rar_getc(mpeg->stream);
402 if (c < 0)
403 return -1;
404 hdrlen = c;
405 dataidx = mpeg_tell(mpeg) + hdrlen;
406 if (dataidx > idx + len) {
407 mp_msg(MSGT_VOBSUB, MSGL_ERR, "Invalid header length: %d (total length: %d, idx: %d, dataidx: %d)\n",
408 hdrlen, len, idx, dataidx);
409 return -1;
411 if ((pts_flags & 0xc0) == 0x80) {
412 if (rar_read(buf, 5, 1, mpeg->stream) != 1)
413 return -1;
414 if (!(((buf[0] & 0xf0) == 0x20) && (buf[0] & 1) && (buf[2] & 1) && (buf[4] & 1))) {
415 mp_msg(MSGT_VOBSUB, MSGL_ERR, "vobsub PTS error: 0x%02x %02x%02x %02x%02x \n",
416 buf[0], buf[1], buf[2], buf[3], buf[4]);
417 mpeg->pts = 0;
418 } else
419 mpeg->pts = ((buf[0] & 0x0e) << 29 | buf[1] << 22 | (buf[2] & 0xfe) << 14
420 | buf[3] << 7 | (buf[4] >> 1));
421 } else /* if ((pts_flags & 0xc0) == 0xc0) */ {
422 /* what's this? */
423 /* abort(); */
425 rar_seek(mpeg->stream, dataidx, SEEK_SET);
426 mpeg->aid = rar_getc(mpeg->stream);
427 if (mpeg->aid < 0) {
428 mp_msg(MSGT_VOBSUB, MSGL_ERR, "Bogus aid %d\n", mpeg->aid);
429 return -1;
431 mpeg->packet_size = len - ((unsigned int) mpeg_tell(mpeg) - idx);
432 if (mpeg->packet_reserve < mpeg->packet_size) {
433 free(mpeg->packet);
434 mpeg->packet = malloc(mpeg->packet_size);
435 if (mpeg->packet)
436 mpeg->packet_reserve = mpeg->packet_size;
438 if (mpeg->packet == NULL) {
439 mp_msg(MSGT_VOBSUB, MSGL_FATAL, "malloc failure");
440 mpeg->packet_reserve = 0;
441 mpeg->packet_size = 0;
442 return -1;
444 if (rar_read(mpeg->packet, mpeg->packet_size, 1, mpeg->stream) != 1) {
445 mp_msg(MSGT_VOBSUB, MSGL_ERR, "fread failure");
446 mpeg->packet_size = 0;
447 return -1;
449 idx = len;
451 break;
452 case 0xbe: /* Padding */
453 if (rar_read(buf, 2, 1, mpeg->stream) != 1)
454 return -1;
455 len = buf[0] << 8 | buf[1];
456 if (len > 0 && rar_seek(mpeg->stream, len, SEEK_CUR))
457 return -1;
458 mpeg->padding_was_here = 1;
459 break;
460 default:
461 if (0xc0 <= buf[3] && buf[3] < 0xf0) {
462 /* MPEG audio or video */
463 if (rar_read(buf, 2, 1, mpeg->stream) != 1)
464 return -1;
465 len = buf[0] << 8 | buf[1];
466 if (len > 0 && rar_seek(mpeg->stream, len, SEEK_CUR))
467 return -1;
468 } else {
469 mp_msg(MSGT_VOBSUB, MSGL_ERR, "unknown header 0x%02X%02X%02X%02X\n",
470 buf[0], buf[1], buf[2], buf[3]);
471 return -1;
474 return 0;
477 /**********************************************************************
478 * Packet queue
479 **********************************************************************/
481 typedef struct {
482 unsigned int pts100;
483 off_t filepos;
484 unsigned int size;
485 unsigned char *data;
486 } packet_t;
488 typedef struct {
489 char *id;
490 packet_t *packets;
491 unsigned int packets_reserve;
492 unsigned int packets_size;
493 unsigned int current_index;
494 } packet_queue_t;
496 static void packet_construct(packet_t *pkt)
498 pkt->pts100 = 0;
499 pkt->filepos = 0;
500 pkt->size = 0;
501 pkt->data = NULL;
504 static void packet_destroy(packet_t *pkt)
506 free(pkt->data);
509 static void packet_queue_construct(packet_queue_t *queue)
511 queue->id = NULL;
512 queue->packets = NULL;
513 queue->packets_reserve = 0;
514 queue->packets_size = 0;
515 queue->current_index = 0;
518 static void packet_queue_destroy(packet_queue_t *queue)
520 if (queue->packets) {
521 while (queue->packets_size--)
522 packet_destroy(queue->packets + queue->packets_size);
523 free(queue->packets);
525 return;
528 /* Make sure there is enough room for needed_size packets in the
529 packet queue. */
530 static int packet_queue_ensure(packet_queue_t *queue, unsigned int needed_size)
532 if (queue->packets_reserve < needed_size) {
533 if (queue->packets) {
534 packet_t *tmp = realloc(queue->packets, 2 * queue->packets_reserve * sizeof(packet_t));
535 if (tmp == NULL) {
536 mp_msg(MSGT_VOBSUB, MSGL_FATAL, "realloc failure");
537 return -1;
539 queue->packets = tmp;
540 queue->packets_reserve *= 2;
541 } else {
542 queue->packets = malloc(sizeof(packet_t));
543 if (queue->packets == NULL) {
544 mp_msg(MSGT_VOBSUB, MSGL_FATAL, "malloc failure");
545 return -1;
547 queue->packets_reserve = 1;
550 return 0;
553 /* add one more packet */
554 static int packet_queue_grow(packet_queue_t *queue)
556 if (packet_queue_ensure(queue, queue->packets_size + 1) < 0)
557 return -1;
558 packet_construct(queue->packets + queue->packets_size);
559 ++queue->packets_size;
560 return 0;
563 /* insert a new packet, duplicating pts from the current one */
564 static int packet_queue_insert(packet_queue_t *queue)
566 packet_t *pkts;
567 if (packet_queue_ensure(queue, queue->packets_size + 1) < 0)
568 return -1;
569 /* XXX packet_size does not reflect the real thing here, it will be updated a bit later */
570 memmove(queue->packets + queue->current_index + 2,
571 queue->packets + queue->current_index + 1,
572 sizeof(packet_t) * (queue->packets_size - queue->current_index - 1));
573 pkts = queue->packets + queue->current_index;
574 ++queue->packets_size;
575 ++queue->current_index;
576 packet_construct(pkts + 1);
577 pkts[1].pts100 = pkts[0].pts100;
578 pkts[1].filepos = pkts[0].filepos;
579 return 0;
582 /**********************************************************************
583 * Vobsub
584 **********************************************************************/
586 typedef struct {
587 unsigned int palette[16];
588 int delay;
589 unsigned int have_palette;
590 unsigned int orig_frame_width, orig_frame_height;
591 unsigned int origin_x, origin_y;
592 /* index */
593 packet_queue_t *spu_streams;
594 unsigned int spu_streams_size;
595 unsigned int spu_streams_current;
596 unsigned int spu_valid_streams_size;
597 } vobsub_t;
599 /* Make sure that the spu stream idx exists. */
600 static int vobsub_ensure_spu_stream(vobsub_t *vob, unsigned int index)
602 if (index >= vob->spu_streams_size) {
603 /* This is a new stream */
604 if (vob->spu_streams) {
605 packet_queue_t *tmp = realloc(vob->spu_streams, (index + 1) * sizeof(packet_queue_t));
606 if (tmp == NULL) {
607 mp_msg(MSGT_VOBSUB, MSGL_ERR, "vobsub_ensure_spu_stream: realloc failure");
608 return -1;
610 vob->spu_streams = tmp;
611 } else {
612 vob->spu_streams = malloc((index + 1) * sizeof(packet_queue_t));
613 if (vob->spu_streams == NULL) {
614 mp_msg(MSGT_VOBSUB, MSGL_ERR, "vobsub_ensure_spu_stream: malloc failure");
615 return -1;
618 while (vob->spu_streams_size <= index) {
619 packet_queue_construct(vob->spu_streams + vob->spu_streams_size);
620 ++vob->spu_streams_size;
623 return 0;
626 static int vobsub_add_id(vobsub_t *vob, const char *id, size_t idlen,
627 const unsigned int index)
629 if (vobsub_ensure_spu_stream(vob, index) < 0)
630 return -1;
631 if (id && idlen) {
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 free(line);
864 return res;
867 int vobsub_parse_ifo(void* this, const char *const name, unsigned int *palette,
868 unsigned int *width, unsigned int *height, int force,
869 int sid, char *langid)
871 vobsub_t *vob = this;
872 int res = -1;
873 rar_stream_t *fd = rar_open(name, "rb");
874 if (fd == NULL) {
875 if (force)
876 mp_msg(MSGT_VOBSUB, MSGL_WARN, "VobSub: Can't open IFO file\n");
877 } else {
878 // parse IFO header
879 unsigned char block[0x800];
880 const char *const ifo_magic = "DVDVIDEO-VTS";
881 if (rar_read(block, sizeof(block), 1, fd) != 1) {
882 if (force)
883 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Can't read IFO header\n");
884 } else if (memcmp(block, ifo_magic, strlen(ifo_magic) + 1))
885 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Bad magic in IFO header\n");
886 else {
887 unsigned long pgci_sector = block[0xcc] << 24 | block[0xcd] << 16
888 | block[0xce] << 8 | block[0xcf];
889 int standard = (block[0x200] & 0x30) >> 4;
890 int resolution = (block[0x201] & 0x0c) >> 2;
891 *height = standard ? 576 : 480;
892 *width = 0;
893 switch (resolution) {
894 case 0x0:
895 *width = 720;
896 break;
897 case 0x1:
898 *width = 704;
899 break;
900 case 0x2:
901 *width = 352;
902 break;
903 case 0x3:
904 *width = 352;
905 *height /= 2;
906 break;
907 default:
908 mp_msg(MSGT_VOBSUB, MSGL_WARN, "Vobsub: Unknown resolution %d \n", resolution);
910 if (langid && 0 <= sid && sid < 32) {
911 unsigned char *tmp = block + 0x256 + sid * 6 + 2;
912 langid[0] = tmp[0];
913 langid[1] = tmp[1];
914 langid[2] = 0;
916 if (rar_seek(fd, pgci_sector * sizeof(block), SEEK_SET)
917 || rar_read(block, sizeof(block), 1, fd) != 1)
918 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Can't read IFO PGCI\n");
919 else {
920 unsigned long idx;
921 unsigned long pgc_offset = block[0xc] << 24 | block[0xd] << 16
922 | block[0xe] << 8 | block[0xf];
923 for (idx = 0; idx < 16; ++idx) {
924 unsigned char *p = block + pgc_offset + 0xa4 + 4 * idx;
925 palette[idx] = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
927 if (vob)
928 vob->have_palette = 1;
929 res = 0;
932 rar_close(fd);
934 return res;
937 void *vobsub_open(const char *const name, const char *const ifo,
938 const int force, void** spu)
940 unsigned char *extradata = NULL;
941 unsigned int extradata_len = 0;
942 vobsub_t *vob = calloc(1, sizeof(vobsub_t));
943 if (spu)
944 *spu = NULL;
945 if (vobsubid == -2)
946 vobsubid = vobsub_id;
947 if (vob) {
948 char *buf;
949 buf = malloc(strlen(name) + 5);
950 if (buf) {
951 rar_stream_t *fd;
952 mpeg_t *mpg;
953 /* read in the info file */
954 if (!ifo) {
955 strcpy(buf, name);
956 strcat(buf, ".ifo");
957 vobsub_parse_ifo(vob, buf, vob->palette, &vob->orig_frame_width, &vob->orig_frame_height, force, -1, NULL);
958 } else
959 vobsub_parse_ifo(vob, ifo, vob->palette, &vob->orig_frame_width, &vob->orig_frame_height, force, -1, NULL);
960 /* read in the index */
961 strcpy(buf, name);
962 strcat(buf, ".idx");
963 fd = rar_open(buf, "rb");
964 if (fd == NULL) {
965 if (force)
966 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Can't open IDX file\n");
967 else {
968 free(buf);
969 free(vob);
970 return NULL;
972 } else {
973 while (vobsub_parse_one_line(vob, fd, &extradata, &extradata_len) >= 0)
974 /* NOOP */ ;
975 rar_close(fd);
977 if (spu)
978 *spu = spudec_new_scaled(vob->palette, vob->orig_frame_width, vob->orig_frame_height, extradata, extradata_len);
979 free(extradata);
981 /* read the indexed mpeg_stream */
982 strcpy(buf, name);
983 strcat(buf, ".sub");
984 mpg = mpeg_open(buf);
985 if (mpg == NULL) {
986 if (force)
987 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Can't open SUB file\n");
988 else {
989 free(buf);
990 free(vob);
991 return NULL;
993 } else {
994 long last_pts_diff = 0;
995 while (!mpeg_eof(mpg)) {
996 off_t pos = mpeg_tell(mpg);
997 if (mpeg_run(mpg) < 0) {
998 if (!mpeg_eof(mpg))
999 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: mpeg_run error\n");
1000 break;
1002 if (mpg->packet_size) {
1003 if ((mpg->aid & 0xe0) == 0x20) {
1004 unsigned int sid = mpg->aid & 0x1f;
1005 if (vobsub_ensure_spu_stream(vob, sid) >= 0) {
1006 packet_queue_t *queue = vob->spu_streams + sid;
1007 /* get the packet to fill */
1008 if (queue->packets_size == 0 && packet_queue_grow(queue) < 0)
1009 abort();
1010 while (queue->current_index + 1 < queue->packets_size
1011 && queue->packets[queue->current_index + 1].filepos <= pos)
1012 ++queue->current_index;
1013 if (queue->current_index < queue->packets_size) {
1014 packet_t *pkt;
1015 if (queue->packets[queue->current_index].data) {
1016 /* insert a new packet and fix the PTS ! */
1017 packet_queue_insert(queue);
1018 queue->packets[queue->current_index].pts100 =
1019 mpg->pts + last_pts_diff;
1021 pkt = queue->packets + queue->current_index;
1022 if (pkt->pts100 != UINT_MAX) {
1023 if (queue->packets_size > 1)
1024 last_pts_diff = pkt->pts100 - mpg->pts;
1025 else
1026 pkt->pts100 = mpg->pts;
1027 if (mpg->merge && queue->current_index > 0) {
1028 packet_t *last = &queue->packets[queue->current_index - 1];
1029 pkt->pts100 = last->pts100;
1031 mpg->merge = 0;
1032 /* FIXME: should not use mpg_sub internal informations, make a copy */
1033 pkt->data = mpg->packet;
1034 pkt->size = mpg->packet_size;
1035 mpg->packet = NULL;
1036 mpg->packet_reserve = 0;
1037 mpg->packet_size = 0;
1040 } else
1041 mp_msg(MSGT_VOBSUB, MSGL_WARN, "don't know what to do with subtitle #%u\n", sid);
1045 vob->spu_streams_current = vob->spu_streams_size;
1046 while (vob->spu_streams_current-- > 0) {
1047 vob->spu_streams[vob->spu_streams_current].current_index = 0;
1048 if (vobsubid == vob->spu_streams_current ||
1049 vob->spu_streams[vob->spu_streams_current].packets_size > 0)
1050 ++vob->spu_valid_streams_size;
1052 mpeg_free(mpg);
1054 free(buf);
1057 return vob;
1060 void vobsub_close(void *this)
1062 vobsub_t *vob = this;
1063 if (vob->spu_streams) {
1064 while (vob->spu_streams_size--)
1065 packet_queue_destroy(vob->spu_streams + vob->spu_streams_size);
1066 free(vob->spu_streams);
1068 free(vob);
1071 unsigned int vobsub_get_indexes_count(void *vobhandle)
1073 vobsub_t *vob = vobhandle;
1074 return vob->spu_valid_streams_size;
1077 char *vobsub_get_id(void *vobhandle, unsigned int index)
1079 vobsub_t *vob = vobhandle;
1080 return (index < vob->spu_streams_size) ? vob->spu_streams[index].id : NULL;
1083 int vobsub_get_id_by_index(void *vobhandle, unsigned int index)
1085 vobsub_t *vob = vobhandle;
1086 int i, j;
1087 if (vob == NULL)
1088 return -1;
1089 for (i = 0, j = 0; i < vob->spu_streams_size; ++i)
1090 if (i == vobsubid || vob->spu_streams[i].packets_size > 0) {
1091 if (j == index)
1092 return i;
1093 ++j;
1095 return -1;
1098 int vobsub_get_index_by_id(void *vobhandle, int id)
1100 vobsub_t *vob = vobhandle;
1101 int i, j;
1102 if (vob == NULL || id < 0 || id >= vob->spu_streams_size)
1103 return -1;
1104 if (id != vobsubid && !vob->spu_streams[id].packets_size)
1105 return -1;
1106 for (i = 0, j = 0; i < id; ++i)
1107 if (i == vobsubid || vob->spu_streams[i].packets_size > 0)
1108 ++j;
1109 return j;
1112 int vobsub_set_from_lang(void *vobhandle, unsigned char * lang)
1114 int i;
1115 vobsub_t *vob= vobhandle;
1116 while (lang && strlen(lang) >= 2) {
1117 for (i = 0; i < vob->spu_streams_size; i++)
1118 if (vob->spu_streams[i].id)
1119 if ((strncmp(vob->spu_streams[i].id, lang, 2) == 0)) {
1120 vobsub_id = i;
1121 mp_msg(MSGT_VOBSUB, MSGL_INFO, "Selected VOBSUB language: %d language: %s\n", i, vob->spu_streams[i].id);
1122 return 0;
1124 lang+=2;while (lang[0]==',' || lang[0]==' ') ++lang;
1126 mp_msg(MSGT_VOBSUB, MSGL_WARN, "No matching VOBSUB language found!\n");
1127 return -1;
1130 /// make sure we seek to the first packet of packets having same pts values.
1131 static void vobsub_queue_reseek(packet_queue_t *queue, unsigned int pts100)
1133 int reseek_count = 0;
1134 unsigned int lastpts = 0;
1136 if (queue->current_index > 0
1137 && (queue->packets[queue->current_index].pts100 == UINT_MAX
1138 || queue->packets[queue->current_index].pts100 > pts100)) {
1139 // possible pts seek previous, try to check it.
1140 int i = 1;
1141 while (queue->current_index >= i
1142 && queue->packets[queue->current_index-i].pts100 == UINT_MAX)
1143 ++i;
1144 if (queue->current_index >= i
1145 && queue->packets[queue->current_index-i].pts100 > pts100)
1146 // pts seek previous confirmed, reseek from beginning
1147 queue->current_index = 0;
1149 while (queue->current_index < queue->packets_size
1150 && queue->packets[queue->current_index].pts100 <= pts100) {
1151 lastpts = queue->packets[queue->current_index].pts100;
1152 ++queue->current_index;
1153 ++reseek_count;
1155 while (reseek_count-- && --queue->current_index) {
1156 if (queue->packets[queue->current_index-1].pts100 != UINT_MAX &&
1157 queue->packets[queue->current_index-1].pts100 != lastpts)
1158 break;
1162 int vobsub_get_packet(void *vobhandle, float pts, void** data, int* timestamp)
1164 vobsub_t *vob = vobhandle;
1165 unsigned int pts100 = 90000 * pts;
1166 if (vob->spu_streams && 0 <= vobsub_id && (unsigned) vobsub_id < vob->spu_streams_size) {
1167 packet_queue_t *queue = vob->spu_streams + vobsub_id;
1169 vobsub_queue_reseek(queue, pts100);
1171 while (queue->current_index < queue->packets_size) {
1172 packet_t *pkt = queue->packets + queue->current_index;
1173 if (pkt->pts100 != UINT_MAX)
1174 if (pkt->pts100 <= pts100) {
1175 ++queue->current_index;
1176 *data = pkt->data;
1177 *timestamp = pkt->pts100;
1178 return pkt->size;
1179 } else
1180 break;
1181 else
1182 ++queue->current_index;
1185 return -1;
1188 int vobsub_get_next_packet(void *vobhandle, void** data, int* timestamp)
1190 vobsub_t *vob = vobhandle;
1191 if (vob->spu_streams && 0 <= vobsub_id && (unsigned) vobsub_id < vob->spu_streams_size) {
1192 packet_queue_t *queue = vob->spu_streams + vobsub_id;
1193 if (queue->current_index < queue->packets_size) {
1194 packet_t *pkt = queue->packets + queue->current_index;
1195 ++queue->current_index;
1196 *data = pkt->data;
1197 *timestamp = pkt->pts100;
1198 return pkt->size;
1201 return -1;
1204 void vobsub_seek(void * vobhandle, float pts)
1206 vobsub_t * vob = vobhandle;
1207 packet_queue_t * queue;
1208 int seek_pts100 = pts * 90000;
1210 if (vob->spu_streams && 0 <= vobsub_id && (unsigned) vobsub_id < vob->spu_streams_size) {
1211 /* do not seek if we don't know the id */
1212 if (vobsub_get_id(vob, vobsub_id) == NULL)
1213 return;
1214 queue = vob->spu_streams + vobsub_id;
1215 queue->current_index = 0;
1216 vobsub_queue_reseek(queue, seek_pts100);
1220 void vobsub_reset(void *vobhandle)
1222 vobsub_t *vob = vobhandle;
1223 if (vob->spu_streams) {
1224 unsigned int n = vob->spu_streams_size;
1225 while (n-- > 0)
1226 vob->spu_streams[n].current_index = 0;
1230 /**********************************************************************
1231 * Vobsub output
1232 **********************************************************************/
1234 typedef struct {
1235 FILE *fsub;
1236 FILE *fidx;
1237 unsigned int aid;
1238 } vobsub_out_t;
1240 static void create_idx(vobsub_out_t *me, const unsigned int *palette,
1241 unsigned int orig_width, unsigned int orig_height)
1243 int i;
1244 fprintf(me->fidx,
1245 "# VobSub index file, v7 (do not modify this line!)\n"
1246 "#\n"
1247 "# Generated by %s\n"
1248 "# See <URL:http://www.mplayerhq.hu/> for more information about MPlayer\n"
1249 "# See <URL:http://wiki.multimedia.cx/index.php?title=VOBsub> for more information about Vobsub\n"
1250 "#\n"
1251 "size: %ux%u\n",
1252 mplayer_version, orig_width, orig_height);
1253 if (palette) {
1254 fputs("palette:", me->fidx);
1255 for (i = 0; i < 16; ++i) {
1256 const double y = palette[i] >> 16 & 0xff,
1257 u = (palette[i] >> 8 & 0xff) - 128.0,
1258 v = (palette[i] & 0xff) - 128.0;
1259 if (i)
1260 putc(',', me->fidx);
1261 fprintf(me->fidx, " %02x%02x%02x",
1262 av_clip_uint8(y + 1.4022 * u),
1263 av_clip_uint8(y - 0.3456 * u - 0.7145 * v),
1264 av_clip_uint8(y + 1.7710 * v));
1266 putc('\n', me->fidx);
1269 fprintf(me->fidx, "# ON: displays only forced subtitles, OFF: shows everything\n"
1270 "forced subs: OFF\n");
1273 void *vobsub_out_open(const char *basename, const unsigned int *palette,
1274 unsigned int orig_width, unsigned int orig_height,
1275 const char *id, unsigned int index)
1277 vobsub_out_t *result = NULL;
1278 char *filename;
1279 filename = malloc(strlen(basename) + 5);
1280 if (filename) {
1281 result = malloc(sizeof(vobsub_out_t));
1282 if (result) {
1283 result->aid = index;
1284 strcpy(filename, basename);
1285 strcat(filename, ".sub");
1286 result->fsub = fopen(filename, "ab");
1287 if (result->fsub == NULL)
1288 perror("Error: vobsub_out_open subtitle file open failed");
1289 strcpy(filename, basename);
1290 strcat(filename, ".idx");
1291 result->fidx = fopen(filename, "ab");
1292 if (result->fidx) {
1293 if (ftell(result->fidx) == 0) {
1294 create_idx(result, palette, orig_width, orig_height);
1295 /* Make the selected language the default language */
1296 fprintf(result->fidx, "\n# Language index in use\nlangidx: %u\n", index);
1298 fprintf(result->fidx, "\nid: %s, index: %u\n", id ? id : "xx", index);
1299 /* So that we can check the file now */
1300 fflush(result->fidx);
1301 } else
1302 perror("Error: vobsub_out_open index file open failed");
1303 free(filename);
1306 return result;
1309 void vobsub_out_close(void *me)
1311 vobsub_out_t *vob = me;
1312 if (vob->fidx)
1313 fclose(vob->fidx);
1314 if (vob->fsub)
1315 fclose(vob->fsub);
1316 free(vob);
1319 void vobsub_out_output(void *me, const unsigned char *packet,
1320 int len, double pts)
1322 static double last_pts;
1323 static int last_pts_set = 0;
1324 vobsub_out_t *vob = me;
1325 if (vob->fsub) {
1326 /* Windows' Vobsub require that every packet is exactly 2kB long */
1327 unsigned char buffer[2048];
1328 unsigned char *p;
1329 int remain = 2048;
1330 /* Do not output twice a line with the same timestamp, this
1331 breaks Windows' Vobsub */
1332 if (vob->fidx && (!last_pts_set || last_pts != pts)) {
1333 static unsigned int last_h = 9999, last_m = 9999, last_s = 9999, last_ms = 9999;
1334 unsigned int h, m, ms;
1335 double s;
1336 s = pts;
1337 h = s / 3600;
1338 s -= h * 3600;
1339 m = s / 60;
1340 s -= m * 60;
1341 ms = (s - (unsigned int) s) * 1000;
1342 if (ms >= 1000) /* prevent overfolws or bad float stuff */
1343 ms = 0;
1344 if (h != last_h || m != last_m || (unsigned int) s != last_s || ms != last_ms) {
1345 fprintf(vob->fidx, "timestamp: %02u:%02u:%02u:%03u, filepos: %09lx\n",
1346 h, m, (unsigned int) s, ms, ftell(vob->fsub));
1347 last_h = h;
1348 last_m = m;
1349 last_s = (unsigned int) s;
1350 last_ms = ms;
1353 last_pts = pts;
1354 last_pts_set = 1;
1356 /* Packet start code: Windows' Vobsub needs this */
1357 p = buffer;
1358 *p++ = 0; /* 0x00 */
1359 *p++ = 0;
1360 *p++ = 1;
1361 *p++ = 0xba;
1362 *p++ = 0x40;
1363 memset(p, 0, 9);
1364 p += 9;
1365 { /* Packet */
1366 static unsigned char last_pts[5] = { 0, 0, 0, 0, 0};
1367 unsigned char now_pts[5];
1368 int pts_len, pad_len, datalen = len;
1369 pts *= 90000;
1370 now_pts[0] = 0x21 | (((unsigned long)pts >> 29) & 0x0e);
1371 now_pts[1] = ((unsigned long)pts >> 22) & 0xff;
1372 now_pts[2] = 0x01 | (((unsigned long)pts >> 14) & 0xfe);
1373 now_pts[3] = ((unsigned long)pts >> 7) & 0xff;
1374 now_pts[4] = 0x01 | (((unsigned long)pts << 1) & 0xfe);
1375 pts_len = memcmp(last_pts, now_pts, sizeof(now_pts)) ? sizeof(now_pts) : 0;
1376 memcpy(last_pts, now_pts, sizeof(now_pts));
1378 datalen += 3; /* Version, PTS_flags, pts_len */
1379 datalen += pts_len;
1380 datalen += 1; /* AID */
1381 pad_len = 2048 - (p - buffer) - 4 /* MPEG ID */ - 2 /* payload len */ - datalen;
1382 /* XXX - Go figure what should go here! In any case the
1383 packet has to be completly filled. If I can fill it
1384 with padding (0x000001be) latter I'll do that. But if
1385 there is only room for 6 bytes then I can not write a
1386 padding packet. So I add some padding in the PTS
1387 field. This looks like a dirty kludge. Oh well... */
1388 if (pad_len < 0) {
1389 /* Packet is too big. Let's try ommiting the PTS field */
1390 datalen -= pts_len;
1391 pts_len = 0;
1392 pad_len = 0;
1393 } else if (pad_len > 6)
1394 pad_len = 0;
1395 datalen += pad_len;
1397 *p++ = 0; /* 0x0e */
1398 *p++ = 0;
1399 *p++ = 1;
1400 *p++ = 0xbd;
1402 *p++ = (datalen >> 8) & 0xff; /* length of payload */
1403 *p++ = datalen & 0xff;
1404 *p++ = 0x80; /* System-2 (.VOB) stream */
1405 *p++ = pts_len ? 0x80 : 0x00; /* pts_flags */
1406 *p++ = pts_len + pad_len;
1407 memcpy(p, now_pts, pts_len);
1408 p += pts_len;
1409 memset(p, 0, pad_len);
1410 p += pad_len;
1412 *p++ = 0x20 | vob->aid; /* aid */
1413 if (fwrite(buffer, p - buffer, 1, vob->fsub) != 1
1414 || fwrite(packet, len, 1, vob->fsub) != 1)
1415 perror("ERROR: vobsub write failed");
1416 else
1417 remain -= p - buffer + len;
1419 /* Padding */
1420 if (remain >= 6) {
1421 p = buffer;
1422 *p++ = 0x00;
1423 *p++ = 0x00;
1424 *p++ = 0x01;
1425 *p++ = 0xbe;
1426 *p++ = (remain - 6) >> 8;
1427 *p++ = (remain - 6) & 0xff;
1428 /* for better compression, blank this */
1429 memset(buffer + 6, 0, remain - (p - buffer));
1430 if (fwrite(buffer, remain, 1, vob->fsub) != 1)
1431 perror("ERROR: vobsub padding write failed");
1432 } else if (remain > 0) {
1433 /* I don't know what to output. But anyway the block
1434 needs to be 2KB big */
1435 memset(buffer, 0, remain);
1436 if (fwrite(buffer, remain, 1, vob->fsub) != 1)
1437 perror("ERROR: vobsub blank padding write failed");
1438 } else if (remain < 0)
1439 fprintf(stderr,
1440 "\nERROR: wrong thing happenned...\n"
1441 " I wrote a %i data bytes spu packet and that's too long\n", len);