1 /*****************************************************************************
2 * asademux.c: asa demuxer VM
3 *****************************************************************************
4 * Copyright (C) 2007 the VideoLAN team
6 * Originated from asa: portable digital subtitle renderer
8 * Authors: David Lamparter <equinox at diac24 dot net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 ****************************************************************************/
25 /****************************************************************************
26 * Changes from asa version:
28 * - external definition file support dropped
29 * - integer timestamps
30 ****************************************************************************
31 * Please retain Linux kernel CodingStyle for sync.
32 * base commit d8c269b0fae9a8f8904e16e92313da165d664c74
33 ****************************************************************************/
36 #include <vlc_input.h>
37 #include <vlc_demux.h>
44 #define MAXDELTA 4 /**< nr of times kept for delta backref */
45 #define MAXGROUP 24 /**< maximum number of regex match groups */
47 #define xmalloc malloc
48 #define xrealloc realloc
50 #define xstrdup strdup
52 /** state of a running import */
53 struct asa_import_state {
54 demux_t *demux; /**< demuxer for msg* funcs */
55 const char *line; /**< beginning of current line */
56 size_t remain; /**< remaining data at line */
58 char **matches; /**< active matchgroups */
59 unsigned nmatches; /**< number of matchgroups */
60 char *selstr; /**< buffer for currently selected */
61 size_t sellen; /**< strlen of selstr */
62 char *out; /**< output buffer (NULL if empty) */
63 size_t outlen; /**< length of output string w/o \0 */
65 int64_t usecperf, /**< microseconds per frame, active */
66 origusecperf, /**< microseconds per frame, from app */
67 start, /**< start time */
69 int64_t delta[MAXDELTA]; /**< hist of last times for delta */
71 asa_import_callback *cb; /**< commit callback */
72 void *cb_arg; /**< callback argument */
75 #define iargs struct asa_import_state *state, struct asa_import_insn *insn
76 /** asa instruction function.
77 * @param state import state
78 * @param insn instruction to execute
79 * @return status code.\n
81 * -1: restart from beginning
84 typedef int (*asa_import_func)(iargs);
86 static int asai_commit (iargs);
87 static int asai_discard(iargs);
88 static int asai_break (iargs);
89 static int asai_select (iargs);
90 static int asai_sg (iargs);
91 static int asai_sgu (iargs);
92 static int asai_append (iargs);
93 static int asai_fps (iargs);
94 static int asai_show (iargs);
95 static int asai_hide (iargs);
96 static int asai_child (iargs);
99 /** vm functions. KEEP IN SYNC WITH imports.h! */
100 static asa_import_func importfuncs[] = {
113 #define ASAI_MAX (unsigned)(sizeof(importfuncs) / sizeof(importfuncs[0]))
115 struct asa_import_detect *asa_det_first = NULL,
116 **asa_det_last = &asa_det_first;
117 struct asa_import_format *asa_fmt_first = NULL,
118 **asa_fmt_last = &asa_fmt_first;
120 /** asa_imports_crosslink - resolve references in imports file.
121 * updates asa_import_format.prevtgt, asa_import_format.nexttgt,
122 * asa_import_detect.fmt
124 static void asa_imports_crosslink(void)
126 struct asa_import_format *fmt, *fmt2;
127 struct asa_import_detect *det;
129 for (fmt = asa_fmt_first; fmt; fmt = fmt->next) {
130 for (fmt2 = fmt->next; fmt2; fmt2 = fmt2->next)
131 if (!strcmp(fmt->name, fmt2->name)) {
138 for (det = asa_det_first; det; det = det->next) {
140 for (fmt = asa_fmt_first; fmt; fmt = fmt->next)
141 if (!strcmp(det->name, fmt->name)) {
148 /** asa_imports_detect - autodetect subtitle format.
149 * @param data pointer to subtitle byte data
150 * @param datalen byte length of data
151 * @return the detect structure that hit or NULL if detection failed
153 struct asa_import_detect *asa_imports_detect(const void *data, size_t dlen)
155 struct asa_import_detect *det;
156 const char *d = (const char *)data;
161 for (det = asa_det_first; det; det = det->next)
162 if (pcre_exec(det->re.pcre, NULL, d, dlen, 0, 0, v, 64) >= 0)
167 /** asai_run_insns - execute a list of instructions.
168 * @see asa_import_func */
169 static int asai_run_insns(struct asa_import_state *state,
170 struct asa_import_insn *insn)
172 struct asa_import_insn *inow = insn, preload;
173 int rv, repeating = 0;
176 for (; inow; inow = inow->next) {
177 if (repeating && inow->insn != ASAI_CHILD)
179 if (inow->insn >= ASAI_MAX)
181 rv = importfuncs[inow->insn](state, inow);
189 /* ran through everything, let's try another round */
193 /** asai_commit - commit a block to the user.
194 * @see asa_import_func */
195 static int asai_commit(struct asa_import_state *state,
196 struct asa_import_insn *insn)
202 if (state->outlen > 0)
203 rv = state->cb(state->demux, state->cb_arg,
204 state->start, state->end,
205 state->out, state->outlen);
214 /** asai_discard - clear the buffer without committing.
215 * @see asa_import_func */
216 static int asai_discard(struct asa_import_state *state,
217 struct asa_import_insn *insn)
227 /** asai_break - jump out of child.
228 * @see asa_import_func */
229 static int asai_break(struct asa_import_state *state,
230 struct asa_import_insn *insn)
232 return insn->v.break_depth;
235 /** asai_select - choose a match group to be the active one.
236 * @see asa_import_func */
237 static int asai_select (struct asa_import_state *state,
238 struct asa_import_insn *insn)
240 if (insn->v.select < 0
241 || (unsigned)insn->v.select >= state->nmatches) {
242 msg_Err(state->demux, "import script trying to "
243 "reference group %d, maximum is %d",
244 insn->v.select, state->nmatches);
248 xfree(state->selstr);
249 state->selstr = xstrdup(state->matches[insn->v.select]);
250 state->sellen = strlen(state->selstr);
255 static ptrdiff_t asai_process_replace(struct asa_import_state *state,
256 struct asa_import_insn *insn, int *v, int rv)
260 ptrdiff_t newpos, firstold;
263 newstr_size = v[0] * 2;
264 newstr = (char *)xmalloc(newstr_size);
265 memcpy(newstr, state->selstr, v[0]);
268 for (r = insn->v.sg.repl; r; r = r->next) {
269 size_t avail = newstr_size - newpos, need;
272 if (r->group >= rv) {
273 msg_Err(state->demux,
274 "import script trying to replace by "
275 "reference group %d, maximum is %d",
280 need = v[r->group * 2 + 1] - v[r->group * 2];
281 src = state->selstr + v[r->group * 2];
283 need = strlen(r->text);
287 newstr_size += need - avail + 256;
288 newstr = (char *)xrealloc(newstr, newstr_size);
290 memcpy(newstr + newpos, src, need);
294 newstr_size = newpos + state->sellen - v[1];
295 newstr = (char *)xrealloc(newstr, newstr_size + 1);
296 memcpy(newstr + newpos, state->selstr + v[1],
297 state->sellen - v[1] + 1);
298 state->selstr = newstr;
299 state->sellen = newstr_size;
303 /** asai_sg - search and replace.
304 * @see asa_import_func */
305 static int asai_sg(struct asa_import_state *state,
306 struct asa_import_insn *insn)
308 int rv, v[MAXGROUP * 2];
314 while ((unsigned)s < state->sellen &&
315 (rv = pcre_exec(insn->v.sg.regex.pcre, NULL, state->selstr,
316 state->sellen, s, 0, v, MAXGROUP * 2)) >= 0) {
317 oldstr = state->selstr;
318 s = asai_process_replace(state, insn, v, rv);
324 /** asai_chunk_alloc - allocate composite chunk.
325 * @see asa_import_func */
326 static inline char **asai_chunk_alloc(char **old, int *v, int rv)
328 size_t s = rv * sizeof(char *);
330 for (i = 0; i < rv; i++)
331 s += v[i * 2 + 1] - v[i * 2] + 1;
332 return (char **)xrealloc(old, s);
335 /** asai_set_matches - load result from pcre_exec into matches */
336 static void asai_set_matches(struct asa_import_state *state,
337 const char *src, int *v, int rv)
342 state->matches = asai_chunk_alloc(state->matches, v, rv);
343 state->nmatches = rv;
344 dst = (char *)(state->matches + rv);
345 for (i = 0; i < state->nmatches; i++) {
346 size_t len = v[2 * i + 1] - v[2 * i];
347 state->matches[i] = dst;
348 memcpy(dst, src + v[2 * i], len);
353 xfree(state->selstr);
354 state->selstr = xstrdup(state->matches[0]);
355 state->sellen = strlen(state->selstr);
358 /** asai_sgu - replace one time and update matches.
359 * @see asa_import_func */
360 static int asai_sgu(struct asa_import_state *state,
361 struct asa_import_insn *insn)
363 int rv, v[MAXGROUP * 2];
368 if ((rv = pcre_exec(insn->v.sg.regex.pcre, NULL, state->selstr,
369 state->sellen, 0, 0, v, MAXGROUP * 2)) >= 0) {
370 oldstr = state->selstr;
371 asai_process_replace(state, insn, v, rv);
373 asai_set_matches(state, oldstr, v, rv);
379 /** asai_append - append selected string to output buffer.
380 * @see asa_import_func */
381 static int asai_append (struct asa_import_state *state,
382 struct asa_import_insn *insn)
384 state->out = (char *)xrealloc(state->out,
385 state->outlen + state->sellen + 1);
386 memcpy(state->out + state->outlen, state->selstr, state->sellen);
387 state->outlen += state->sellen;
388 state->out[state->outlen] = '\0';
392 /** asai_fps - override fps.
393 * @see asa_import_func */
394 static int asai_fps(struct asa_import_state *state,
395 struct asa_import_insn *insn)
397 if (insn->v.fps_value == 0)
398 state->usecperf = state->origusecperf;
400 state->usecperf = (int64_t)(1000000. / insn->v.fps_value);
404 /** asai_gettime - walk asa_tspec and sum up the time.
405 * @see asa_import_func
406 * @return the calculated time, delta[0] on error
407 * also updates the delta history.
409 static int64_t asai_gettime(struct asa_import_state *state,
410 struct asa_import_insn *insn)
412 struct asa_tspec *tsp;
414 if (insn->v.tspec.delta_select != -1) {
415 if (insn->v.tspec.delta_select < MAXDELTA)
416 t += state->delta[insn->v.tspec.delta_select];
418 msg_Err(state->demux, "imports: tspec "
419 "delta %d exceeds compiled-in maximum of %d",
420 insn->v.tspec.delta_select, MAXDELTA);
422 for (tsp = insn->v.tspec.tsp; tsp; tsp = tsp->next) {
426 if ((unsigned)tsp->group >= state->nmatches) {
427 msg_Err(state->demux, "imports: tspec "
428 "tries to access group %d, but only "
430 tsp->group, state->nmatches);
433 if (!*state->matches[tsp->group])
435 src = strtod(state->matches[tsp->group], &errptr);
437 msg_Warn(state->demux, "imports: invalid tspec '%s'",
438 state->matches[tsp->group]);
439 t += (src * tsp->mult * 1000000)
440 + src * tsp->fps_mult * state->usecperf;
442 memmove(state->delta + 1, state->delta,
443 sizeof(state->delta[0]) * (MAXDELTA - 1));
448 /** asai_show - set start time.
449 * @see asa_import_func */
450 static int asai_show(struct asa_import_state *state,
451 struct asa_import_insn *insn)
453 state->start = asai_gettime(state, insn);
457 /** asai_hide - set end time.
458 * @see asa_import_func */
459 static int asai_hide(struct asa_import_state *state,
460 struct asa_import_insn *insn)
462 state->end = asai_gettime(state, insn);
466 /** asai_child - execute childs if we match.
467 * @see asa_import_func */
468 static int asai_child(struct asa_import_state *state,
469 struct asa_import_insn *insn)
471 int rv, v[MAXGROUP * 2];
472 if ((rv = pcre_exec(insn->v.child.regex.pcre, NULL, state->line,
473 state->remain, 0, 0, v, MAXGROUP * 2)) >= 0) {
474 asai_set_matches(state, state->line, v, rv);
476 state->remain -= v[1];
477 rv = asai_run_insns(state, insn->v.child.insns);
483 int asa_import(demux_t *d, const void *data, size_t dlen,
484 int64_t usecperframe, struct asa_import_detect *det,
485 asa_import_callback *callback, void *arg)
487 struct asa_import_format *fmt = det->fmt;
488 struct asa_import_state state;
491 memset(&state, 0, sizeof(state));
493 state.usecperf = state.origusecperf = usecperframe;
494 state.line = (const char *)data;
499 rv = asai_run_insns(&state, fmt->insns);
501 xfree(state.matches);
503 callback(d, arg, state.start, state.end,
504 state.out, state.outlen);
512 int asa_pcre_compile(asa_pcre *out, const char *str)
517 out->pcre = pcre_compile2(str, 0, &ec, &err, &eo, NULL);
523 #include "asademux_defs.h"
525 void asa_init_import()
527 static int setup = 0;
532 asa_imports_crosslink();