annotate vobsub.c @ 4597:4ba62f7d04c6

yuv4mpeg output, by Robert Kesterson <robertk@robertk.com>
author arpi
date Sat, 09 Feb 2002 01:25:21 +0000
parents aeb27b09de8e
children 71c3a45cf1fd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4080
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
1 /*
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
2 * Some code freely inspired from VobSub <URL:http://vobsub.edensrising.com>,
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
3 * with kind permission from Gabest <gabest@freemail.hu>
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
4 */
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
5 /* #define HAVE_GETLINE */
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
6 #include <ctype.h>
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
7 #include <errno.h>
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
8 #include <stdio.h>
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
9 #include <stdlib.h>
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
10 #include <string.h>
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
11 #include <fcntl.h>
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
12 #include <unistd.h>
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
13 #include <sys/stat.h>
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
14 #include <sys/types.h>
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
15
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
16 #include "config.h"
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
17
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
18 #include "stream.h"
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
19 #include "vobsub.h"
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
20 #include "spudec.h"
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
21 #include "mp_msg.h"
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
22
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
23 extern int vobsub_id;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
24
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
25 extern int verbose;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
26
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
27 #ifdef HAVE_GETLINE
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
28 extern ssize_t getline(char **, size_t *, FILE *);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
29 #else
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
30 /* FIXME This should go into a general purpose library or even a
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
31 separate file. */
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
32 static ssize_t
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
33 getline (char **lineptr, size_t *n, FILE *stream)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
34 {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
35 size_t res = 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
36 int c;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
37 if (*lineptr == NULL) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
38 *lineptr = malloc(4096);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
39 if (*lineptr)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
40 *n = 4096;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
41 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
42 else if (*n == 0) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
43 char *tmp = realloc(*lineptr, 4096);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
44 if (tmp) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
45 *lineptr = tmp;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
46 *n = 4096;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
47 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
48 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
49 if (*lineptr == NULL || *n == 0)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
50 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
51
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
52 for (c = fgetc(stream); c != EOF; c = fgetc(stream)) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
53 if (res + 1 >= *n) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
54 char *tmp = realloc(*lineptr, *n * 2);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
55 if (tmp == NULL)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
56 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
57 *lineptr = tmp;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
58 *n *= 2;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
59 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
60 (*lineptr)[res++] = c;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
61 if (c == '\n') {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
62 (*lineptr)[res] = 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
63 return res;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
64 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
65 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
66 if (res == 0)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
67 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
68 (*lineptr)[res] = 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
69 return res;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
70 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
71 #endif
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
72
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
73 /**********************************************************************
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
74 * MPEG parsing
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
75 **********************************************************************/
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
76
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
77 typedef struct {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
78 stream_t *stream;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
79 unsigned int pts;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
80 int aid;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
81 unsigned char *packet;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
82 unsigned int packet_reserve;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
83 unsigned int packet_size;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
84 } mpeg_t;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
85
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
86 static mpeg_t *
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
87 mpeg_open(const char *filename)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
88 {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
89 mpeg_t *res = malloc(sizeof(mpeg_t));
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
90 int err = res == NULL;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
91 if (!err) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
92 int fd;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
93 res->pts = 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
94 res->aid = -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
95 res->packet = NULL;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
96 res->packet_size = 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
97 res->packet_reserve = 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
98 fd = open(filename, O_RDONLY);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
99 err = fd < 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
100 if (!err) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
101 res->stream = new_stream(fd, STREAMTYPE_FILE);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
102 err = res->stream == NULL;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
103 if (err)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
104 close(fd);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
105 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
106 if (err)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
107 free(res);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
108 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
109 return err ? NULL : res;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
110 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
111
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
112 static void
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
113 mpeg_free(mpeg_t *mpeg)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
114 {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
115 int fd;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
116 if (mpeg->packet)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
117 free(mpeg->packet);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
118 fd = mpeg->stream->fd;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
119 free_stream(mpeg->stream);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
120 close(fd);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
121 free(mpeg);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
122 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
123
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
124 static int
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
125 mpeg_eof(mpeg_t *mpeg)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
126 {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
127 return stream_eof(mpeg->stream);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
128 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
129
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
130 static off_t
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
131 mpeg_tell(mpeg_t *mpeg)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
132 {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
133 return stream_tell(mpeg->stream);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
134 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
135
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
136 static int
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
137 mpeg_run(mpeg_t *mpeg)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
138 {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
139 unsigned int len, idx, version;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
140 int c;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
141 /* Goto start of a packet, it starts with 0x000001?? */
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
142 const unsigned char wanted[] = { 0, 0, 1 };
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
143 unsigned char buf[5];
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
144
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
145 mpeg->aid = -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
146 mpeg->packet_size = 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
147 if (stream_read(mpeg->stream, buf, 4) != 4)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
148 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
149 while (memcmp(buf, wanted, sizeof(wanted)) != 0) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
150 c = stream_read_char(mpeg->stream);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
151 if (c < 0)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
152 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
153 memmove(buf, buf + 1, 3);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
154 buf[3] = c;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
155 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
156 switch (buf[3]) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
157 case 0xb9: /* System End Code */
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
158 break;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
159 case 0xba: /* Packet start code */
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
160 c = stream_read_char(mpeg->stream);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
161 if (c < 0)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
162 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
163 if ((c & 0xc0) == 0x40)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
164 version = 4;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
165 else if ((c & 0xf0) == 0x20)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
166 version = 2;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
167 else {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
168 fprintf(stderr, "Unsupported MPEG version: 0x%02x", c);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
169 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
170 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
171 if (version == 4) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
172 if (!stream_skip(mpeg->stream, 9))
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
173 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
174 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
175 else if (version == 2) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
176 if (!stream_skip(mpeg->stream, 7))
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
177 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
178 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
179 else
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
180 abort();
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
181 break;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
182 case 0xbd: /* packet */
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
183 if (stream_read(mpeg->stream, buf, 2) != 2)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
184 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
185 len = buf[0] << 8 | buf[1];
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
186 idx = mpeg_tell(mpeg);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
187 c = stream_read_char(mpeg->stream);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
188 if (c < 0)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
189 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
190 if ((c & 0xC0) == 0x40) { /* skip STD scale & size */
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
191 if (stream_read_char(mpeg->stream) < 0)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
192 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
193 c = stream_read_char(mpeg->stream);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
194 if (c < 0)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
195 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
196 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
197 if ((c & 0xf0) == 0x20) { /* System-1 stream timestamp */
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
198 /* Do we need this? */
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
199 abort();
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
200 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
201 else if ((c & 0xf0) == 0x30) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
202 /* Do we need this? */
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
203 abort();
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
204 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
205 else if ((c & 0xc0) == 0x80) { /* System-2 (.VOB) stream */
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
206 unsigned int pts_flags, hdrlen, dataidx;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
207 c = stream_read_char(mpeg->stream);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
208 if (c < 0)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
209 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
210 pts_flags = c;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
211 c = stream_read_char(mpeg->stream);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
212 if (c < 0)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
213 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
214 hdrlen = c;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
215 dataidx = mpeg_tell(mpeg) + hdrlen;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
216 if (dataidx > idx + len) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
217 fprintf(stderr, "Invalid header length: %d (total length: %d, idx: %d, dataidx: %d)\n",
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
218 hdrlen, len, idx, dataidx);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
219 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
220 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
221 if ((pts_flags & 0xc0) == 0x80) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
222 if (stream_read(mpeg->stream, buf, 5) != 5)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
223 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
224 if (!(((buf[0] & 0xf0) == 0x20) && (buf[0] & 1) && (buf[2] & 1) && (buf[4] & 1))) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
225 fprintf(stderr, "vobsub PTS error: 0x%02x %02x%02x %02x%02x \n",
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
226 buf[0], buf[1], buf[2], buf[3], buf[4]);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
227 mpeg->pts = 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
228 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
229 else
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
230 mpeg->pts = ((buf[0] & 0x0e) << 29 | buf[1] << 22 | (buf[2] & 0xfe) << 14
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
231 | buf[3] << 7 | (buf[4] >> 1)) / 900;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
232 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
233 else /* if ((pts_flags & 0xc0) == 0xc0) */ {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
234 /* what's this? */
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
235 /* abort(); */
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
236 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
237 stream_seek(mpeg->stream, dataidx);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
238 mpeg->aid = stream_read_char(mpeg->stream);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
239 if (mpeg->aid < 0) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
240 fprintf(stderr, "Bogus aid %d\n", mpeg->aid);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
241 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
242 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
243 mpeg->packet_size = len - ((unsigned int) mpeg_tell(mpeg) - idx);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
244 if (mpeg->packet_reserve < mpeg->packet_size) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
245 if (mpeg->packet)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
246 free(mpeg->packet);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
247 mpeg->packet = malloc(mpeg->packet_size);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
248 if (mpeg->packet)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
249 mpeg->packet_reserve = mpeg->packet_size;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
250 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
251 if (mpeg->packet == NULL) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
252 perror("malloc failure");
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
253 mpeg->packet_reserve = 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
254 mpeg->packet_size = 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
255 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
256 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
257 if (stream_read(mpeg->stream, mpeg->packet, mpeg->packet_size) != mpeg->packet_size) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
258 perror("stream_read failure");
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
259 mpeg->packet_size = 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
260 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
261 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
262 idx = len;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
263 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
264 break;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
265 case 0xbe: /* Padding */
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
266 if (stream_read(mpeg->stream, buf, 2) != 2)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
267 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
268 len = buf[0] << 8 | buf[1];
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
269 if (len > 0 && !stream_skip(mpeg->stream, len))
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
270 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
271 break;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
272 default:
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
273 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
274 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
275 return 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
276 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
277
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
278 /**********************************************************************
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
279 * Packet queue
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
280 **********************************************************************/
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
281
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
282 typedef struct {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
283 unsigned int pts100;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
284 off_t filepos;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
285 unsigned int size;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
286 unsigned char *data;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
287 } packet_t;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
288
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
289 typedef struct {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
290 char *id;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
291 packet_t *packets;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
292 unsigned int packets_reserve;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
293 unsigned int packets_size;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
294 unsigned int current_index;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
295 } packet_queue_t;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
296
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
297 static void
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
298 packet_construct(packet_t *pkt)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
299 {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
300 pkt->pts100 = 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
301 pkt->filepos = 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
302 pkt->size = 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
303 pkt->data = NULL;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
304 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
305
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
306 static void
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
307 packet_destroy(packet_t *pkt)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
308 {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
309 if (pkt->data)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
310 free(pkt->data);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
311 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
312
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
313 static void
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
314 packet_queue_construct(packet_queue_t *queue)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
315 {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
316 queue->id = NULL;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
317 queue->packets = NULL;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
318 queue->packets_reserve = 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
319 queue->packets_size = 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
320 queue->current_index = 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
321 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
322
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
323 static void
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
324 packet_queue_destroy(packet_queue_t *queue)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
325 {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
326 if (queue->packets) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
327 while (queue->packets_size--)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
328 packet_destroy(queue->packets + queue->packets_size);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
329 free(queue->packets);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
330 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
331 return;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
332 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
333
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
334 /* Make sure there is enough room for needed_size packets in the
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
335 packet queue. */
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
336 static int
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
337 packet_queue_ensure(packet_queue_t *queue, unsigned int needed_size)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
338 {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
339 if (queue->packets_reserve < needed_size) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
340 if (queue->packets) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
341 packet_t *tmp = realloc(queue->packets, 2 * queue->packets_reserve * sizeof(packet_t));
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
342 if (tmp == NULL) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
343 perror("realloc failure");
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
344 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
345 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
346 queue->packets = tmp;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
347 queue->packets_reserve *= 2;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
348 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
349 else {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
350 queue->packets = malloc(sizeof(packet_t));
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
351 if (queue->packets == NULL) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
352 perror("malloc failure");
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
353 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
354 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
355 queue->packets_reserve = 1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
356 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
357 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
358 return 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
359 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
360
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
361 /* add one more packet */
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
362 static int
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
363 packet_queue_grow(packet_queue_t *queue)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
364 {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
365 if (packet_queue_ensure(queue, queue->packets_size + 1) < 0)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
366 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
367 packet_construct(queue->packets + queue->packets_size);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
368 ++queue->packets_size;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
369 return 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
370 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
371
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
372 /* insert a new packet, duplicating pts from the current one */
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
373 static int
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
374 packet_queue_insert(packet_queue_t *queue)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
375 {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
376 packet_t *pkts;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
377 if (packet_queue_ensure(queue, queue->packets_size + 1) < 0)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
378 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
379 /* XXX packet_size does not reflect the real thing here, it will be updated a bit later */
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
380 memmove(queue->packets + queue->current_index + 2,
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
381 queue->packets + queue->current_index + 1,
4085
dc0a91965b97 Support vobsub without index files.
kmkaplan
parents: 4080
diff changeset
382 sizeof(packet_t) * (queue->packets_size - queue->current_index - 1));
4080
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
383 pkts = queue->packets + queue->current_index;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
384 ++queue->packets_size;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
385 ++queue->current_index;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
386 packet_construct(pkts + 1);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
387 pkts[1].pts100 = pkts[0].pts100;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
388 pkts[1].filepos = pkts[0].filepos;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
389 return 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
390 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
391
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
392 /**********************************************************************
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
393 * Vosub
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
394 **********************************************************************/
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
395
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
396 typedef struct {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
397 void *spudec;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
398 unsigned int palette[16];
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
399 /* index */
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
400 packet_queue_t *spu_streams;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
401 unsigned int spu_streams_size;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
402 unsigned int spu_streams_current;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
403 } vobsub_t;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
404
4085
dc0a91965b97 Support vobsub without index files.
kmkaplan
parents: 4080
diff changeset
405 /* Make sure that the spu stream idx exists. */
4080
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
406 static int
4085
dc0a91965b97 Support vobsub without index files.
kmkaplan
parents: 4080
diff changeset
407 vobsub_ensure_spu_stream(vobsub_t *vob, unsigned int index)
4080
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
408 {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
409 if (index >= vob->spu_streams_size) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
410 /* This is a new stream */
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
411 if (vob->spu_streams) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
412 packet_queue_t *tmp = realloc(vob->spu_streams, (index + 1) * sizeof(packet_queue_t));
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
413 if (tmp == NULL) {
4085
dc0a91965b97 Support vobsub without index files.
kmkaplan
parents: 4080
diff changeset
414 perror("vobsub_ensure_spu_stream: realloc failure");
4080
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
415 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
416 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
417 vob->spu_streams = tmp;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
418 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
419 else {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
420 vob->spu_streams = malloc((index + 1) * sizeof(packet_queue_t));
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
421 if (vob->spu_streams == NULL) {
4085
dc0a91965b97 Support vobsub without index files.
kmkaplan
parents: 4080
diff changeset
422 perror("vobsub_ensure_spu_stream: malloc failure");
4080
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
423 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
424 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
425 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
426 while (vob->spu_streams_size <= index) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
427 packet_queue_construct(vob->spu_streams + vob->spu_streams_size);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
428 ++vob->spu_streams_size;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
429 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
430 }
4085
dc0a91965b97 Support vobsub without index files.
kmkaplan
parents: 4080
diff changeset
431 return 0;
dc0a91965b97 Support vobsub without index files.
kmkaplan
parents: 4080
diff changeset
432 }
dc0a91965b97 Support vobsub without index files.
kmkaplan
parents: 4080
diff changeset
433
dc0a91965b97 Support vobsub without index files.
kmkaplan
parents: 4080
diff changeset
434 static int
dc0a91965b97 Support vobsub without index files.
kmkaplan
parents: 4080
diff changeset
435 vobsub_add_id(vobsub_t *vob, const char *id, size_t idlen, const unsigned int index)
dc0a91965b97 Support vobsub without index files.
kmkaplan
parents: 4080
diff changeset
436 {
dc0a91965b97 Support vobsub without index files.
kmkaplan
parents: 4080
diff changeset
437 if (vobsub_ensure_spu_stream(vob, index) < 0)
dc0a91965b97 Support vobsub without index files.
kmkaplan
parents: 4080
diff changeset
438 return -1;
4080
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
439 if (id && idlen) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
440 if (vob->spu_streams[index].id)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
441 free(vob->spu_streams[index].id);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
442 vob->spu_streams[index].id = malloc(idlen + 1);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
443 if (vob->spu_streams[index].id == NULL) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
444 perror("vobsub_add_id: malloc failure");
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
445 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
446 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
447 vob->spu_streams[index].id[idlen] = 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
448 memcpy(vob->spu_streams[index].id, id, idlen);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
449 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
450 vob->spu_streams_current = index;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
451 if (verbose)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
452 fprintf(stderr, "[vobsub] subtitle (vobsubid): %d language %s\n",
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
453 index, vob->spu_streams[index].id);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
454 return 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
455 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
456
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
457 static int
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
458 vobsub_add_timestamp(vobsub_t *vob, off_t filepos, unsigned int ms)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
459 {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
460 packet_queue_t *queue;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
461 packet_t *pkt;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
462 if (vob->spu_streams == 0) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
463 fprintf(stderr, "[vobsub] warning, binning some index entries. Check your index file\n");
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
464 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
465 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
466 queue = vob->spu_streams + vob->spu_streams_current;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
467 if (packet_queue_grow(queue) >= 0) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
468 pkt = queue->packets + (queue->packets_size - 1);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
469 pkt->filepos = filepos;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
470 pkt->pts100 = ms / 10;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
471 return 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
472 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
473 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
474 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
475
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
476 static int
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
477 vobsub_parse_id(vobsub_t *vob, const char *line)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
478 {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
479 // id: xx, index: n
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
480 size_t idlen;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
481 const char *p, *q;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
482 p = line;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
483 while (isspace(*p))
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
484 ++p;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
485 q = p;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
486 while (isalpha(*q))
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
487 ++q;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
488 idlen = q - p;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
489 if (idlen == 0)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
490 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
491 ++q;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
492 while (isspace(*q))
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
493 ++q;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
494 if (strncmp("index:", q, 6))
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
495 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
496 q += 6;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
497 while (isspace(*q))
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
498 ++q;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
499 if (!isdigit(*q))
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
500 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
501 return vobsub_add_id(vob, p, idlen, atoi(q));
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
502 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
503
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
504 static int
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
505 vobsub_parse_timestamp(vobsub_t *vob, const char *line)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
506 {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
507 // timestamp: HH:MM:SS.mmm, filepos: 0nnnnnnnnn
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
508 const char *p;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
509 int h, m, s, ms;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
510 off_t filepos;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
511 while (isspace(*line))
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
512 ++line;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
513 p = line;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
514 while (isdigit(*p))
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
515 ++p;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
516 if (p - line != 2)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
517 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
518 h = atoi(line);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
519 if (*p != ':')
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
520 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
521 line = ++p;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
522 while (isdigit(*p))
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
523 ++p;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
524 if (p - line != 2)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
525 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
526 m = atoi(line);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
527 if (*p != ':')
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
528 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
529 line = ++p;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
530 while (isdigit(*p))
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
531 ++p;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
532 if (p - line != 2)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
533 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
534 s = atoi(line);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
535 if (*p != ':')
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
536 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
537 line = ++p;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
538 while (isdigit(*p))
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
539 ++p;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
540 if (p - line != 3)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
541 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
542 ms = atoi(line);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
543 if (*p != ',')
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
544 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
545 line = p + 1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
546 while (isspace(*line))
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
547 ++line;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
548 if (strncmp("filepos:", line, 8))
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
549 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
550 line += 8;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
551 while (isspace(*line))
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
552 ++line;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
553 if (! isxdigit(*line))
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
554 return -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
555 filepos = strtol(line, NULL, 16);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
556 return vobsub_add_timestamp(vob, filepos, ms + 1000 * (s + 60 * (m + 60 * h)));
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
557 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
558
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
559 static int
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
560 vobsub_parse_one_line(vobsub_t *vob, FILE *fd)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
561 {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
562 ssize_t line_size;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
563 int res = -1;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
564 do {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
565 int line_reserve = 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
566 char *line = NULL;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
567 line_size = getline(&line, &line_reserve, fd);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
568 if (line_size < 0) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
569 if (line)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
570 free(line);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
571 break;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
572 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
573 if (*line == 0 || *line == '\r' || *line == '\n' || *line == '#')
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
574 continue;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
575 else if (strncmp("id:", line, 3) == 0)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
576 res = vobsub_parse_id(vob, line + 3);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
577 else if (strncmp("timestamp:", line, 10) == 0)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
578 res = vobsub_parse_timestamp(vob, line + 10);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
579 else {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
580 if (verbose)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
581 fprintf(stderr, "vobsub: ignoring %s", line);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
582 continue;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
583 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
584 if (res < 0)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
585 fprintf(stderr, "ERROR in %s", line);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
586 break;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
587 } while (1);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
588 return res;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
589 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
590
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
591 void *
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
592 vobsub_open(const char *const name)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
593 {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
594 vobsub_t *vob = malloc(sizeof(vobsub_t));
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
595 if (vob) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
596 char *buf;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
597 vob->spudec = NULL;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
598 vob->spu_streams = NULL;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
599 vob->spu_streams_size = 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
600 vob->spu_streams_current = 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
601 buf = malloc((strlen(name) + 5) * sizeof(char));
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
602 if (buf) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
603 FILE *fd;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
604 mpeg_t *mpg;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
605 strcpy(buf, name);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
606 strcat(buf, ".ifo");
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
607 fd = fopen(buf, "rb");
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
608 if (fd == NULL)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
609 perror("Can't open IFO file");
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
610 else {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
611 // parse IFO header
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
612 unsigned char block[0x800];
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
613 const char *const ifo_magic = "DVDVIDEO-VTS";
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
614 if (fread(block, sizeof(block), 1, fd) != 1)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
615 perror("Can't read IFO header");
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
616 else if (memcmp(block, ifo_magic, strlen(ifo_magic) + 1))
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
617 fprintf(stderr, "Bad magic in IFO header\n");
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
618 else {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
619 unsigned long pgci_sector = block[0xcc] << 24 | block[0xcd] << 16
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
620 | block[0xce] << 8 | block[0xcf];
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
621 int standard = (block[0x200] & 0x30) >> 4;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
622 int resolution = (block[0x201] & 0x0c) >> 2;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
623 unsigned int orig_frame_y = standard ? 576 : 480;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
624 unsigned int orig_frame_x = 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
625 switch (resolution) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
626 case 0x0:
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
627 orig_frame_x = 720;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
628 break;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
629 case 0x1:
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
630 orig_frame_x = 704;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
631 break;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
632 case 0x2:
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
633 orig_frame_x = 352;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
634 break;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
635 case 0x3:
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
636 orig_frame_x = 352;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
637 orig_frame_y /= 2;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
638 break;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
639 default:
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
640 fprintf(stderr, "Unknown resolution %d \n", resolution);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
641 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
642 if (fseek(fd, pgci_sector * sizeof(block), SEEK_SET)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
643 || fread(block, sizeof(block), 1, fd) != 1)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
644 perror("Can't read IFO PGCI");
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
645 else {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
646 unsigned long idx;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
647 unsigned long pgc_offset = block[0xc] << 24 | block[0xd] << 16
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
648 | block[0xe] << 8 | block[0xf];
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
649 for (idx = 0; idx < 16; ++idx) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
650 unsigned char *p = block + pgc_offset + 0xa4 + 4 * idx;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
651 vob->palette[idx] = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
652 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
653 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
654 vob->spudec = spudec_new_scaled(vob->palette, orig_frame_x, orig_frame_y);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
655 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
656 fclose(fd);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
657 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
658
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
659 /* read in the index */
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
660 strcpy(buf, name);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
661 strcat(buf, ".idx");
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
662 fd = fopen(buf, "rb");
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
663 if (fd == NULL)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
664 perror("Can't open IDX file");
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
665 else {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
666 while (vobsub_parse_one_line(vob, fd) >= 0)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
667 /* NOOP */ ;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
668 fclose(fd);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
669 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
670
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
671 /* read the indexed mpeg_stream */
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
672 strcpy(buf, name);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
673 strcat(buf, ".sub");
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
674 mpg = mpeg_open(buf);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
675 if (mpg == NULL)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
676 perror("Can't open SUB file");
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
677 else {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
678 long last_pts_diff = 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
679 while (!mpeg_eof(mpg)) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
680 off_t pos = mpeg_tell(mpg);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
681 if (mpeg_run(mpg) < 0) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
682 if (!mpeg_eof(mpg))
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
683 perror("mpeg_run error");
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
684 break;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
685 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
686 if (mpg->packet_size) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
687 if ((mpg->aid & 0xe0) == 0x20) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
688 unsigned int sid = mpg->aid & 0x1f;
4085
dc0a91965b97 Support vobsub without index files.
kmkaplan
parents: 4080
diff changeset
689 if (vobsub_ensure_spu_stream(vob, sid) >= 0) {
4080
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
690 packet_queue_t *queue = vob->spu_streams + sid;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
691 /* get the packet to fill */
4085
dc0a91965b97 Support vobsub without index files.
kmkaplan
parents: 4080
diff changeset
692 if (queue->packets_size == 0 && packet_queue_grow(queue) < 0)
dc0a91965b97 Support vobsub without index files.
kmkaplan
parents: 4080
diff changeset
693 abort();
4080
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
694 while (queue->current_index + 1 < queue->packets_size
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
695 && queue->packets[queue->current_index + 1].filepos <= pos)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
696 ++queue->current_index;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
697 if (queue->current_index < queue->packets_size) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
698 packet_t *pkt;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
699 if (queue->packets[queue->current_index].data) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
700 /* insert a new packet and fix the PTS ! */
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
701 packet_queue_insert(queue);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
702 queue->packets[queue->current_index].pts100 =
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
703 mpg->pts + last_pts_diff;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
704 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
705 pkt = queue->packets + queue->current_index;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
706 last_pts_diff = pkt->pts100 - mpg->pts;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
707 /* FIXME: should not use mpg_sub internal informations, make a copy */
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
708 pkt->data = mpg->packet;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
709 pkt->size = mpg->packet_size;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
710 mpg->packet = NULL;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
711 mpg->packet_reserve = 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
712 mpg->packet_size = 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
713 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
714 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
715 else
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
716 fprintf(stderr, "don't know what to do with subtitle #%u\n", sid);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
717 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
718 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
719 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
720 vob->spu_streams_current = vob->spu_streams_size;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
721 while (vob->spu_streams_current-- > 0)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
722 vob->spu_streams[vob->spu_streams_current].current_index = 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
723 mpeg_free(mpg);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
724 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
725 free(buf);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
726 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
727 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
728 return vob;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
729 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
730
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
731 void
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
732 vobsub_close(void *this)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
733 {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
734 vobsub_t *vob = (vobsub_t *)this;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
735 if (vob->spudec)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
736 spudec_free(vob->spudec);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
737 if (vob->spu_streams) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
738 while (vob->spu_streams_size--)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
739 packet_queue_destroy(vob->spu_streams + vob->spu_streams_size);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
740 free(vob->spu_streams);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
741 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
742 free(vob);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
743 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
744
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
745 void vobsub_draw(void *this, int dxs, int dys, void (*draw_alpha)(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride))
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
746 {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
747 vobsub_t *vob = (vobsub_t *)this;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
748 if (vob->spudec)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
749 spudec_draw_scaled(vob->spudec, dxs, dys, draw_alpha);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
750 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
751
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
752 void
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
753 vobsub_process(void *vobhandle, float pts)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
754 {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
755 vobsub_t *vob = (vobsub_t *)vobhandle;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
756 unsigned int pts100 = 100 * pts;
4114
aeb27b09de8e Check for NULL vob->spudec before using.
kmkaplan
parents: 4085
diff changeset
757 if (vob->spudec) {
4080
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
758 spudec_heartbeat(vob->spudec, pts100);
4114
aeb27b09de8e Check for NULL vob->spudec before using.
kmkaplan
parents: 4085
diff changeset
759 if (vob->spu_streams && 0 <= vobsub_id && (unsigned) vobsub_id < vob->spu_streams_size) {
aeb27b09de8e Check for NULL vob->spudec before using.
kmkaplan
parents: 4085
diff changeset
760 packet_queue_t *queue = vob->spu_streams + vobsub_id;
aeb27b09de8e Check for NULL vob->spudec before using.
kmkaplan
parents: 4085
diff changeset
761 while (queue->current_index < queue->packets_size) {
aeb27b09de8e Check for NULL vob->spudec before using.
kmkaplan
parents: 4085
diff changeset
762 packet_t *pkt = queue->packets + queue->current_index;
aeb27b09de8e Check for NULL vob->spudec before using.
kmkaplan
parents: 4085
diff changeset
763 if (pkt->pts100 <= pts100) {
aeb27b09de8e Check for NULL vob->spudec before using.
kmkaplan
parents: 4085
diff changeset
764 spudec_assemble(vob->spudec, pkt->data, pkt->size, pkt->pts100);
aeb27b09de8e Check for NULL vob->spudec before using.
kmkaplan
parents: 4085
diff changeset
765 ++queue->current_index;
aeb27b09de8e Check for NULL vob->spudec before using.
kmkaplan
parents: 4085
diff changeset
766 }
aeb27b09de8e Check for NULL vob->spudec before using.
kmkaplan
parents: 4085
diff changeset
767 else
aeb27b09de8e Check for NULL vob->spudec before using.
kmkaplan
parents: 4085
diff changeset
768 break;
4080
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
769 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
770 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
771 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
772 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
773
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
774 void
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
775 vobsub_reset(void *vobhandle)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
776 {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
777 vobsub_t *vob = (vobsub_t *)vobhandle;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
778 if (vob->spu_streams) {
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
779 unsigned int n = vob->spu_streams_size;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
780 while (n-- > 0)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
781 vob->spu_streams[n].current_index = 0;
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
782 }
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
783 if (vob->spudec)
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
784 spudec_reset(vob->spudec);
47bcafe1442e Add vobsub support.
kmkaplan
parents:
diff changeset
785 }