1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
/*
ogmmerge -- utility for splicing together ogg bitstreams
from component media subtypes
r_vobsub.cpp
VobSub text subtitle reader module
Written by Moritz Bunkus <moritz@bunkus.org>
Based on Xiph.org's 'oggmerge' found in their CVS repository
See http://www.xiph.org
Distributed under the GPL
see the file COPYING for details
or visit http://www.gnu.org/copyleft/gpl.html
*/
#ifdef ENABLE_VOBSUB
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ogg/ogg.h>
#include "ogmmerge.h"
#include "ogmstreams.h"
#include "queue.h"
#include "r_vobsub.h"
#include "subtitles.h"
#define istimestampstr(s) (!strncmp(s, "timestamp: ", 11))
#define iscommafileposstr(s) (!strncmp(s, ", filepos: ", 11))
#define iscolon(s) (*(s) == ':')
#define istwodigits(s) (isdigit(*(s)) && isdigit(*(s + 1)))
#define isthreedigits(s) (isdigit(*(s)) && isdigit(*(s + 1)) && \
isdigit(*(s + 2)))
#define istwodigitscolon(s) (istwodigits(s) && iscolon(s + 2))
#define istimestamp(s) (istwodigitscolon(s) && \
istwodigitscolon(s + 3) && \
istwodigitscolon(s + 6) && \
isthreedigits(s + 9))
#define ishexdigit(s) (isdigit(s) || \
(strchr("abcdefABCDEF", s) != NULL))
#define isfilepos(s) (ishexdigit(*(s)) && ishexdigit(*(s + 1)) && \
ishexdigit(*(s + 2)) && \
ishexdigit(*(s + 3)) && \
ishexdigit(*(s + 4)) && \
ishexdigit(*(s + 5)) && \
ishexdigit(*(s + 6)) && \
ishexdigit(*(s + 7)) && \
ishexdigit(*(s + 8)))
#define isvobsubline(s) (istimestampstr(s) && istimestamp(s + 11) && \
iscommafileposstr(s + 23) && \
isfilepos(s + 34))
int vobsub_reader_c::probe_file(FILE *file, off_t size) {
char chunk[2048];
if (fseeko(file, 0, SEEK_SET) != 0)
return 0;
if (fgets(chunk, 2047, file) == NULL)
return 0;
if (strncmp(chunk, "# VobSub index file, v7",
strlen("# VobSub index file, v7")))
return 0;
if (fseeko(file, 0, SEEK_SET) != 0)
return 0;
return 1;
}
vobsub_reader_c::vobsub_reader_c(char *fname, audio_sync_t *nasync,
range_t *nrange, char **ncomments)
throw (error_c) {
char *name;
if ((file = fopen(fname, "r")) == NULL)
throw error_c("vobsub_reader: Could not open source file.");
if (!vobsub_reader_c::probe_file(file, 0))
throw error_c("vobsub_reader: Source is not a valid VobSub index file.");
name = strdup(fname);
if (name == NULL)
die("strdup");
if ((strlen(name) > 4) && (name[strlen(name) - 4] == '.'))
name[strlen(name) - 4] = 0;
else {
name = (char *)realloc(name, strlen(name) + 5);
if (name == NULL)
die("realloc");
}
strcat(name, ".sub");
if ((subfile = fopen(name, "r")) == NULL)
throw error_c("vobsub_reader: Could not open the sub file.");
vobsub_packetizer = NULL;
all_packetizers = NULL;
num_packetizers = 0;
if (verbose)
fprintf(stderr, "Using VobSub subtitle reader for %s/%s.\n+-> Using " \
"VobSub subtitle output module for subtitles.\n", fname, name);
free(name);
memcpy(&async, nasync, sizeof(audio_sync_t));
memcpy(&range, nrange, sizeof(range_t));
if (ncomments == NULL)
comments = ncomments;
else
comments = dup_comments(ncomments);
}
vobsub_reader_c::~vobsub_reader_c() {
int i;
for (i = 0; i < num_packetizers; i++)
if (all_packetizers[i] != NULL)
delete all_packetizers[i];
if (comments != NULL)
free_comments(comments);
}
void vobsub_reader_c::add_vobsub_packetizer(int width, int height,
char *palette, int langidx,
char *id, int index) {
all_packetizers = (vobsub_packetizer_c **)realloc(all_packetizers,
(num_packetizers + 1) *
sizeof(void *));
if (all_packetizers == NULL)
die("realloc");
try {
vobsub_packetizer = new vobsub_packetizer_c(width, height, palette,
langidx, id, index,
&async, &range, comments);
} catch (error_c error) {
fprintf(stderr, "vobsub_reader: Could not create a new vobsub_packetizer: "
"%s\n", error.get_error());
exit(1);
}
all_packetizers[num_packetizers] = vobsub_packetizer;
num_packetizers++;
}
int vobsub_reader_c::read() {
ogg_int64_t start, filepos, last_start;
off_t last_filepos;
char *s, *s2;
int width = -1, height = -1;
char *palette = NULL;
int langidx = -1;
char *id = NULL;
int index = -1;
int lineno;
chunk[2047] = 0;
lineno = 0;
last_start = -1;
last_filepos = -1;
while (1) {
if (fgets(chunk, 2047, file) == NULL)
break;
lineno++;
if ((*chunk == 0) || (strchr("#\n\r", *chunk) != NULL))
continue;
if (!strncmp(chunk, "size: ", 6)) {
if (sscanf(&chunk[6], "%dx%d", &width, &height) != 2) {
width = -1;
height = -1;
fprintf(stdout, "vobsub_reader: Warning: Incorrect \"size:\" entry "
"on line %d. Ignored.\n", lineno);
}
} else if (!strncmp(chunk, "palette: ", 9)) {
if (strlen(chunk) < 10)
fprintf(stdout, "vobsub_reader: Warning: Incorrect \"palette:\" entry "
"on line %d. Ignored.\n", lineno);
else {
palette = strdup(&chunk[9]);
if (palette == NULL)
die("strdup");
}
} else if (!strncmp(chunk, "langidx: ", 9)) {
langidx = strtol(&chunk[9], NULL, 10);
if ((langidx < 0) || (errno != 0)) {
fprintf(stdout, "vobsub_reader: Warning: Incorrect \"langidx:\" entry "
"on line %d. Ignored.\n", lineno);
langidx = -1;
}
} else if (!strncmp(chunk, "id:", 3)) {
s = &chunk[3];
while (isspace(*s))
s++;
s2 = strchr(s, ',');
if (s2 == NULL) {
fprintf(stdout, "vobsub_reader: Warning: Incorrect \"id:\" entry "
"on line %d. Ignored.\n", lineno);
continue;
}
*s2 = 0;
id = strdup(s);
if (id == NULL)
die("strdup");
s = s2 + 1;
while (isspace(*s))
s++;
if (strncmp(s, "index:", 6)) {
fprintf(stdout, "vobsub_reader: Warning: Incorrect \"id:\" entry "
"on line %d. Ignored.\n", lineno);
continue;
}
s += 6;
while (isspace(*s))
s++;
index = strtol(s, NULL, 10);
if ((index < 0) || (errno != 0)) {
fprintf(stdout, "vobsub_reader: Warning: Incorrect \"id:\" entry "
"on line %d. Ignored.\n", lineno);
continue;
}
} else if (!isvobsubline(chunk))
fprintf(stdout, "vobsub_reader: Warning: Unknown line format on line "
"%d. Ignored.\n", lineno);
else if (vobsub_packetizer == NULL) {
if ((width == -1) || (height == -1)) {
fprintf(stdout, "vobsub_reader: No \"size:\" entry found. "
"File seems to be damaged. Aborting.\n");
exit(1);
}
if (palette == NULL) {
fprintf(stdout, "vobsub_reader: No \"palette:\" entry found. "
"File seems to be damaged. Aborting.\n");
exit(1);
}
if (langidx == -1) {
fprintf(stdout, "vobsub_reader: No \"langidx:\" entry found. "
"File seems to be damaged. Aborting.\n");
exit(1);
}
if ((id == NULL) || (index == -1)) {
fprintf(stdout, "vobsub_reader: No \"id:\" entry found. "
"File seems to be damaged. Aborting.\n");
exit(1);
}
add_vobsub_packetizer(width, height, palette, langidx, id, index);
width = -1;
height = -1;
if (palette != NULL) {
free(palette);
palette = NULL;
}
langidx = -1;
if (id != NULL) {
free(id);
id = NULL;
}
index = -1;
} else {
// timestamp: 00:00:03:440, filepos: 000000000
// 0123456789012345678901234567890123456789012
// 1 2 3 4
chunk[13] = 0;
chunk[16] = 0;
chunk[19] = 0;
chunk[23] = 0;
start = atol(&chunk[11]) * 3600000 + atol(&chunk[14]) * 60000 +
atol(&chunk[17]) * 1000 + atol(&chunk[20]);
filepos = strtoll(&chunk[34], NULL, 16);
if ((last_start != -1) && (last_filepos != -1)) {
if (fseeko(subfile, last_filepos, SEEK_SET) != 0)
fprintf(stderr, "Warning: vobsub_reader: Could not seek to position "
"%lld. Ignoring this entry.\n", last_filepos);
else if (last_filepos == filepos)
fprintf(stderr, "Warning: vobsub_reader: This entry and the last "
"entry start at the same position in the file. Ignored.\n");
else {
s = (char *)malloc(filepos - last_filepos);
if (s == NULL)
die("malloc");
if (fread(s, 1, filepos - last_filepos, subfile) !=
(filepos - last_filepos))
fprintf(stderr, "Warning: vobsub_reader: Could not read entry "
"from the sub file. Ignored.\n");
else
vobsub_packetizer->process(last_start, start - last_start, s,
filepos - last_filepos, 0);
free(s);
}
}
last_start = start;
last_filepos = filepos;
fprintf(stdout, "line %d, start %lld, filepos %lld\n", lineno,
start, filepos);
}
}
if ((last_start != -1) && (last_filepos != -1) &&
(vobsub_packetizer != NULL)) {
if (fseeko(subfile, 0, SEEK_END) != 0) {
fprintf(stderr, "Warning: vobsub_reader: Could not seek to end of "
"the sub file. Ignoring last entry.\n");
vobsub_packetizer->produce_eos_packet();
return 0;
}
filepos = ftello(subfile);
if (fseeko(subfile, last_filepos, SEEK_SET) != 0)
fprintf(stderr, "Warning: vobsub_reader: Could not seek to position "
"%lld. Ignoring this entry.\n", last_filepos);
else if (last_filepos == filepos)
fprintf(stderr, "Warning: vobsub_reader: This entry and the last "
"entry start at the same position in the file. Ignored.\n");
else {
s = (char *)malloc(filepos - last_filepos);
if (s == NULL)
die("malloc");
if (fread(s, 1, filepos - last_filepos, subfile) !=
(filepos - last_filepos))
fprintf(stderr, "Warning: vobsub_reader: Could not read entry "
"from the sub file. Ignored.\n");
else
vobsub_packetizer->process(last_start, start - last_start, s,
filepos - last_filepos, 1);
free(s);
}
}
return 0;
}
int vobsub_reader_c::serial_in_use(int serial) {
// return vobsubpacketizer->serial_in_use(serial);
return 0;
}
ogmmerge_page_t *vobsub_reader_c::get_header_page(int header_type) {
// return vobsubpacketizer->get_header_page(header_type);
return NULL;
}
ogmmerge_page_t *vobsub_reader_c::get_page() {
// return vobsubpacketizer->get_page();
return NULL;
}
int vobsub_reader_c::display_priority() {
return DISPLAYPRIORITY_LOW;
}
void vobsub_reader_c::reset() {
// if (vobsubpacketizer != NULL)
// vobsubpacketizer->reset();
}
static char wchar[] = "-\\|/-\\|/-";
void vobsub_reader_c::display_progress() {
fprintf(stdout, "working... %c\r", wchar[act_wchar]);
act_wchar++;
if (act_wchar == strlen(wchar))
act_wchar = 0;
fflush(stdout);
}
#endif // ENABLE_VOBSUB
|