audiod: Fix decoder restart barrier.
[paraslash.git] / filter.c
1 /*
2  * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6 /** \file filter.c the stand-alone filter program */
7
8 #include "para.h"
9
10 #include "filter.cmdline.h"
11 #include "list.h"
12 #include "sched.h"
13 #include "filter.h"
14 #include "string.h"
15 #include "stdin.h"
16 #include "stdout.h"
17 #include "error.h"
18
19 /** init the array of errors for para_filter */
20 INIT_FILTER_ERRLISTS;
21
22 /** the task that reads from stdin */
23 static struct stdin_task stdin_task_struct;
24 /** pointer to the stdin task */
25 static struct stdin_task *sit = &stdin_task_struct;
26
27 /** the task that filters the data */
28 static struct filter_chain filter_chain_struct;
29 /** pointer to the filter chain */
30 static struct filter_chain *fc = &filter_chain_struct;
31
32 /** the task that writes converted data to stdout */
33 static struct stdout_task stdout_task_struct;
34 /** pointer to the stdout task */
35 static struct stdout_task *sot = &stdout_task_struct;
36
37 /** gengetopt struct that holds the command line args */
38 static struct filter_args_info conf;
39
40 INIT_STDERR_LOGGING(conf.loglevel_arg);
41
42 static void open_filters(void)
43 {
44         int i;
45         struct filter_node *fn;
46
47         FOR_EACH_FILTER_NODE(fn, fc, i) {
48                 struct filter *f = filters + fn->filter_num;
49                 f->open(fn);
50                 PARA_INFO_LOG("opened %s filter\n", f->name);
51                 fc->outbuf = fn->buf;
52                 fc->out_loaded = &fn->loaded;
53         }
54 }
55
56 static int init_filter_chain(void)
57 {
58         int i, ret;
59         struct filter_node *fn;
60
61         if (!conf.filter_given)
62                 return -E_NO_FILTERS;
63         fc->num_filters = conf.filter_given;
64         fc->filter_nodes = para_malloc(fc->num_filters * sizeof(struct filter_node));
65         fc->inbuf = sit->buf;
66         fc->in_loaded = &sit->loaded;
67         fc->input_error = &sit->task.error;
68         fc->task.error = 0;
69         fc->output_error = &sot->task.error;
70         fc->task.pre_select = filter_pre_select;
71         sprintf(fc->task.status, "filter chain");
72
73         FOR_EACH_FILTER_NODE(fn, fc, i) {
74                 char *fa = conf.filter_arg[i];
75                 fn = fc->filter_nodes + i;
76                 ret = check_filter_arg(fa, &fn->conf);
77                 if (ret < 0)
78                         return ret;
79                 fn->filter_num = ret;
80                 fn->fc = fc;
81                 INIT_LIST_HEAD(&fn->callbacks);
82                 PARA_DEBUG_LOG("filter #%d: %s\n", i, filters[fn->filter_num].name);
83         }
84         open_filters();
85         return 1;
86 }
87
88 static int parse_config(int argc, char *argv[])
89 {
90         static char *cf; /* config file */
91         struct stat statbuf;
92         int i;
93
94         if (filter_cmdline_parser(argc, argv, &conf))
95                 return -E_FILTER_SYNTAX;
96         HANDLE_VERSION_FLAG("filter", conf);
97         if (!cf) {
98                 char *home = para_homedir();
99                 cf = make_message("%s/.paraslash/filter.conf", home);
100                 free(home);
101         }
102         if (!stat(cf, &statbuf)) {
103                 struct filter_cmdline_parser_params params = {
104                         .override = 0,
105                         .initialize = 0,
106                         .check_required = 0,
107                         .check_ambiguity = 0
108                 };
109                 if (filter_cmdline_parser_config_file(cf, &conf, &params))
110                         return -E_FILTER_SYNTAX;
111         }
112         if (!conf.list_filters_given)
113                 return 1;
114         printf("available filters: ");
115         for (i = 0; filters[i].name; i++)
116                 printf("%s%s%s", i? " " : "", filters[i].name,
117                         filters[i].parse_config? "*": "");
118         printf("\nFilters marked with \"*\" have further command line options. Try\n"
119                 "\tpara_filter -f '<filtername> -h'\nfor more information.\n");
120         exit(EXIT_SUCCESS);
121 }
122
123 /**
124  * para_filter's main function.
125  *
126  * para_filter reads data from stdin, converts it by using a chain
127  * of filters (specified on the command line) and writes the resulting
128  * data to stdout.
129  *
130  * \param argc number of command line options
131  * \param argv vector of arguments
132  *
133  * \return \a EXIT_SUCCESS on success, EXIT_FAILURE on errors.
134  */
135 int main(int argc, char *argv[])
136 {
137         int ret;
138         struct sched s;
139
140         stdin_set_defaults(sit);
141         sit->buf = para_malloc(sit->bufsize),
142
143         filter_init(filters);
144         ret = parse_config(argc, argv);
145         if (ret < 0)
146                 goto out;
147         ret = init_filter_chain();
148         if (ret < 0)
149                 goto out;
150
151         stdout_set_defaults(sot);
152         sot->buf = fc->outbuf;
153         sot->loaded = fc->out_loaded;
154         sot->input_error = &fc->task.error;
155
156         register_task(&sit->task);
157         register_task(&fc->task);
158         register_task(&sot->task);
159         s.default_timeout.tv_sec = 1;
160         s.default_timeout.tv_usec = 0;
161         ret = schedule(&s);
162 out:
163         free(sit->buf);
164         close_filters(fc);
165         if (ret < 0)
166                 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
167         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
168 }