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