stream/http: Do not keep authentication in URL when proxied
[mplayer.git] / vobsub.c
blobe9868b3f2c63aaa7f01c7fdbb2d345363fa4ba28
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 "unrar_exec.h"
40 #include "libavutil/common.h"
42 extern int vobsub_id;
43 // Record the original -vobsubid set by commandline, since vobsub_id will be
44 // overridden if slang match any of vobsub streams.
45 static int vobsubid = -2;
47 /**********************************************************************
48 * RAR stream handling
49 * The RAR file must have the same basename as the file to open
50 **********************************************************************/
51 #ifdef CONFIG_UNRAR_EXEC
52 typedef struct {
53 FILE *file;
54 unsigned char *data;
55 unsigned long size;
56 unsigned long pos;
57 } rar_stream_t;
59 static rar_stream_t *rar_open(const char *const filename,
60 const char *const mode)
62 rar_stream_t *stream;
63 /* unrar_exec can only read */
64 if (strcmp("r", mode) && strcmp("rb", mode)) {
65 errno = EINVAL;
66 return NULL;
68 stream = malloc(sizeof(rar_stream_t));
69 if (stream == NULL)
70 return NULL;
71 /* first try normal access */
72 stream->file = fopen(filename, mode);
73 if (stream->file == NULL) {
74 char *rar_filename;
75 const char *p;
76 int rc;
77 /* Guess the RAR archive filename */
78 rar_filename = NULL;
79 p = strrchr(filename, '.');
80 if (p) {
81 ptrdiff_t l = p - filename;
82 rar_filename = malloc(l + 5);
83 if (rar_filename == NULL) {
84 free(stream);
85 return NULL;
87 strncpy(rar_filename, filename, l);
88 strcpy(rar_filename + l, ".rar");
89 } else {
90 rar_filename = malloc(strlen(filename) + 5);
91 if (rar_filename == NULL) {
92 free(stream);
93 return NULL;
95 strcpy(rar_filename, filename);
96 strcat(rar_filename, ".rar");
98 /* get rid of the path if there is any */
99 if ((p = strrchr(filename, '/')) == NULL) {
100 p = filename;
101 } else {
102 p++;
104 rc = unrar_exec_get(&stream->data, &stream->size, p, rar_filename);
105 if (!rc) {
106 /* There is no matching filename in the archive. However, sometimes
107 * the files we are looking for have been given arbitrary names in the archive.
108 * Let's look for a file with an exact match in the extension only. */
109 int i, num_files, name_len;
110 ArchiveList_struct *list, *lp;
111 num_files = unrar_exec_list(rar_filename, &list);
112 if (num_files > 0) {
113 char *demanded_ext;
114 demanded_ext = strrchr (p, '.');
115 if (demanded_ext) {
116 int demanded_ext_len = strlen (demanded_ext);
117 for (i = 0, lp = list; i < num_files; i++, lp = lp->next) {
118 name_len = strlen (lp->item.Name);
119 if (name_len >= demanded_ext_len && !strcasecmp (lp->item.Name + name_len - demanded_ext_len, demanded_ext)) {
120 rc = unrar_exec_get(&stream->data, &stream->size,
121 lp->item.Name, rar_filename);
122 if (rc)
123 break;
127 unrar_exec_freelist(list);
129 if (!rc) {
130 free(rar_filename);
131 free(stream);
132 return NULL;
136 free(rar_filename);
137 stream->pos = 0;
139 return stream;
142 static int rar_close(rar_stream_t *stream)
144 if (stream->file)
145 return fclose(stream->file);
146 free(stream->data);
147 return 0;
150 static int rar_eof(rar_stream_t *stream)
152 if (stream->file)
153 return feof(stream->file);
154 return stream->pos >= stream->size;
157 static long rar_tell(rar_stream_t *stream)
159 if (stream->file)
160 return ftell(stream->file);
161 return stream->pos;
164 static int rar_seek(rar_stream_t *stream, long offset, int whence)
166 if (stream->file)
167 return fseek(stream->file, offset, whence);
168 switch (whence) {
169 case SEEK_SET:
170 if (offset < 0) {
171 errno = EINVAL;
172 return -1;
174 stream->pos = offset;
175 break;
176 case SEEK_CUR:
177 if (offset < 0 && stream->pos < (unsigned long) -offset) {
178 errno = EINVAL;
179 return -1;
181 stream->pos += offset;
182 break;
183 case SEEK_END:
184 if (offset < 0 && stream->size < (unsigned long) -offset) {
185 errno = EINVAL;
186 return -1;
188 stream->pos = stream->size + offset;
189 break;
190 default:
191 errno = EINVAL;
192 return -1;
194 return 0;
197 static int rar_getc(rar_stream_t *stream)
199 if (stream->file)
200 return getc(stream->file);
201 if (rar_eof(stream))
202 return EOF;
203 return stream->data[stream->pos++];
206 static size_t rar_read(void *ptr, size_t size, size_t nmemb,
207 rar_stream_t *stream)
209 size_t res;
210 unsigned long remain;
211 if (stream->file)
212 return fread(ptr, size, nmemb, stream->file);
213 if (rar_eof(stream))
214 return 0;
215 res = size * nmemb;
216 remain = stream->size - stream->pos;
217 if (res > remain)
218 res = remain / size * size;
219 memcpy(ptr, stream->data + stream->pos, res);
220 stream->pos += res;
221 res /= size;
222 return res;
225 #else
226 typedef FILE rar_stream_t;
227 #define rar_open fopen
228 #define rar_close fclose
229 #define rar_eof feof
230 #define rar_tell ftell
231 #define rar_seek fseek
232 #define rar_getc getc
233 #define rar_read fread
234 #endif
236 /**********************************************************************/
238 static ssize_t vobsub_getline(char **lineptr, size_t *n, rar_stream_t *stream)
240 size_t res = 0;
241 int c;
242 if (*lineptr == NULL) {
243 *lineptr = malloc(4096);
244 if (*lineptr)
245 *n = 4096;
246 } else if (*n == 0) {
247 char *tmp = realloc(*lineptr, 4096);
248 if (tmp) {
249 *lineptr = tmp;
250 *n = 4096;
253 if (*lineptr == NULL || *n == 0)
254 return -1;
256 for (c = rar_getc(stream); c != EOF; c = rar_getc(stream)) {
257 if (res + 1 >= *n) {
258 char *tmp = realloc(*lineptr, *n * 2);
259 if (tmp == NULL)
260 return -1;
261 *lineptr = tmp;
262 *n *= 2;
264 (*lineptr)[res++] = c;
265 if (c == '\n') {
266 (*lineptr)[res] = 0;
267 return res;
270 if (res == 0)
271 return -1;
272 (*lineptr)[res] = 0;
273 return res;
276 /**********************************************************************
277 * MPEG parsing
278 **********************************************************************/
280 typedef struct {
281 rar_stream_t *stream;
282 unsigned int pts;
283 int aid;
284 unsigned char *packet;
285 unsigned int packet_reserve;
286 unsigned int packet_size;
287 int padding_was_here;
288 int merge;
289 } mpeg_t;
291 static mpeg_t *mpeg_open(const char *filename)
293 mpeg_t *res = malloc(sizeof(mpeg_t));
294 int err = res == NULL;
295 if (!err) {
296 res->pts = 0;
297 res->aid = -1;
298 res->packet = NULL;
299 res->packet_size = 0;
300 res->packet_reserve = 0;
301 res->padding_was_here = 1;
302 res->merge = 0;
303 res->stream = rar_open(filename, "rb");
304 err = res->stream == NULL;
305 if (err)
306 perror("fopen Vobsub file failed");
307 if (err)
308 free(res);
310 return err ? NULL : res;
313 static void mpeg_free(mpeg_t *mpeg)
315 free(mpeg->packet);
316 if (mpeg->stream)
317 rar_close(mpeg->stream);
318 free(mpeg);
321 static int mpeg_eof(mpeg_t *mpeg)
323 return rar_eof(mpeg->stream);
326 static off_t mpeg_tell(mpeg_t *mpeg)
328 return rar_tell(mpeg->stream);
331 static int mpeg_run(mpeg_t *mpeg)
333 unsigned int len, idx, version;
334 int c;
335 /* Goto start of a packet, it starts with 0x000001?? */
336 const unsigned char wanted[] = { 0, 0, 1 };
337 unsigned char buf[5];
339 mpeg->aid = -1;
340 mpeg->packet_size = 0;
341 if (rar_read(buf, 4, 1, mpeg->stream) != 1)
342 return -1;
343 while (memcmp(buf, wanted, sizeof(wanted)) != 0) {
344 c = rar_getc(mpeg->stream);
345 if (c < 0)
346 return -1;
347 memmove(buf, buf + 1, 3);
348 buf[3] = c;
350 switch (buf[3]) {
351 case 0xb9: /* System End Code */
352 break;
353 case 0xba: /* Packet start code */
354 c = rar_getc(mpeg->stream);
355 if (c < 0)
356 return -1;
357 if ((c & 0xc0) == 0x40)
358 version = 4;
359 else if ((c & 0xf0) == 0x20)
360 version = 2;
361 else {
362 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Unsupported MPEG version: 0x%02x\n", c);
363 return -1;
365 if (version == 4) {
366 if (rar_seek(mpeg->stream, 9, SEEK_CUR))
367 return -1;
368 } else if (version == 2) {
369 if (rar_seek(mpeg->stream, 7, SEEK_CUR))
370 return -1;
371 } else
372 abort();
373 if (!mpeg->padding_was_here)
374 mpeg->merge = 1;
375 break;
376 case 0xbd: /* packet */
377 if (rar_read(buf, 2, 1, mpeg->stream) != 1)
378 return -1;
379 mpeg->padding_was_here = 0;
380 len = buf[0] << 8 | buf[1];
381 idx = mpeg_tell(mpeg);
382 c = rar_getc(mpeg->stream);
383 if (c < 0)
384 return -1;
385 if ((c & 0xC0) == 0x40) { /* skip STD scale & size */
386 if (rar_getc(mpeg->stream) < 0)
387 return -1;
388 c = rar_getc(mpeg->stream);
389 if (c < 0)
390 return -1;
392 if ((c & 0xf0) == 0x20) { /* System-1 stream timestamp */
393 /* Do we need this? */
394 abort();
395 } else if ((c & 0xf0) == 0x30) {
396 /* Do we need this? */
397 abort();
398 } else if ((c & 0xc0) == 0x80) { /* System-2 (.VOB) stream */
399 unsigned int pts_flags, hdrlen, dataidx;
400 c = rar_getc(mpeg->stream);
401 if (c < 0)
402 return -1;
403 pts_flags = c;
404 c = rar_getc(mpeg->stream);
405 if (c < 0)
406 return -1;
407 hdrlen = c;
408 dataidx = mpeg_tell(mpeg) + hdrlen;
409 if (dataidx > idx + len) {
410 mp_msg(MSGT_VOBSUB, MSGL_ERR, "Invalid header length: %d (total length: %d, idx: %d, dataidx: %d)\n",
411 hdrlen, len, idx, dataidx);
412 return -1;
414 if ((pts_flags & 0xc0) == 0x80) {
415 if (rar_read(buf, 5, 1, mpeg->stream) != 1)
416 return -1;
417 if (!(((buf[0] & 0xf0) == 0x20) && (buf[0] & 1) && (buf[2] & 1) && (buf[4] & 1))) {
418 mp_msg(MSGT_VOBSUB, MSGL_ERR, "vobsub PTS error: 0x%02x %02x%02x %02x%02x \n",
419 buf[0], buf[1], buf[2], buf[3], buf[4]);
420 mpeg->pts = 0;
421 } else
422 mpeg->pts = ((buf[0] & 0x0e) << 29 | buf[1] << 22 | (buf[2] & 0xfe) << 14
423 | buf[3] << 7 | (buf[4] >> 1));
424 } else /* if ((pts_flags & 0xc0) == 0xc0) */ {
425 /* what's this? */
426 /* abort(); */
428 rar_seek(mpeg->stream, dataidx, SEEK_SET);
429 mpeg->aid = rar_getc(mpeg->stream);
430 if (mpeg->aid < 0) {
431 mp_msg(MSGT_VOBSUB, MSGL_ERR, "Bogus aid %d\n", mpeg->aid);
432 return -1;
434 mpeg->packet_size = len - ((unsigned int) mpeg_tell(mpeg) - idx);
435 if (mpeg->packet_reserve < mpeg->packet_size) {
436 free(mpeg->packet);
437 mpeg->packet = malloc(mpeg->packet_size);
438 if (mpeg->packet)
439 mpeg->packet_reserve = mpeg->packet_size;
441 if (mpeg->packet == NULL) {
442 mp_msg(MSGT_VOBSUB, MSGL_FATAL, "malloc failure");
443 mpeg->packet_reserve = 0;
444 mpeg->packet_size = 0;
445 return -1;
447 if (rar_read(mpeg->packet, mpeg->packet_size, 1, mpeg->stream) != 1) {
448 mp_msg(MSGT_VOBSUB, MSGL_ERR, "fread failure");
449 mpeg->packet_size = 0;
450 return -1;
452 idx = len;
454 break;
455 case 0xbe: /* Padding */
456 if (rar_read(buf, 2, 1, mpeg->stream) != 1)
457 return -1;
458 len = buf[0] << 8 | buf[1];
459 if (len > 0 && rar_seek(mpeg->stream, len, SEEK_CUR))
460 return -1;
461 mpeg->padding_was_here = 1;
462 break;
463 default:
464 if (0xc0 <= buf[3] && buf[3] < 0xf0) {
465 /* MPEG audio or video */
466 if (rar_read(buf, 2, 1, mpeg->stream) != 1)
467 return -1;
468 len = buf[0] << 8 | buf[1];
469 if (len > 0 && rar_seek(mpeg->stream, len, SEEK_CUR))
470 return -1;
471 } else {
472 mp_msg(MSGT_VOBSUB, MSGL_ERR, "unknown header 0x%02X%02X%02X%02X\n",
473 buf[0], buf[1], buf[2], buf[3]);
474 return -1;
477 return 0;
480 /**********************************************************************
481 * Packet queue
482 **********************************************************************/
484 typedef struct {
485 unsigned int pts100;
486 off_t filepos;
487 unsigned int size;
488 unsigned char *data;
489 } packet_t;
491 typedef struct {
492 char *id;
493 packet_t *packets;
494 unsigned int packets_reserve;
495 unsigned int packets_size;
496 unsigned int current_index;
497 } packet_queue_t;
499 static void packet_construct(packet_t *pkt)
501 pkt->pts100 = 0;
502 pkt->filepos = 0;
503 pkt->size = 0;
504 pkt->data = NULL;
507 static void packet_destroy(packet_t *pkt)
509 free(pkt->data);
512 static void packet_queue_construct(packet_queue_t *queue)
514 queue->id = NULL;
515 queue->packets = NULL;
516 queue->packets_reserve = 0;
517 queue->packets_size = 0;
518 queue->current_index = 0;
521 static void packet_queue_destroy(packet_queue_t *queue)
523 if (queue->packets) {
524 while (queue->packets_size--)
525 packet_destroy(queue->packets + queue->packets_size);
526 free(queue->packets);
528 return;
531 /* Make sure there is enough room for needed_size packets in the
532 packet queue. */
533 static int packet_queue_ensure(packet_queue_t *queue, unsigned int needed_size)
535 if (queue->packets_reserve < needed_size) {
536 if (queue->packets) {
537 packet_t *tmp = realloc(queue->packets, 2 * queue->packets_reserve * sizeof(packet_t));
538 if (tmp == NULL) {
539 mp_msg(MSGT_VOBSUB, MSGL_FATAL, "realloc failure");
540 return -1;
542 queue->packets = tmp;
543 queue->packets_reserve *= 2;
544 } else {
545 queue->packets = malloc(sizeof(packet_t));
546 if (queue->packets == NULL) {
547 mp_msg(MSGT_VOBSUB, MSGL_FATAL, "malloc failure");
548 return -1;
550 queue->packets_reserve = 1;
553 return 0;
556 /* add one more packet */
557 static int packet_queue_grow(packet_queue_t *queue)
559 if (packet_queue_ensure(queue, queue->packets_size + 1) < 0)
560 return -1;
561 packet_construct(queue->packets + queue->packets_size);
562 ++queue->packets_size;
563 return 0;
566 /* insert a new packet, duplicating pts from the current one */
567 static int packet_queue_insert(packet_queue_t *queue)
569 packet_t *pkts;
570 if (packet_queue_ensure(queue, queue->packets_size + 1) < 0)
571 return -1;
572 /* XXX packet_size does not reflect the real thing here, it will be updated a bit later */
573 memmove(queue->packets + queue->current_index + 2,
574 queue->packets + queue->current_index + 1,
575 sizeof(packet_t) * (queue->packets_size - queue->current_index - 1));
576 pkts = queue->packets + queue->current_index;
577 ++queue->packets_size;
578 ++queue->current_index;
579 packet_construct(pkts + 1);
580 pkts[1].pts100 = pkts[0].pts100;
581 pkts[1].filepos = pkts[0].filepos;
582 return 0;
585 /**********************************************************************
586 * Vobsub
587 **********************************************************************/
589 typedef struct {
590 unsigned int palette[16];
591 int delay;
592 unsigned int have_palette;
593 unsigned int orig_frame_width, orig_frame_height;
594 unsigned int origin_x, origin_y;
595 /* index */
596 packet_queue_t *spu_streams;
597 unsigned int spu_streams_size;
598 unsigned int spu_streams_current;
599 unsigned int spu_valid_streams_size;
600 } vobsub_t;
602 /* Make sure that the spu stream idx exists. */
603 static int vobsub_ensure_spu_stream(vobsub_t *vob, unsigned int index)
605 if (index >= vob->spu_streams_size) {
606 /* This is a new stream */
607 if (vob->spu_streams) {
608 packet_queue_t *tmp = realloc(vob->spu_streams, (index + 1) * sizeof(packet_queue_t));
609 if (tmp == NULL) {
610 mp_msg(MSGT_VOBSUB, MSGL_ERR, "vobsub_ensure_spu_stream: realloc failure");
611 return -1;
613 vob->spu_streams = tmp;
614 } else {
615 vob->spu_streams = malloc((index + 1) * sizeof(packet_queue_t));
616 if (vob->spu_streams == NULL) {
617 mp_msg(MSGT_VOBSUB, MSGL_ERR, "vobsub_ensure_spu_stream: malloc failure");
618 return -1;
621 while (vob->spu_streams_size <= index) {
622 packet_queue_construct(vob->spu_streams + vob->spu_streams_size);
623 ++vob->spu_streams_size;
626 return 0;
629 static int vobsub_add_id(vobsub_t *vob, const char *id, size_t idlen,
630 const unsigned int index)
632 if (vobsub_ensure_spu_stream(vob, index) < 0)
633 return -1;
634 if (id && idlen) {
635 free(vob->spu_streams[index].id);
636 vob->spu_streams[index].id = malloc(idlen + 1);
637 if (vob->spu_streams[index].id == NULL) {
638 mp_msg(MSGT_VOBSUB, MSGL_FATAL, "vobsub_add_id: malloc failure");
639 return -1;
641 vob->spu_streams[index].id[idlen] = 0;
642 memcpy(vob->spu_streams[index].id, id, idlen);
644 vob->spu_streams_current = index;
645 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VOBSUB_ID=%d\n", index);
646 if (id && idlen)
647 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VSID_%d_LANG=%s\n", index, vob->spu_streams[index].id);
648 mp_msg(MSGT_VOBSUB, MSGL_V, "[vobsub] subtitle (vobsubid): %d language %s\n",
649 index, vob->spu_streams[index].id);
650 return 0;
653 static int vobsub_add_timestamp(vobsub_t *vob, off_t filepos, int ms)
655 packet_queue_t *queue;
656 packet_t *pkt;
657 if (vob->spu_streams == 0) {
658 mp_msg(MSGT_VOBSUB, MSGL_WARN, "[vobsub] warning, binning some index entries. Check your index file\n");
659 return -1;
661 queue = vob->spu_streams + vob->spu_streams_current;
662 if (packet_queue_grow(queue) >= 0) {
663 pkt = queue->packets + (queue->packets_size - 1);
664 pkt->filepos = filepos;
665 pkt->pts100 = ms < 0 ? UINT_MAX : (unsigned int)ms * 90;
666 return 0;
668 return -1;
671 static int vobsub_parse_id(vobsub_t *vob, const char *line)
673 // id: xx, index: n
674 size_t idlen;
675 const char *p, *q;
676 p = line;
677 while (isspace(*p))
678 ++p;
679 q = p;
680 while (isalpha(*q))
681 ++q;
682 idlen = q - p;
683 if (idlen == 0)
684 return -1;
685 ++q;
686 while (isspace(*q))
687 ++q;
688 if (strncmp("index:", q, 6))
689 return -1;
690 q += 6;
691 while (isspace(*q))
692 ++q;
693 if (!isdigit(*q))
694 return -1;
695 return vobsub_add_id(vob, p, idlen, atoi(q));
698 static int vobsub_parse_timestamp(vobsub_t *vob, const char *line)
700 // timestamp: HH:MM:SS.mmm, filepos: 0nnnnnnnnn
701 const char *p;
702 int h, m, s, ms;
703 off_t filepos;
704 while (isspace(*line))
705 ++line;
706 p = line;
707 while (isdigit(*p))
708 ++p;
709 if (p - line != 2)
710 return -1;
711 h = atoi(line);
712 if (*p != ':')
713 return -1;
714 line = ++p;
715 while (isdigit(*p))
716 ++p;
717 if (p - line != 2)
718 return -1;
719 m = atoi(line);
720 if (*p != ':')
721 return -1;
722 line = ++p;
723 while (isdigit(*p))
724 ++p;
725 if (p - line != 2)
726 return -1;
727 s = atoi(line);
728 if (*p != ':')
729 return -1;
730 line = ++p;
731 while (isdigit(*p))
732 ++p;
733 if (p - line != 3)
734 return -1;
735 ms = atoi(line);
736 if (*p != ',')
737 return -1;
738 line = p + 1;
739 while (isspace(*line))
740 ++line;
741 if (strncmp("filepos:", line, 8))
742 return -1;
743 line += 8;
744 while (isspace(*line))
745 ++line;
746 if (! isxdigit(*line))
747 return -1;
748 filepos = strtol(line, NULL, 16);
749 return vobsub_add_timestamp(vob, filepos, vob->delay + ms + 1000 * (s + 60 * (m + 60 * h)));
752 static int vobsub_parse_origin(vobsub_t *vob, const char *line)
754 // org: X,Y
755 char *p;
756 while (isspace(*line))
757 ++line;
758 if (!isdigit(*line))
759 return -1;
760 vob->origin_x = strtoul(line, &p, 10);
761 if (*p != ',')
762 return -1;
763 ++p;
764 vob->origin_y = strtoul(p, NULL, 10);
765 return 0;
768 unsigned int vobsub_palette_to_yuv(unsigned int pal)
770 int r, g, b, y, u, v;
771 // Palette in idx file is not rgb value, it was calculated by wrong formula.
772 // Here's reversed formula of the one used to generate palette in idx file.
773 r = pal >> 16 & 0xff;
774 g = pal >> 8 & 0xff;
775 b = pal & 0xff;
776 y = av_clip_uint8( 0.1494 * r + 0.6061 * g + 0.2445 * b);
777 u = av_clip_uint8( 0.6066 * r - 0.4322 * g - 0.1744 * b + 128);
778 v = av_clip_uint8(-0.08435 * r - 0.3422 * g + 0.4266 * b + 128);
779 y = y * 219 / 255 + 16;
780 return y << 16 | u << 8 | v;
783 unsigned int vobsub_rgb_to_yuv(unsigned int rgb)
785 int r, g, b, y, u, v;
786 r = rgb >> 16 & 0xff;
787 g = rgb >> 8 & 0xff;
788 b = rgb & 0xff;
789 y = ( 0.299 * r + 0.587 * g + 0.114 * b) * 219 / 255 + 16.5;
790 u = (-0.16874 * r - 0.33126 * g + 0.5 * b) * 224 / 255 + 128.5;
791 v = ( 0.5 * r - 0.41869 * g - 0.08131 * b) * 224 / 255 + 128.5;
792 return y << 16 | u << 8 | v;
795 static int vobsub_parse_delay(vobsub_t *vob, const char *line)
797 int h, m, s, ms;
798 int forward = 1;
799 if (*(line + 7) == '+') {
800 forward = 1;
801 line++;
802 } else if (*(line + 7) == '-') {
803 forward = -1;
804 line++;
806 mp_msg(MSGT_SPUDEC, MSGL_V, "forward=%d", forward);
807 h = atoi(line + 7);
808 mp_msg(MSGT_VOBSUB, MSGL_V, "h=%d,", h);
809 m = atoi(line + 10);
810 mp_msg(MSGT_VOBSUB, MSGL_V, "m=%d,", m);
811 s = atoi(line + 13);
812 mp_msg(MSGT_VOBSUB, MSGL_V, "s=%d,", s);
813 ms = atoi(line + 16);
814 mp_msg(MSGT_VOBSUB, MSGL_V, "ms=%d", ms);
815 vob->delay = (ms + 1000 * (s + 60 * (m + 60 * h))) * forward;
816 return 0;
819 static int vobsub_set_lang(const char *line)
821 if (vobsub_id == -1)
822 vobsub_id = atoi(line + 8);
823 return 0;
826 static int vobsub_parse_one_line(vobsub_t *vob, rar_stream_t *fd,
827 unsigned char **extradata,
828 unsigned int *extradata_len)
830 ssize_t line_size;
831 int res = -1;
832 size_t line_reserve = 0;
833 char *line = NULL;
834 do {
835 line_size = vobsub_getline(&line, &line_reserve, fd);
836 if (line_size < 0 || line_size > 1000000 ||
837 *extradata_len+line_size > 10000000) {
838 break;
841 *extradata = realloc(*extradata, *extradata_len+line_size+1);
842 memcpy(*extradata+*extradata_len, line, line_size);
843 *extradata_len += line_size;
844 (*extradata)[*extradata_len] = 0;
846 if (*line == 0 || *line == '\r' || *line == '\n' || *line == '#')
847 continue;
848 else if (strncmp("langidx:", line, 8) == 0)
849 res = vobsub_set_lang(line);
850 else if (strncmp("delay:", line, 6) == 0)
851 res = vobsub_parse_delay(vob, line);
852 else if (strncmp("id:", line, 3) == 0)
853 res = vobsub_parse_id(vob, line + 3);
854 else if (strncmp("org:", line, 4) == 0)
855 res = vobsub_parse_origin(vob, line + 4);
856 else if (strncmp("timestamp:", line, 10) == 0)
857 res = vobsub_parse_timestamp(vob, line + 10);
858 else {
859 mp_msg(MSGT_VOBSUB, MSGL_V, "vobsub: ignoring %s", line);
860 continue;
862 if (res < 0)
863 mp_msg(MSGT_VOBSUB, MSGL_ERR, "ERROR in %s", line);
864 break;
865 } while (1);
866 free(line);
867 return res;
870 int vobsub_parse_ifo(void* this, const char *const name, unsigned int *palette,
871 unsigned int *width, unsigned int *height, int force,
872 int sid, char *langid)
874 vobsub_t *vob = this;
875 int res = -1;
876 rar_stream_t *fd = rar_open(name, "rb");
877 if (fd == NULL) {
878 if (force)
879 mp_msg(MSGT_VOBSUB, MSGL_WARN, "VobSub: Can't open IFO file\n");
880 } else {
881 // parse IFO header
882 unsigned char block[0x800];
883 const char *const ifo_magic = "DVDVIDEO-VTS";
884 if (rar_read(block, sizeof(block), 1, fd) != 1) {
885 if (force)
886 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Can't read IFO header\n");
887 } else if (memcmp(block, ifo_magic, strlen(ifo_magic) + 1))
888 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Bad magic in IFO header\n");
889 else {
890 unsigned long pgci_sector = block[0xcc] << 24 | block[0xcd] << 16
891 | block[0xce] << 8 | block[0xcf];
892 int standard = (block[0x200] & 0x30) >> 4;
893 int resolution = (block[0x201] & 0x0c) >> 2;
894 *height = standard ? 576 : 480;
895 *width = 0;
896 switch (resolution) {
897 case 0x0:
898 *width = 720;
899 break;
900 case 0x1:
901 *width = 704;
902 break;
903 case 0x2:
904 *width = 352;
905 break;
906 case 0x3:
907 *width = 352;
908 *height /= 2;
909 break;
910 default:
911 mp_msg(MSGT_VOBSUB, MSGL_WARN, "Vobsub: Unknown resolution %d \n", resolution);
913 if (langid && 0 <= sid && sid < 32) {
914 unsigned char *tmp = block + 0x256 + sid * 6 + 2;
915 langid[0] = tmp[0];
916 langid[1] = tmp[1];
917 langid[2] = 0;
919 if (rar_seek(fd, pgci_sector * sizeof(block), SEEK_SET)
920 || rar_read(block, sizeof(block), 1, fd) != 1)
921 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Can't read IFO PGCI\n");
922 else {
923 unsigned long idx;
924 unsigned long pgc_offset = block[0xc] << 24 | block[0xd] << 16
925 | block[0xe] << 8 | block[0xf];
926 for (idx = 0; idx < 16; ++idx) {
927 unsigned char *p = block + pgc_offset + 0xa4 + 4 * idx;
928 palette[idx] = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
930 if (vob)
931 vob->have_palette = 1;
932 res = 0;
935 rar_close(fd);
937 return res;
940 void *vobsub_open(const char *const name, const char *const ifo,
941 const int force, void** spu)
943 unsigned char *extradata = NULL;
944 unsigned int extradata_len = 0;
945 vobsub_t *vob = calloc(1, sizeof(vobsub_t));
946 if (spu)
947 *spu = NULL;
948 if (vobsubid == -2)
949 vobsubid = vobsub_id;
950 if (vob) {
951 char *buf;
952 buf = malloc(strlen(name) + 5);
953 if (buf) {
954 rar_stream_t *fd;
955 mpeg_t *mpg;
956 /* read in the info file */
957 if (!ifo) {
958 strcpy(buf, name);
959 strcat(buf, ".ifo");
960 vobsub_parse_ifo(vob, buf, vob->palette, &vob->orig_frame_width, &vob->orig_frame_height, force, -1, NULL);
961 } else
962 vobsub_parse_ifo(vob, ifo, vob->palette, &vob->orig_frame_width, &vob->orig_frame_height, force, -1, NULL);
963 /* read in the index */
964 strcpy(buf, name);
965 strcat(buf, ".idx");
966 fd = rar_open(buf, "rb");
967 if (fd == NULL) {
968 if (force)
969 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Can't open IDX file\n");
970 else {
971 free(buf);
972 free(vob);
973 return NULL;
975 } else {
976 while (vobsub_parse_one_line(vob, fd, &extradata, &extradata_len) >= 0)
977 /* NOOP */ ;
978 rar_close(fd);
980 if (spu)
981 *spu = spudec_new_scaled(vob->palette, vob->orig_frame_width, vob->orig_frame_height, extradata, extradata_len);
982 free(extradata);
984 /* read the indexed mpeg_stream */
985 strcpy(buf, name);
986 strcat(buf, ".sub");
987 mpg = mpeg_open(buf);
988 if (mpg == NULL) {
989 if (force)
990 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: Can't open SUB file\n");
991 else {
992 free(buf);
993 free(vob);
994 return NULL;
996 } else {
997 long last_pts_diff = 0;
998 while (!mpeg_eof(mpg)) {
999 off_t pos = mpeg_tell(mpg);
1000 if (mpeg_run(mpg) < 0) {
1001 if (!mpeg_eof(mpg))
1002 mp_msg(MSGT_VOBSUB, MSGL_ERR, "VobSub: mpeg_run error\n");
1003 break;
1005 if (mpg->packet_size) {
1006 if ((mpg->aid & 0xe0) == 0x20) {
1007 unsigned int sid = mpg->aid & 0x1f;
1008 if (vobsub_ensure_spu_stream(vob, sid) >= 0) {
1009 packet_queue_t *queue = vob->spu_streams + sid;
1010 /* get the packet to fill */
1011 if (queue->packets_size == 0 && packet_queue_grow(queue) < 0)
1012 abort();
1013 while (queue->current_index + 1 < queue->packets_size
1014 && queue->packets[queue->current_index + 1].filepos <= pos)
1015 ++queue->current_index;
1016 if (queue->current_index < queue->packets_size) {
1017 packet_t *pkt;
1018 if (queue->packets[queue->current_index].data) {
1019 /* insert a new packet and fix the PTS ! */
1020 packet_queue_insert(queue);
1021 queue->packets[queue->current_index].pts100 =
1022 mpg->pts + last_pts_diff;
1024 pkt = queue->packets + queue->current_index;
1025 if (pkt->pts100 != UINT_MAX) {
1026 if (queue->packets_size > 1)
1027 last_pts_diff = pkt->pts100 - mpg->pts;
1028 else
1029 pkt->pts100 = mpg->pts;
1030 if (mpg->merge && queue->current_index > 0) {
1031 packet_t *last = &queue->packets[queue->current_index - 1];
1032 pkt->pts100 = last->pts100;
1034 mpg->merge = 0;
1035 /* FIXME: should not use mpg_sub internal informations, make a copy */
1036 pkt->data = mpg->packet;
1037 pkt->size = mpg->packet_size;
1038 mpg->packet = NULL;
1039 mpg->packet_reserve = 0;
1040 mpg->packet_size = 0;
1043 } else
1044 mp_msg(MSGT_VOBSUB, MSGL_WARN, "don't know what to do with subtitle #%u\n", sid);
1048 vob->spu_streams_current = vob->spu_streams_size;
1049 while (vob->spu_streams_current-- > 0) {
1050 vob->spu_streams[vob->spu_streams_current].current_index = 0;
1051 if (vobsubid == vob->spu_streams_current ||
1052 vob->spu_streams[vob->spu_streams_current].packets_size > 0)
1053 ++vob->spu_valid_streams_size;
1055 mpeg_free(mpg);
1057 free(buf);
1060 return vob;
1063 void vobsub_close(void *this)
1065 vobsub_t *vob = this;
1066 if (vob->spu_streams) {
1067 while (vob->spu_streams_size--)
1068 packet_queue_destroy(vob->spu_streams + vob->spu_streams_size);
1069 free(vob->spu_streams);
1071 free(vob);
1074 unsigned int vobsub_get_indexes_count(void *vobhandle)
1076 vobsub_t *vob = vobhandle;
1077 return vob->spu_valid_streams_size;
1080 char *vobsub_get_id(void *vobhandle, unsigned int index)
1082 vobsub_t *vob = vobhandle;
1083 return (index < vob->spu_streams_size) ? vob->spu_streams[index].id : NULL;
1086 int vobsub_get_id_by_index(void *vobhandle, unsigned int index)
1088 vobsub_t *vob = vobhandle;
1089 int i, j;
1090 if (vob == NULL)
1091 return -1;
1092 for (i = 0, j = 0; i < vob->spu_streams_size; ++i)
1093 if (i == vobsubid || vob->spu_streams[i].packets_size > 0) {
1094 if (j == index)
1095 return i;
1096 ++j;
1098 return -1;
1101 int vobsub_get_index_by_id(void *vobhandle, int id)
1103 vobsub_t *vob = vobhandle;
1104 int i, j;
1105 if (vob == NULL || id < 0 || id >= vob->spu_streams_size)
1106 return -1;
1107 if (id != vobsubid && !vob->spu_streams[id].packets_size)
1108 return -1;
1109 for (i = 0, j = 0; i < id; ++i)
1110 if (i == vobsubid || vob->spu_streams[i].packets_size > 0)
1111 ++j;
1112 return j;
1115 int vobsub_set_from_lang(void *vobhandle, unsigned char * lang)
1117 int i;
1118 vobsub_t *vob= vobhandle;
1119 while (lang && strlen(lang) >= 2) {
1120 for (i = 0; i < vob->spu_streams_size; i++)
1121 if (vob->spu_streams[i].id)
1122 if ((strncmp(vob->spu_streams[i].id, lang, 2) == 0)) {
1123 vobsub_id = i;
1124 mp_msg(MSGT_VOBSUB, MSGL_INFO, "Selected VOBSUB language: %d language: %s\n", i, vob->spu_streams[i].id);
1125 return 0;
1127 lang+=2;while (lang[0]==',' || lang[0]==' ') ++lang;
1129 mp_msg(MSGT_VOBSUB, MSGL_WARN, "No matching VOBSUB language found!\n");
1130 return -1;
1133 /// make sure we seek to the first packet of packets having same pts values.
1134 static void vobsub_queue_reseek(packet_queue_t *queue, unsigned int pts100)
1136 int reseek_count = 0;
1137 unsigned int lastpts = 0;
1139 if (queue->current_index > 0
1140 && (queue->packets[queue->current_index].pts100 == UINT_MAX
1141 || queue->packets[queue->current_index].pts100 > pts100)) {
1142 // possible pts seek previous, try to check it.
1143 int i = 1;
1144 while (queue->current_index >= i
1145 && queue->packets[queue->current_index-i].pts100 == UINT_MAX)
1146 ++i;
1147 if (queue->current_index >= i
1148 && queue->packets[queue->current_index-i].pts100 > pts100)
1149 // pts seek previous confirmed, reseek from beginning
1150 queue->current_index = 0;
1152 while (queue->current_index < queue->packets_size
1153 && queue->packets[queue->current_index].pts100 <= pts100) {
1154 lastpts = queue->packets[queue->current_index].pts100;
1155 ++queue->current_index;
1156 ++reseek_count;
1158 while (reseek_count-- && --queue->current_index) {
1159 if (queue->packets[queue->current_index-1].pts100 != UINT_MAX &&
1160 queue->packets[queue->current_index-1].pts100 != lastpts)
1161 break;
1165 int vobsub_get_packet(void *vobhandle, float pts, void** data, int* timestamp)
1167 vobsub_t *vob = vobhandle;
1168 unsigned int pts100 = 90000 * pts;
1169 if (vob->spu_streams && 0 <= vobsub_id && (unsigned) vobsub_id < vob->spu_streams_size) {
1170 packet_queue_t *queue = vob->spu_streams + vobsub_id;
1172 vobsub_queue_reseek(queue, pts100);
1174 while (queue->current_index < queue->packets_size) {
1175 packet_t *pkt = queue->packets + queue->current_index;
1176 if (pkt->pts100 != UINT_MAX)
1177 if (pkt->pts100 <= pts100) {
1178 ++queue->current_index;
1179 *data = pkt->data;
1180 *timestamp = pkt->pts100;
1181 return pkt->size;
1182 } else
1183 break;
1184 else
1185 ++queue->current_index;
1188 return -1;
1191 int vobsub_get_next_packet(void *vobhandle, void** data, int* timestamp)
1193 vobsub_t *vob = vobhandle;
1194 if (vob->spu_streams && 0 <= vobsub_id && (unsigned) vobsub_id < vob->spu_streams_size) {
1195 packet_queue_t *queue = vob->spu_streams + vobsub_id;
1196 if (queue->current_index < queue->packets_size) {
1197 packet_t *pkt = queue->packets + queue->current_index;
1198 ++queue->current_index;
1199 *data = pkt->data;
1200 *timestamp = pkt->pts100;
1201 return pkt->size;
1204 return -1;
1207 void vobsub_seek(void * vobhandle, float pts)
1209 vobsub_t * vob = vobhandle;
1210 packet_queue_t * queue;
1211 int seek_pts100 = pts * 90000;
1213 if (vob->spu_streams && 0 <= vobsub_id && (unsigned) vobsub_id < vob->spu_streams_size) {
1214 /* do not seek if we don't know the id */
1215 if (vobsub_get_id(vob, vobsub_id) == NULL)
1216 return;
1217 queue = vob->spu_streams + vobsub_id;
1218 queue->current_index = 0;
1219 vobsub_queue_reseek(queue, seek_pts100);
1223 void vobsub_reset(void *vobhandle)
1225 vobsub_t *vob = vobhandle;
1226 if (vob->spu_streams) {
1227 unsigned int n = vob->spu_streams_size;
1228 while (n-- > 0)
1229 vob->spu_streams[n].current_index = 0;
1233 /**********************************************************************
1234 * Vobsub output
1235 **********************************************************************/
1237 typedef struct {
1238 FILE *fsub;
1239 FILE *fidx;
1240 unsigned int aid;
1241 } vobsub_out_t;
1243 static void create_idx(vobsub_out_t *me, const unsigned int *palette,
1244 unsigned int orig_width, unsigned int orig_height)
1246 int i;
1247 fprintf(me->fidx,
1248 "# VobSub index file, v7 (do not modify this line!)\n"
1249 "#\n"
1250 "# Generated by %s\n"
1251 "# See <URL:http://www.mplayerhq.hu/> for more information about MPlayer\n"
1252 "# See <URL:http://wiki.multimedia.cx/index.php?title=VOBsub> for more information about Vobsub\n"
1253 "#\n"
1254 "size: %ux%u\n",
1255 mplayer_version, orig_width, orig_height);
1256 if (palette) {
1257 fputs("palette:", me->fidx);
1258 for (i = 0; i < 16; ++i) {
1259 const double y = palette[i] >> 16 & 0xff,
1260 u = (palette[i] >> 8 & 0xff) - 128.0,
1261 v = (palette[i] & 0xff) - 128.0;
1262 if (i)
1263 putc(',', me->fidx);
1264 fprintf(me->fidx, " %02x%02x%02x",
1265 av_clip_uint8(y + 1.4022 * u),
1266 av_clip_uint8(y - 0.3456 * u - 0.7145 * v),
1267 av_clip_uint8(y + 1.7710 * v));
1269 putc('\n', me->fidx);
1272 fprintf(me->fidx, "# ON: displays only forced subtitles, OFF: shows everything\n"
1273 "forced subs: OFF\n");
1276 void *vobsub_out_open(const char *basename, const unsigned int *palette,
1277 unsigned int orig_width, unsigned int orig_height,
1278 const char *id, unsigned int index)
1280 vobsub_out_t *result = NULL;
1281 char *filename;
1282 filename = malloc(strlen(basename) + 5);
1283 if (filename) {
1284 result = malloc(sizeof(vobsub_out_t));
1285 if (result) {
1286 result->aid = index;
1287 strcpy(filename, basename);
1288 strcat(filename, ".sub");
1289 result->fsub = fopen(filename, "ab");
1290 if (result->fsub == NULL)
1291 perror("Error: vobsub_out_open subtitle file open failed");
1292 strcpy(filename, basename);
1293 strcat(filename, ".idx");
1294 result->fidx = fopen(filename, "ab");
1295 if (result->fidx) {
1296 if (ftell(result->fidx) == 0) {
1297 create_idx(result, palette, orig_width, orig_height);
1298 /* Make the selected language the default language */
1299 fprintf(result->fidx, "\n# Language index in use\nlangidx: %u\n", index);
1301 fprintf(result->fidx, "\nid: %s, index: %u\n", id ? id : "xx", index);
1302 /* So that we can check the file now */
1303 fflush(result->fidx);
1304 } else
1305 perror("Error: vobsub_out_open index file open failed");
1306 free(filename);
1309 return result;
1312 void vobsub_out_close(void *me)
1314 vobsub_out_t *vob = me;
1315 if (vob->fidx)
1316 fclose(vob->fidx);
1317 if (vob->fsub)
1318 fclose(vob->fsub);
1319 free(vob);
1322 void vobsub_out_output(void *me, const unsigned char *packet,
1323 int len, double pts)
1325 static double last_pts;
1326 static int last_pts_set = 0;
1327 vobsub_out_t *vob = me;
1328 if (vob->fsub) {
1329 /* Windows' Vobsub require that every packet is exactly 2kB long */
1330 unsigned char buffer[2048];
1331 unsigned char *p;
1332 int remain = 2048;
1333 /* Do not output twice a line with the same timestamp, this
1334 breaks Windows' Vobsub */
1335 if (vob->fidx && (!last_pts_set || last_pts != pts)) {
1336 static unsigned int last_h = 9999, last_m = 9999, last_s = 9999, last_ms = 9999;
1337 unsigned int h, m, ms;
1338 double s;
1339 s = pts;
1340 h = s / 3600;
1341 s -= h * 3600;
1342 m = s / 60;
1343 s -= m * 60;
1344 ms = (s - (unsigned int) s) * 1000;
1345 if (ms >= 1000) /* prevent overfolws or bad float stuff */
1346 ms = 0;
1347 if (h != last_h || m != last_m || (unsigned int) s != last_s || ms != last_ms) {
1348 fprintf(vob->fidx, "timestamp: %02u:%02u:%02u:%03u, filepos: %09lx\n",
1349 h, m, (unsigned int) s, ms, ftell(vob->fsub));
1350 last_h = h;
1351 last_m = m;
1352 last_s = (unsigned int) s;
1353 last_ms = ms;
1356 last_pts = pts;
1357 last_pts_set = 1;
1359 /* Packet start code: Windows' Vobsub needs this */
1360 p = buffer;
1361 *p++ = 0; /* 0x00 */
1362 *p++ = 0;
1363 *p++ = 1;
1364 *p++ = 0xba;
1365 *p++ = 0x40;
1366 memset(p, 0, 9);
1367 p += 9;
1368 { /* Packet */
1369 static unsigned char last_pts[5] = { 0, 0, 0, 0, 0};
1370 unsigned char now_pts[5];
1371 int pts_len, pad_len, datalen = len;
1372 pts *= 90000;
1373 now_pts[0] = 0x21 | (((unsigned long)pts >> 29) & 0x0e);
1374 now_pts[1] = ((unsigned long)pts >> 22) & 0xff;
1375 now_pts[2] = 0x01 | (((unsigned long)pts >> 14) & 0xfe);
1376 now_pts[3] = ((unsigned long)pts >> 7) & 0xff;
1377 now_pts[4] = 0x01 | (((unsigned long)pts << 1) & 0xfe);
1378 pts_len = memcmp(last_pts, now_pts, sizeof(now_pts)) ? sizeof(now_pts) : 0;
1379 memcpy(last_pts, now_pts, sizeof(now_pts));
1381 datalen += 3; /* Version, PTS_flags, pts_len */
1382 datalen += pts_len;
1383 datalen += 1; /* AID */
1384 pad_len = 2048 - (p - buffer) - 4 /* MPEG ID */ - 2 /* payload len */ - datalen;
1385 /* XXX - Go figure what should go here! In any case the
1386 packet has to be completly filled. If I can fill it
1387 with padding (0x000001be) latter I'll do that. But if
1388 there is only room for 6 bytes then I can not write a
1389 padding packet. So I add some padding in the PTS
1390 field. This looks like a dirty kludge. Oh well... */
1391 if (pad_len < 0) {
1392 /* Packet is too big. Let's try ommiting the PTS field */
1393 datalen -= pts_len;
1394 pts_len = 0;
1395 pad_len = 0;
1396 } else if (pad_len > 6)
1397 pad_len = 0;
1398 datalen += pad_len;
1400 *p++ = 0; /* 0x0e */
1401 *p++ = 0;
1402 *p++ = 1;
1403 *p++ = 0xbd;
1405 *p++ = (datalen >> 8) & 0xff; /* length of payload */
1406 *p++ = datalen & 0xff;
1407 *p++ = 0x80; /* System-2 (.VOB) stream */
1408 *p++ = pts_len ? 0x80 : 0x00; /* pts_flags */
1409 *p++ = pts_len + pad_len;
1410 memcpy(p, now_pts, pts_len);
1411 p += pts_len;
1412 memset(p, 0, pad_len);
1413 p += pad_len;
1415 *p++ = 0x20 | vob->aid; /* aid */
1416 if (fwrite(buffer, p - buffer, 1, vob->fsub) != 1
1417 || fwrite(packet, len, 1, vob->fsub) != 1)
1418 perror("ERROR: vobsub write failed");
1419 else
1420 remain -= p - buffer + len;
1422 /* Padding */
1423 if (remain >= 6) {
1424 p = buffer;
1425 *p++ = 0x00;
1426 *p++ = 0x00;
1427 *p++ = 0x01;
1428 *p++ = 0xbe;
1429 *p++ = (remain - 6) >> 8;
1430 *p++ = (remain - 6) & 0xff;
1431 /* for better compression, blank this */
1432 memset(buffer + 6, 0, remain - (p - buffer));
1433 if (fwrite(buffer, remain, 1, vob->fsub) != 1)
1434 perror("ERROR: vobsub padding write failed");
1435 } else if (remain > 0) {
1436 /* I don't know what to output. But anyway the block
1437 needs to be 2KB big */
1438 memset(buffer, 0, remain);
1439 if (fwrite(buffer, remain, 1, vob->fsub) != 1)
1440 perror("ERROR: vobsub blank padding write failed");
1441 } else if (remain < 0)
1442 fprintf(stderr,
1443 "\nERROR: wrong thing happenned...\n"
1444 " I wrote a %i data bytes spu packet and that's too long\n", len);