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