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 ****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_input.h>
37 #include <vlc_demux.h>
38 #include <vlc_memory.h>
46 #define MAXDELTA 4 /**< nr of times kept for delta backref */
47 #define MAXGROUP 24 /**< maximum number of regex match groups */
49 #define xmalloc malloc
50 #define xrealloc realloc_or_free
52 #define xstrdup strdup
54 /** state of a running import */
55 struct asa_import_state {
56 demux_t *demux; /**< demuxer for msg* funcs */
57 const char *line; /**< beginning of current line */
58 size_t remain; /**< remaining data at line */
60 char **matches; /**< active matchgroups */
61 unsigned nmatches; /**< number of matchgroups */
62 char *selstr; /**< buffer for currently selected */
63 size_t sellen; /**< strlen of selstr */
64 char *out; /**< output buffer (NULL if empty) */
65 size_t outlen; /**< length of output string w/o \0 */
67 int64_t usecperf, /**< microseconds per frame, active */
68 origusecperf, /**< microseconds per frame, from app */
69 start, /**< start time */
71 int64_t delta[MAXDELTA]; /**< hist of last times for delta */
73 asa_import_callback *cb; /**< commit callback */
74 void *cb_arg; /**< callback argument */
77 #define iargs struct asa_import_state *state, struct asa_import_insn *insn
78 /** asa instruction function.
79 * @param state import state
80 * @param insn instruction to execute
81 * @return status code.\n
83 * -1: restart from beginning
86 typedef int (*asa_import_func)(iargs);
88 static int asai_commit (iargs);
89 static int asai_discard(iargs);
90 static int asai_break (iargs);
91 static int asai_select (iargs);
92 static int asai_sg (iargs);
93 static int asai_sgu (iargs);
94 static int asai_append (iargs);
95 static int asai_fps (iargs);
96 static int asai_show (iargs);
97 static int asai_hide (iargs);
98 static int asai_child (iargs);
101 /** vm functions. KEEP IN SYNC WITH imports.h! */
102 static const asa_import_func importfuncs[] = {
115 #define ASAI_MAX (unsigned)(sizeof(importfuncs) / sizeof(importfuncs[0]))
117 struct asa_import_detect *asa_det_first = NULL,
118 **asa_det_last = &asa_det_first;
119 struct asa_import_format *asa_fmt_first = NULL,
120 **asa_fmt_last = &asa_fmt_first;
122 /** asa_imports_crosslink - resolve references in imports file.
123 * updates asa_import_format.prevtgt, asa_import_format.nexttgt,
124 * asa_import_detect.fmt
126 static void asa_imports_crosslink(void)
128 struct asa_import_format *fmt, *fmt2;
129 struct asa_import_detect *det;
131 for (fmt = asa_fmt_first; fmt; fmt = fmt->next) {
132 for (fmt2 = fmt->next; fmt2; fmt2 = fmt2->next)
133 if (!strcmp(fmt->name, fmt2->name)) {
140 for (det = asa_det_first; det; det = det->next) {
142 for (fmt = asa_fmt_first; fmt; fmt = fmt->next)
143 if (!strcmp(det->name, fmt->name)) {
150 /** asa_imports_detect - autodetect subtitle format.
151 * @param data pointer to subtitle byte data
152 * @param datalen byte length of data
153 * @return the detect structure that hit or NULL if detection failed
155 struct asa_import_detect *asa_imports_detect(const void *data, size_t dlen)
157 struct asa_import_detect *det;
158 const char *d = (const char *)data;
163 for (det = asa_det_first; det; det = det->next)
164 if (pcre_exec(det->re.pcre, NULL, d, dlen, 0, 0, v, 64) >= 0)
169 /** asai_run_insns - execute a list of instructions.
170 * @see asa_import_func */
171 static int asai_run_insns(struct asa_import_state *state,
172 struct asa_import_insn *insn)
174 struct asa_import_insn *inow = insn, preload;
175 int rv, repeating = 0;
178 for (; inow; inow = inow->next) {
179 if (repeating && inow->insn != ASAI_CHILD)
181 if (inow->insn >= ASAI_MAX)
183 rv = importfuncs[inow->insn](state, inow);
191 /* ran through everything, let's try another round */
195 /** asai_commit - commit a block to the user.
196 * @see asa_import_func */
197 static int asai_commit(struct asa_import_state *state,
198 struct asa_import_insn *insn)
204 if (state->outlen > 0)
205 rv = state->cb(state->demux, state->cb_arg,
206 state->start, state->end,
207 state->out, state->outlen);
216 /** asai_discard - clear the buffer without committing.
217 * @see asa_import_func */
218 static int asai_discard(struct asa_import_state *state,
219 struct asa_import_insn *insn)
229 /** asai_break - jump out of child.
230 * @see asa_import_func */
231 static int asai_break(struct asa_import_state *state,
232 struct asa_import_insn *insn)
234 return insn->v.break_depth;
237 /** asai_select - choose a match group to be the active one.
238 * @see asa_import_func */
239 static int asai_select (struct asa_import_state *state,
240 struct asa_import_insn *insn)
242 if (insn->v.select < 0
243 || (unsigned)insn->v.select >= state->nmatches) {
244 msg_Err(state->demux, "import script trying to "
245 "reference group %d, maximum is %d",
246 insn->v.select, state->nmatches);
250 xfree(state->selstr);
251 state->selstr = xstrdup(state->matches[insn->v.select]);
252 assert(state->selstr);
253 state->sellen = strlen(state->selstr);
258 static ptrdiff_t asai_process_replace(struct asa_import_state *state,
259 struct asa_import_insn *insn, int *v, int rv)
263 ptrdiff_t newpos, firstold;
266 newstr_size = v[0] * 2;
267 newstr = (char *)xmalloc(newstr_size);
269 memcpy(newstr, state->selstr, v[0]);
272 for (r = insn->v.sg.repl; r; r = r->next) {
273 size_t avail = newstr_size - newpos, need;
276 if (r->group >= rv) {
277 msg_Err(state->demux,
278 "import script trying to replace by "
279 "reference group %d, maximum is %d",
284 need = v[r->group * 2 + 1] - v[r->group * 2];
285 src = state->selstr + v[r->group * 2];
287 need = strlen(r->text);
291 newstr_size += need - avail + 256;
292 newstr = (char *)xrealloc(newstr, newstr_size);
295 memcpy(newstr + newpos, src, need);
299 newstr_size = newpos + state->sellen - v[1];
300 newstr = (char *)xrealloc(newstr, newstr_size + 1);
302 memcpy(newstr + newpos, state->selstr + v[1],
303 state->sellen - v[1] + 1);
304 state->selstr = newstr;
305 state->sellen = newstr_size;
309 /** asai_sg - search and replace.
310 * @see asa_import_func */
311 static int asai_sg(struct asa_import_state *state,
312 struct asa_import_insn *insn)
314 int rv, v[MAXGROUP * 2];
320 while ((unsigned)s < state->sellen &&
321 (rv = pcre_exec(insn->v.sg.regex.pcre, NULL, state->selstr,
322 state->sellen, s, 0, v, MAXGROUP * 2)) >= 0) {
323 oldstr = state->selstr;
324 s = asai_process_replace(state, insn, v, rv);
330 /** asai_chunk_alloc - allocate composite chunk.
331 * @see asa_import_func */
332 static inline char **asai_chunk_alloc(char **old, int *v, int rv)
334 size_t s = rv * sizeof(char *);
336 for (i = 0; i < rv; i++)
337 s += v[i * 2 + 1] - v[i * 2] + 1;
338 return (char **)xrealloc(old, s);
341 /** asai_set_matches - load result from pcre_exec into matches */
342 static void asai_set_matches(struct asa_import_state *state,
343 const char *src, int *v, int rv)
348 state->matches = asai_chunk_alloc(state->matches, v, rv);
349 state->nmatches = rv;
350 dst = (char *)(state->matches + rv);
351 for (i = 0; i < state->nmatches; i++) {
352 size_t len = v[2 * i + 1] - v[2 * i];
353 state->matches[i] = dst;
354 memcpy(dst, src + v[2 * i], len);
359 xfree(state->selstr);
360 state->selstr = xstrdup(state->matches[0]);
361 assert(state->selstr);
362 state->sellen = strlen(state->selstr);
365 /** asai_sgu - replace one time and update matches.
366 * @see asa_import_func */
367 static int asai_sgu(struct asa_import_state *state,
368 struct asa_import_insn *insn)
370 int rv, v[MAXGROUP * 2];
375 if ((rv = pcre_exec(insn->v.sg.regex.pcre, NULL, state->selstr,
376 state->sellen, 0, 0, v, MAXGROUP * 2)) >= 0) {
377 oldstr = state->selstr;
378 asai_process_replace(state, insn, v, rv);
380 asai_set_matches(state, oldstr, v, rv);
386 /** asai_append - append selected string to output buffer.
387 * @see asa_import_func */
388 static int asai_append (struct asa_import_state *state,
389 struct asa_import_insn *insn)
391 state->out = (char *)xrealloc(state->out,
392 state->outlen + state->sellen + 1);
394 memcpy(state->out + state->outlen, state->selstr, state->sellen);
395 state->outlen += state->sellen;
396 state->out[state->outlen] = '\0';
400 /** asai_fps - override fps.
401 * @see asa_import_func */
402 static int asai_fps(struct asa_import_state *state,
403 struct asa_import_insn *insn)
405 if (insn->v.fps_value == 0)
406 state->usecperf = state->origusecperf;
408 state->usecperf = (int64_t)(1000000. / insn->v.fps_value);
412 /** asai_gettime - walk asa_tspec and sum up the time.
413 * @see asa_import_func
414 * @return the calculated time, delta[0] on error
415 * also updates the delta history.
417 static int64_t asai_gettime(struct asa_import_state *state,
418 struct asa_import_insn *insn)
420 struct asa_tspec *tsp;
422 if (insn->v.tspec.delta_select != -1) {
423 if (insn->v.tspec.delta_select < MAXDELTA)
424 t += state->delta[insn->v.tspec.delta_select];
426 msg_Err(state->demux, "imports: tspec "
427 "delta %d exceeds compiled-in maximum of %d",
428 insn->v.tspec.delta_select, MAXDELTA);
430 for (tsp = insn->v.tspec.tsp; tsp; tsp = tsp->next) {
434 if ((unsigned)tsp->group >= state->nmatches) {
435 msg_Err(state->demux, "imports: tspec "
436 "tries to access group %d, but only "
438 tsp->group, state->nmatches);
441 if (!*state->matches[tsp->group])
443 src = strtod(state->matches[tsp->group], &errptr);
445 msg_Warn(state->demux, "imports: invalid tspec '%s'",
446 state->matches[tsp->group]);
447 t += (src * tsp->mult * 1000000)
448 + src * tsp->fps_mult * state->usecperf;
450 memmove(state->delta + 1, state->delta,
451 sizeof(state->delta[0]) * (MAXDELTA - 1));
456 /** asai_show - set start time.
457 * @see asa_import_func */
458 static int asai_show(struct asa_import_state *state,
459 struct asa_import_insn *insn)
461 state->start = asai_gettime(state, insn);
465 /** asai_hide - set end time.
466 * @see asa_import_func */
467 static int asai_hide(struct asa_import_state *state,
468 struct asa_import_insn *insn)
470 state->end = asai_gettime(state, insn);
474 /** asai_child - execute childs if we match.
475 * @see asa_import_func */
476 static int asai_child(struct asa_import_state *state,
477 struct asa_import_insn *insn)
479 int rv, v[MAXGROUP * 2];
480 if ((rv = pcre_exec(insn->v.child.regex.pcre, NULL, state->line,
481 state->remain, 0, 0, v, MAXGROUP * 2)) >= 0) {
482 asai_set_matches(state, state->line, v, rv);
484 state->remain -= v[1];
485 rv = asai_run_insns(state, insn->v.child.insns);
491 int asa_import(demux_t *d, const void *data, size_t dlen,
492 int64_t usecperframe, struct asa_import_detect *det,
493 asa_import_callback *callback, void *arg)
495 struct asa_import_format *fmt = det->fmt;
496 struct asa_import_state state;
499 memset(&state, 0, sizeof(state));
501 state.usecperf = state.origusecperf = usecperframe;
502 state.line = (const char *)data;
507 rv = asai_run_insns(&state, fmt->insns);
509 xfree(state.matches);
511 callback(d, arg, state.start, state.end,
512 state.out, state.outlen);
520 int asa_pcre_compile(asa_pcre *out, const char *str)
525 out->pcre = pcre_compile2(str, 0, &ec, &err, &eo, NULL);
531 #include "asademux_defs.h"
533 void asa_init_import()
535 static int setup = 0;
540 asa_imports_crosslink();