aacdec: Improve and silence error message.
[paraslash.git] / filter_common.c
1 /*
2  * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file filter_common.c Common helper functions for filter input/output. */
8
9 #include <regex.h>
10 #include <sys/types.h>
11
12 #include "para.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "fd.h"
16 #include "ggo.h"
17 #include "buffer_tree.h"
18 #include "filter.h"
19 #include "error.h"
20 #include "string.h"
21
22 /** Iterate over the array of supported filters. */
23 #define FOR_EACH_SUPPORTED_FILTER(j)  for (j = 0; j < NUM_SUPPORTED_FILTERS; j++)
24
25 /** The array of supported filters. */
26 static struct filter filters[NUM_SUPPORTED_FILTERS] = {FILTER_ARRAY};
27
28 /**
29  * Obtain a reference to a filter structure.
30  *
31  * \param filter_num Between zero and NUM_SUPPORTED_FILTERS, inclusively.
32  *
33  * \return Pointer to the filter identified by the given filter number.
34  *
35  * It is a fatal error if the given number is out of range. In this case
36  * the function aborts.
37  */
38 const struct filter *filter_get(int filter_num)
39 {
40         assert(filter_num >= 0);
41         assert(filter_num < NUM_SUPPORTED_FILTERS);
42         return filters + filter_num;
43 }
44
45 /**
46  * Call the init function of each supported filter.
47  * \sa filter::init
48  */
49 void filter_init(void)
50 {
51         int i;
52
53         FOR_EACH_SUPPORTED_FILTER(i)
54                 filter_get(i)->init((struct filter *)filter_get(i));
55 }
56
57 /*
58  * If the filter has a command line parser and options is not NULL, run it.
59  * Returns filter_num on success, negative on errors
60  */
61 static int parse_filter_args(int filter_num, const char *options, void **conf)
62 {
63         const struct filter *f = filter_get(filter_num);
64         int ret, argc;
65         char **argv;
66
67         if (!f->parse_config)
68                 return strlen(options)? -E_BAD_FILTER_OPTIONS : filter_num;
69         argc = create_shifted_argv(options, " \t", &argv);
70         if (argc < 0)
71                 return -E_BAD_FILTER_OPTIONS;
72         argv[0] = para_strdup(f->name);
73         ret = f->parse_config(argc, argv, conf);
74         free_argv(argv);
75         return ret < 0? ret : filter_num;
76 }
77
78 /**
79  * Check the filter command line options.
80  *
81  * \param fa The filter argument.
82  * \param conf Points to the filter configuration upon successful return.
83  *
84  * Check if the given filter argument starts with the name of a supported
85  * filter, optionally followed by options for this filter. If yes, call the
86  * command line parser of that filter.
87  *
88  * \return On success, the number of the filter is returned and \a conf
89  * is initialized to point to the filter configuration determined by \a fa.
90  * On errors, a negative value is returned.
91  *
92  * Note: If \a fa specifies a filter that has no command line parser success is
93  * returned, and \a conf is initialized to \p NULL.
94  *
95  * \sa filter::parse_config
96  */
97 int check_filter_arg(const char *fa, void **conf)
98 {
99         int j;
100
101         *conf = NULL;
102 //      PARA_DEBUG_LOG("arg: %s\n", fa);
103         FOR_EACH_SUPPORTED_FILTER(j) {
104                 const char *name = filter_get(j)->name;
105                 size_t len = strlen(name);
106                 char c;
107                 if (strlen(fa) < len)
108                         continue;
109                 if (strncmp(name, fa, len))
110                         continue;
111                 c = fa[len];
112                 if (c && c != ' ')
113                         continue;
114                 if (c && !filter_get(j)->parse_config)
115                         return -E_BAD_FILTER_OPTIONS;
116                 return parse_filter_args(j, c? fa + len + 1 :
117                         fa + strlen(fa), conf);
118         }
119         return -E_UNSUPPORTED_FILTER;
120 }
121
122 /**
123  * Print help text of each filter to stdout.
124  *
125  * \param flags Passed to \ref ggo_print_help().
126  */
127 void print_filter_helps(unsigned flags)
128 {
129         int i, num = 0;
130
131         printf_or_die("\nAvailable filters: ");
132         FOR_EACH_SUPPORTED_FILTER(i) {
133                 if (num > 50) {
134                         printf_or_die("\n                  ");
135                         num = 0;
136                 }
137                 num += printf_or_die("%s%s", i? " " : "", filter_get(i)->name);
138         }
139         printf_or_die("\n");
140
141         FOR_EACH_SUPPORTED_FILTER(i) {
142                 struct filter *f = (struct filter *)filter_get(i);
143
144                 if (!f->help.short_help)
145                         continue;
146                 printf_or_die("\nOptions for %s (%s):", f->name,
147                         f->help.purpose);
148                 ggo_print_help(&f->help, flags);
149         }
150 }
151
152 /**
153  * Set select timeout of the scheduler.
154  *
155  * \param s The scheduler.
156  * \param context Pointer to the filter node (task context).
157  *
158  * This looks at the status of the btr node of the filter. If data is available
159  * in the input queue of the filter, or if an error occurred, a minimal timeout
160  * for the next select call is requested from the scheduler. Otherwise the
161  * scheduler timeout is left unchanged.
162  */
163 void generic_filter_pre_select(struct sched *s, void *context)
164 {
165         struct filter_node *fn = context;
166
167         if (btr_node_status(fn->btrn, fn->min_iqs, BTR_NT_INTERNAL) != 0)
168                 sched_min_delay(s);
169 }
170
171 #ifdef WORDS_BIGENDIAN
172 #define DECODER_SAMPLE_FORMAT SF_S16_BE
173 #else
174 #define DECODER_SAMPLE_FORMAT SF_S16_LE
175 #endif
176
177 /**
178  * Execute a btr command for a decoder.
179  *
180  * The buffer tree nodes of the writers ask the parent nodes about sample_rate,
181  * channels count and sample format. This function is called by all decoders to
182  * answer these queries.
183  *
184  * \param cmd The command to be executed by the child node.
185  * \param sample_rate Known to the decoder.
186  * \param channels Known to the decoder.
187  * \param result Ascii representation on the answer is stored here.
188  *
189  * \return Standard.
190  */
191 int decoder_execute(const char *cmd, unsigned sample_rate, unsigned channels,
192                 char **result)
193 {
194         if (!strcmp(cmd, "sample_rate")) {
195                 if (sample_rate == 0)
196                         return -E_BTR_NAVAIL;
197                 *result = make_message("%u", sample_rate);
198                 return 1;
199         }
200         if (!strcmp(cmd, "channels")) {
201                 if (channels == 0)
202                         return -E_BTR_NAVAIL;
203                 *result = make_message("%u", channels);
204                 return 1;
205         }
206         if (!strcmp(cmd, "sample_format")) {
207                 *result = make_message("%d", DECODER_SAMPLE_FORMAT);
208                 return 1;
209         }
210         return -ERRNO_TO_PARA_ERROR(ENOTSUP);
211 }