Mercurial > mplayer.hg
annotate vobsub.c @ 5425:f3a451b6f794
-alang sig11 when playing avis fixed - 10l
author | arpi |
---|---|
date | Sun, 31 Mar 2002 19:20:01 +0000 |
parents | 3af2729c5c87 |
children | 71372b7125cf |
rev | line source |
---|---|
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: | |
5388
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
273 if (0xc0 <= buf[3] && buf[3] < 0xf0) { |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
274 /* MPEG audio or video */ |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
275 if (stream_read(mpeg->stream, buf, 2) != 2) |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
276 return -1; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
277 len = buf[0] << 8 | buf[1]; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
278 if (len > 0 && !stream_skip(mpeg->stream, len)) |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
279 return -1; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
280 |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
281 } |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
282 else { |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
283 fprintf(stderr, "unknown header 0x%02X%02X%02X%02X\n", |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
284 buf[0], buf[1], buf[2], buf[3]); |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
285 return -1; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
286 } |
4080 | 287 } |
288 return 0; | |
289 } | |
290 | |
291 /********************************************************************** | |
292 * Packet queue | |
293 **********************************************************************/ | |
294 | |
295 typedef struct { | |
296 unsigned int pts100; | |
297 off_t filepos; | |
298 unsigned int size; | |
299 unsigned char *data; | |
300 } packet_t; | |
301 | |
302 typedef struct { | |
303 char *id; | |
304 packet_t *packets; | |
305 unsigned int packets_reserve; | |
306 unsigned int packets_size; | |
307 unsigned int current_index; | |
308 } packet_queue_t; | |
309 | |
310 static void | |
311 packet_construct(packet_t *pkt) | |
312 { | |
313 pkt->pts100 = 0; | |
314 pkt->filepos = 0; | |
315 pkt->size = 0; | |
316 pkt->data = NULL; | |
317 } | |
318 | |
319 static void | |
320 packet_destroy(packet_t *pkt) | |
321 { | |
322 if (pkt->data) | |
323 free(pkt->data); | |
324 } | |
325 | |
326 static void | |
327 packet_queue_construct(packet_queue_t *queue) | |
328 { | |
329 queue->id = NULL; | |
330 queue->packets = NULL; | |
331 queue->packets_reserve = 0; | |
332 queue->packets_size = 0; | |
333 queue->current_index = 0; | |
334 } | |
335 | |
336 static void | |
337 packet_queue_destroy(packet_queue_t *queue) | |
338 { | |
339 if (queue->packets) { | |
340 while (queue->packets_size--) | |
341 packet_destroy(queue->packets + queue->packets_size); | |
342 free(queue->packets); | |
343 } | |
344 return; | |
345 } | |
346 | |
347 /* Make sure there is enough room for needed_size packets in the | |
348 packet queue. */ | |
349 static int | |
350 packet_queue_ensure(packet_queue_t *queue, unsigned int needed_size) | |
351 { | |
352 if (queue->packets_reserve < needed_size) { | |
353 if (queue->packets) { | |
354 packet_t *tmp = realloc(queue->packets, 2 * queue->packets_reserve * sizeof(packet_t)); | |
355 if (tmp == NULL) { | |
356 perror("realloc failure"); | |
357 return -1; | |
358 } | |
359 queue->packets = tmp; | |
360 queue->packets_reserve *= 2; | |
361 } | |
362 else { | |
363 queue->packets = malloc(sizeof(packet_t)); | |
364 if (queue->packets == NULL) { | |
365 perror("malloc failure"); | |
366 return -1; | |
367 } | |
368 queue->packets_reserve = 1; | |
369 } | |
370 } | |
371 return 0; | |
372 } | |
373 | |
374 /* add one more packet */ | |
375 static int | |
376 packet_queue_grow(packet_queue_t *queue) | |
377 { | |
378 if (packet_queue_ensure(queue, queue->packets_size + 1) < 0) | |
379 return -1; | |
380 packet_construct(queue->packets + queue->packets_size); | |
381 ++queue->packets_size; | |
382 return 0; | |
383 } | |
384 | |
385 /* insert a new packet, duplicating pts from the current one */ | |
386 static int | |
387 packet_queue_insert(packet_queue_t *queue) | |
388 { | |
389 packet_t *pkts; | |
390 if (packet_queue_ensure(queue, queue->packets_size + 1) < 0) | |
391 return -1; | |
392 /* XXX packet_size does not reflect the real thing here, it will be updated a bit later */ | |
393 memmove(queue->packets + queue->current_index + 2, | |
394 queue->packets + queue->current_index + 1, | |
4085 | 395 sizeof(packet_t) * (queue->packets_size - queue->current_index - 1)); |
4080 | 396 pkts = queue->packets + queue->current_index; |
397 ++queue->packets_size; | |
398 ++queue->current_index; | |
399 packet_construct(pkts + 1); | |
400 pkts[1].pts100 = pkts[0].pts100; | |
401 pkts[1].filepos = pkts[0].filepos; | |
402 return 0; | |
403 } | |
404 | |
405 /********************************************************************** | |
406 * Vosub | |
407 **********************************************************************/ | |
408 | |
409 typedef struct { | |
410 void *spudec; | |
411 unsigned int palette[16]; | |
5388
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
412 unsigned int orig_frame_width, orig_frame_height; |
4080 | 413 /* index */ |
414 packet_queue_t *spu_streams; | |
415 unsigned int spu_streams_size; | |
416 unsigned int spu_streams_current; | |
417 } vobsub_t; | |
418 | |
4085 | 419 /* Make sure that the spu stream idx exists. */ |
4080 | 420 static int |
4085 | 421 vobsub_ensure_spu_stream(vobsub_t *vob, unsigned int index) |
4080 | 422 { |
423 if (index >= vob->spu_streams_size) { | |
424 /* This is a new stream */ | |
425 if (vob->spu_streams) { | |
426 packet_queue_t *tmp = realloc(vob->spu_streams, (index + 1) * sizeof(packet_queue_t)); | |
427 if (tmp == NULL) { | |
4085 | 428 perror("vobsub_ensure_spu_stream: realloc failure"); |
4080 | 429 return -1; |
430 } | |
431 vob->spu_streams = tmp; | |
432 } | |
433 else { | |
434 vob->spu_streams = malloc((index + 1) * sizeof(packet_queue_t)); | |
435 if (vob->spu_streams == NULL) { | |
4085 | 436 perror("vobsub_ensure_spu_stream: malloc failure"); |
4080 | 437 return -1; |
438 } | |
439 } | |
440 while (vob->spu_streams_size <= index) { | |
441 packet_queue_construct(vob->spu_streams + vob->spu_streams_size); | |
442 ++vob->spu_streams_size; | |
443 } | |
444 } | |
4085 | 445 return 0; |
446 } | |
447 | |
448 static int | |
449 vobsub_add_id(vobsub_t *vob, const char *id, size_t idlen, const unsigned int index) | |
450 { | |
451 if (vobsub_ensure_spu_stream(vob, index) < 0) | |
452 return -1; | |
4080 | 453 if (id && idlen) { |
454 if (vob->spu_streams[index].id) | |
455 free(vob->spu_streams[index].id); | |
456 vob->spu_streams[index].id = malloc(idlen + 1); | |
457 if (vob->spu_streams[index].id == NULL) { | |
458 perror("vobsub_add_id: malloc failure"); | |
459 return -1; | |
460 } | |
461 vob->spu_streams[index].id[idlen] = 0; | |
462 memcpy(vob->spu_streams[index].id, id, idlen); | |
463 } | |
464 vob->spu_streams_current = index; | |
465 if (verbose) | |
466 fprintf(stderr, "[vobsub] subtitle (vobsubid): %d language %s\n", | |
467 index, vob->spu_streams[index].id); | |
468 return 0; | |
469 } | |
470 | |
471 static int | |
472 vobsub_add_timestamp(vobsub_t *vob, off_t filepos, unsigned int ms) | |
473 { | |
474 packet_queue_t *queue; | |
475 packet_t *pkt; | |
476 if (vob->spu_streams == 0) { | |
477 fprintf(stderr, "[vobsub] warning, binning some index entries. Check your index file\n"); | |
478 return -1; | |
479 } | |
480 queue = vob->spu_streams + vob->spu_streams_current; | |
481 if (packet_queue_grow(queue) >= 0) { | |
482 pkt = queue->packets + (queue->packets_size - 1); | |
483 pkt->filepos = filepos; | |
484 pkt->pts100 = ms / 10; | |
485 return 0; | |
486 } | |
487 return -1; | |
488 } | |
489 | |
490 static int | |
491 vobsub_parse_id(vobsub_t *vob, const char *line) | |
492 { | |
493 // id: xx, index: n | |
494 size_t idlen; | |
495 const char *p, *q; | |
496 p = line; | |
497 while (isspace(*p)) | |
498 ++p; | |
499 q = p; | |
500 while (isalpha(*q)) | |
501 ++q; | |
502 idlen = q - p; | |
503 if (idlen == 0) | |
504 return -1; | |
505 ++q; | |
506 while (isspace(*q)) | |
507 ++q; | |
508 if (strncmp("index:", q, 6)) | |
509 return -1; | |
510 q += 6; | |
511 while (isspace(*q)) | |
512 ++q; | |
513 if (!isdigit(*q)) | |
514 return -1; | |
515 return vobsub_add_id(vob, p, idlen, atoi(q)); | |
516 } | |
517 | |
518 static int | |
519 vobsub_parse_timestamp(vobsub_t *vob, const char *line) | |
520 { | |
521 // timestamp: HH:MM:SS.mmm, filepos: 0nnnnnnnnn | |
522 const char *p; | |
523 int h, m, s, ms; | |
524 off_t filepos; | |
525 while (isspace(*line)) | |
526 ++line; | |
527 p = line; | |
528 while (isdigit(*p)) | |
529 ++p; | |
530 if (p - line != 2) | |
531 return -1; | |
532 h = atoi(line); | |
533 if (*p != ':') | |
534 return -1; | |
535 line = ++p; | |
536 while (isdigit(*p)) | |
537 ++p; | |
538 if (p - line != 2) | |
539 return -1; | |
540 m = atoi(line); | |
541 if (*p != ':') | |
542 return -1; | |
543 line = ++p; | |
544 while (isdigit(*p)) | |
545 ++p; | |
546 if (p - line != 2) | |
547 return -1; | |
548 s = atoi(line); | |
549 if (*p != ':') | |
550 return -1; | |
551 line = ++p; | |
552 while (isdigit(*p)) | |
553 ++p; | |
554 if (p - line != 3) | |
555 return -1; | |
556 ms = atoi(line); | |
557 if (*p != ',') | |
558 return -1; | |
559 line = p + 1; | |
560 while (isspace(*line)) | |
561 ++line; | |
562 if (strncmp("filepos:", line, 8)) | |
563 return -1; | |
564 line += 8; | |
565 while (isspace(*line)) | |
566 ++line; | |
567 if (! isxdigit(*line)) | |
568 return -1; | |
569 filepos = strtol(line, NULL, 16); | |
570 return vobsub_add_timestamp(vob, filepos, ms + 1000 * (s + 60 * (m + 60 * h))); | |
571 } | |
572 | |
573 static int | |
5388
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
574 vobsub_parse_size(vobsub_t *vob, const char *line) |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
575 { |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
576 // size: WWWxHHH |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
577 char *p; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
578 while (isspace(*line)) |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
579 ++line; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
580 if (!isdigit(*line)) |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
581 return -1; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
582 vob->orig_frame_width = strtoul(line, &p, 10); |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
583 if (*p != 'x') |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
584 return -1; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
585 ++p; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
586 vob->orig_frame_height = strtoul(p, NULL, 10); |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
587 return 0; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
588 } |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
589 |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
590 static int |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
591 vobsub_parse_palette(vobsub_t *vob, const char *line) |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
592 { |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
593 // palette: XXXXXX, XXXXXX, XXXXXX, XXXXXX, XXXXXX, XXXXXX, XXXXXX, XXXXXX, XXXXXX, XXXXXX, XXXXXX, XXXXXX, XXXXXX, XXXXXX, XXXXXX, XXXXXX |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
594 unsigned int n; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
595 n = 0; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
596 while (1) { |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
597 const char *p; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
598 while (isspace(*line)) |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
599 ++line; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
600 p = line; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
601 while (isxdigit(*p)) |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
602 ++p; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
603 if (p - line != 6) |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
604 return -1; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
605 vob->palette[n++] = strtoul(line, NULL, 16); |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
606 if (n == 16) |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
607 break; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
608 if (*p == ',') |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
609 ++p; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
610 line = p; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
611 } |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
612 return 0; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
613 } |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
614 |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
615 static int |
4080 | 616 vobsub_parse_one_line(vobsub_t *vob, FILE *fd) |
617 { | |
618 ssize_t line_size; | |
619 int res = -1; | |
620 do { | |
621 int line_reserve = 0; | |
622 char *line = NULL; | |
623 line_size = getline(&line, &line_reserve, fd); | |
624 if (line_size < 0) { | |
625 if (line) | |
626 free(line); | |
627 break; | |
628 } | |
629 if (*line == 0 || *line == '\r' || *line == '\n' || *line == '#') | |
630 continue; | |
631 else if (strncmp("id:", line, 3) == 0) | |
632 res = vobsub_parse_id(vob, line + 3); | |
5388
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
633 else if (strncmp("palette:", line, 8) == 0) |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
634 res = vobsub_parse_palette(vob, line + 8); |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
635 else if (strncmp("size:", line, 5) == 0) |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
636 res = vobsub_parse_size(vob, line + 5); |
4080 | 637 else if (strncmp("timestamp:", line, 10) == 0) |
638 res = vobsub_parse_timestamp(vob, line + 10); | |
639 else { | |
640 if (verbose) | |
641 fprintf(stderr, "vobsub: ignoring %s", line); | |
642 continue; | |
643 } | |
644 if (res < 0) | |
645 fprintf(stderr, "ERROR in %s", line); | |
646 break; | |
647 } while (1); | |
648 return res; | |
649 } | |
650 | |
5388
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
651 int |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
652 vobsub_parse_ifo(const char *const name, unsigned int *palette, unsigned int *width, unsigned int *height, int force) |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
653 { |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
654 int res = -1; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
655 FILE *fd = fopen(name, "rb"); |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
656 if (fd == NULL) |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
657 perror("Can't open IFO file"); |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
658 else { |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
659 // parse IFO header |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
660 unsigned char block[0x800]; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
661 const char *const ifo_magic = "DVDVIDEO-VTS"; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
662 if (fread(block, sizeof(block), 1, fd) != 1) { |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
663 if (force) |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
664 perror("Can't read IFO header"); |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
665 } else if (memcmp(block, ifo_magic, strlen(ifo_magic) + 1)) |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
666 fprintf(stderr, "Bad magic in IFO header\n"); |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
667 else { |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
668 unsigned long pgci_sector = block[0xcc] << 24 | block[0xcd] << 16 |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
669 | block[0xce] << 8 | block[0xcf]; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
670 int standard = (block[0x200] & 0x30) >> 4; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
671 int resolution = (block[0x201] & 0x0c) >> 2; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
672 *height = standard ? 576 : 480; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
673 *width = 0; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
674 switch (resolution) { |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
675 case 0x0: |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
676 *width = 720; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
677 break; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
678 case 0x1: |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
679 *width = 704; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
680 break; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
681 case 0x2: |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
682 *width = 352; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
683 break; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
684 case 0x3: |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
685 *width = 352; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
686 *height /= 2; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
687 break; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
688 default: |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
689 fprintf(stderr, "Unknown resolution %d \n", resolution); |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
690 } |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
691 if (fseek(fd, pgci_sector * sizeof(block), SEEK_SET) |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
692 || fread(block, sizeof(block), 1, fd) != 1) |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
693 perror("Can't read IFO PGCI"); |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
694 else { |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
695 unsigned long idx; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
696 unsigned long pgc_offset = block[0xc] << 24 | block[0xd] << 16 |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
697 | block[0xe] << 8 | block[0xf]; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
698 for (idx = 0; idx < 16; ++idx) { |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
699 unsigned char *p = block + pgc_offset + 0xa4 + 4 * idx; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
700 palette[idx] = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3]; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
701 } |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
702 res = 0; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
703 } |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
704 } |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
705 fclose(fd); |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
706 } |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
707 return res; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
708 } |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
709 |
4080 | 710 void * |
4787 | 711 vobsub_open(const char *const name, const int force) |
4080 | 712 { |
713 vobsub_t *vob = malloc(sizeof(vobsub_t)); | |
714 if (vob) { | |
715 char *buf; | |
716 vob->spudec = NULL; | |
5388
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
717 vob->orig_frame_width = 0; |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
718 vob->orig_frame_height = 0; |
4080 | 719 vob->spu_streams = NULL; |
720 vob->spu_streams_size = 0; | |
721 vob->spu_streams_current = 0; | |
722 buf = malloc((strlen(name) + 5) * sizeof(char)); | |
723 if (buf) { | |
724 FILE *fd; | |
725 mpeg_t *mpg; | |
5388
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
726 /* read in the info file */ |
4080 | 727 strcpy(buf, name); |
728 strcat(buf, ".ifo"); | |
5388
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
729 vobsub_parse_ifo(buf, vob->palette, &vob->orig_frame_width, &vob->orig_frame_height, force); |
4080 | 730 /* read in the index */ |
731 strcpy(buf, name); | |
732 strcat(buf, ".idx"); | |
733 fd = fopen(buf, "rb"); | |
4787 | 734 if (fd == NULL) { |
735 if(force) | |
736 perror("VobSub: Can't open IDX file"); | |
737 else | |
738 return NULL; | |
739 } else { | |
4080 | 740 while (vobsub_parse_one_line(vob, fd) >= 0) |
741 /* NOOP */ ; | |
742 fclose(fd); | |
743 } | |
5388
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
744 if (vob->orig_frame_width && vob->orig_frame_height) |
3af2729c5c87
* New command line switch for mplayer & mencoder:
kmkaplan
parents:
4787
diff
changeset
|
745 vob->spudec = spudec_new_scaled(vob->palette, vob->orig_frame_width, vob->orig_frame_height); |
4080 | 746 |
747 /* read the indexed mpeg_stream */ | |
748 strcpy(buf, name); | |
749 strcat(buf, ".sub"); | |
750 mpg = mpeg_open(buf); | |
4787 | 751 if (mpg == NULL) { |
752 if(force) | |
753 perror("VobSub: Can't open SUB file"); | |
754 } else { | |
4080 | 755 long last_pts_diff = 0; |
756 while (!mpeg_eof(mpg)) { | |
757 off_t pos = mpeg_tell(mpg); | |
758 if (mpeg_run(mpg) < 0) { | |
759 if (!mpeg_eof(mpg)) | |
760 perror("mpeg_run error"); | |
761 break; | |
762 } | |
763 if (mpg->packet_size) { | |
764 if ((mpg->aid & 0xe0) == 0x20) { | |
765 unsigned int sid = mpg->aid & 0x1f; | |
4085 | 766 if (vobsub_ensure_spu_stream(vob, sid) >= 0) { |
4080 | 767 packet_queue_t *queue = vob->spu_streams + sid; |
768 /* get the packet to fill */ | |
4085 | 769 if (queue->packets_size == 0 && packet_queue_grow(queue) < 0) |
770 abort(); | |
4080 | 771 while (queue->current_index + 1 < queue->packets_size |
772 && queue->packets[queue->current_index + 1].filepos <= pos) | |
773 ++queue->current_index; | |
774 if (queue->current_index < queue->packets_size) { | |
775 packet_t *pkt; | |
776 if (queue->packets[queue->current_index].data) { | |
777 /* insert a new packet and fix the PTS ! */ | |
778 packet_queue_insert(queue); | |
779 queue->packets[queue->current_index].pts100 = | |
780 mpg->pts + last_pts_diff; | |
781 } | |
782 pkt = queue->packets + queue->current_index; | |
783 last_pts_diff = pkt->pts100 - mpg->pts; | |
784 /* FIXME: should not use mpg_sub internal informations, make a copy */ | |
785 pkt->data = mpg->packet; | |
786 pkt->size = mpg->packet_size; | |
787 mpg->packet = NULL; | |
788 mpg->packet_reserve = 0; | |
789 mpg->packet_size = 0; | |
790 } | |
791 } | |
792 else | |
793 fprintf(stderr, "don't know what to do with subtitle #%u\n", sid); | |
794 } | |
795 } | |
796 } | |
797 vob->spu_streams_current = vob->spu_streams_size; | |
798 while (vob->spu_streams_current-- > 0) | |
799 vob->spu_streams[vob->spu_streams_current].current_index = 0; | |
800 mpeg_free(mpg); | |
801 } | |
802 free(buf); | |
803 } | |
804 } | |
805 return vob; | |
806 } | |
807 | |
808 void | |
809 vobsub_close(void *this) | |
810 { | |
811 vobsub_t *vob = (vobsub_t *)this; | |
812 if (vob->spudec) | |
813 spudec_free(vob->spudec); | |
814 if (vob->spu_streams) { | |
815 while (vob->spu_streams_size--) | |
816 packet_queue_destroy(vob->spu_streams + vob->spu_streams_size); | |
817 free(vob->spu_streams); | |
818 } | |
819 free(vob); | |
820 } | |
821 | |
822 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)) | |
823 { | |
824 vobsub_t *vob = (vobsub_t *)this; | |
825 if (vob->spudec) | |
826 spudec_draw_scaled(vob->spudec, dxs, dys, draw_alpha); | |
827 } | |
828 | |
829 void | |
830 vobsub_process(void *vobhandle, float pts) | |
831 { | |
832 vobsub_t *vob = (vobsub_t *)vobhandle; | |
833 unsigned int pts100 = 100 * pts; | |
4114 | 834 if (vob->spudec) { |
4080 | 835 spudec_heartbeat(vob->spudec, pts100); |
4114 | 836 if (vob->spu_streams && 0 <= vobsub_id && (unsigned) vobsub_id < vob->spu_streams_size) { |
837 packet_queue_t *queue = vob->spu_streams + vobsub_id; | |
838 while (queue->current_index < queue->packets_size) { | |
839 packet_t *pkt = queue->packets + queue->current_index; | |
840 if (pkt->pts100 <= pts100) { | |
841 spudec_assemble(vob->spudec, pkt->data, pkt->size, pkt->pts100); | |
842 ++queue->current_index; | |
843 } | |
844 else | |
845 break; | |
4080 | 846 } |
847 } | |
848 } | |
849 } | |
850 | |
851 void | |
852 vobsub_reset(void *vobhandle) | |
853 { | |
854 vobsub_t *vob = (vobsub_t *)vobhandle; | |
855 if (vob->spu_streams) { | |
856 unsigned int n = vob->spu_streams_size; | |
857 while (n-- > 0) | |
858 vob->spu_streams[n].current_index = 0; | |
859 } | |
860 if (vob->spudec) | |
861 spudec_reset(vob->spudec); | |
862 } |