Merge branch 'refs/heads/t/para_play' into next
[paraslash.git] / check_wav.c
1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file check_wav.c Detect and delete a wav header. */
4
5 #include <regex.h>
6
7 #include "para.h"
8 #include "string.h"
9 #include "list.h"
10 #include "sched.h"
11 #include "buffer_tree.h"
12 #include "error.h"
13 #include "check_wav.h"
14 #include "portable_io.h"
15
16 /** Length of a standard wav header. */
17 #define WAV_HEADER_LEN 44
18
19 /** The possible states of a check_wav instance. */
20 enum check_wav_state {
21 /** Initial state, less than \p WAV_HEADER_LEN bytes available. */
22 CWS_NEED_HEADER,
23 /** Wav hader was detected. */
24 CWS_HAVE_HEADER,
25 /** First part of the stream did not look like a wav header. */
26 CWS_NO_HEADER,
27 };
28
29 struct check_wav_context {
30 enum check_wav_state state;
31 struct btr_node *btrn;
32 size_t min_iqs;
33 /* Command line args. */
34 struct wav_params params;
35 /* Extracted from the wav header.*/
36 unsigned channels;
37 unsigned sample_format;
38 unsigned sample_rate;
39 };
40
41 /**
42 * Set select timeout according to the given context.
43 *
44 * \param s Contains the timeval that should be set.
45 * \param cwc Contains a pointer to the buffer tree node.
46 *
47 * This requests a minimal timeout from the scheduler if btrn of \a cwc is not
48 * idle.
49 */
50 void check_wav_pre_select(struct sched *s, struct check_wav_context *cwc)
51 {
52 int ret = btr_node_status(cwc->btrn, cwc->min_iqs, BTR_NT_INTERNAL);
53 if (ret != 0)
54 sched_min_delay(s);
55 }
56
57 static int check_wav_exec(struct btr_node *btrn, const char *cmd, char **result)
58 {
59 struct check_wav_context *cwc = btr_context(btrn);
60 int val, header_val, given, arg;
61
62 header_val = cwc->channels;
63 arg = cwc->params.channels_arg;
64 given = cwc->params.channels_given;
65 if (!strcmp(cmd, "channels"))
66 goto out;
67
68 header_val = cwc->sample_rate;
69 arg = cwc->params.sample_rate_arg;
70 given = cwc->params.sample_rate_given;
71 if (!strcmp(cmd, "sample_rate"))
72 goto out;
73
74 header_val = cwc->sample_format;
75 arg = cwc->params.sample_format_arg;
76 given = cwc->params.sample_format_given;
77 if (!strcmp(cmd, "sample_format"))
78 goto out;
79
80 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
81 out:
82 if (given)
83 val = arg;
84 else {
85 switch (cwc->state) {
86 case CWS_HAVE_HEADER:
87 val = header_val;
88 break;
89 case CWS_NO_HEADER:
90 /*
91 * No wav header available and no value specified at
92 * the command line. Maybe one of our parent nodes
93 * knows.
94 */
95 if (btr_exec_up(btr_parent(cwc->btrn), cmd, result) >= 0)
96 return 1;
97 /* Use default value */
98 val = arg;
99 break;
100 default:
101 return -E_BTR_NAVAIL;
102 }
103 }
104 *result = make_message("%d", val);
105 return 1;
106 }
107
108 /**
109 * Filter out the wav header, pushdown everything else.
110 *
111 * \param cwc The context of this instance.
112 *
113 * This function looks at the first \p WAV_HEADER_SIZE bytes of the input queue
114 * of the btrn of \a cwc. If they look like a wav header, the function extracts
115 * the information of interest and swallows this part of the stream. Otherwise
116 * it is pushed down to all children. In either case the rest of the input is
117 * pushed down as well.
118 *
119 * Once the first part has been processed this way, the state of the instance
120 * changes from \p CWS_NEED_HEADER to \p CWS_HAVE_HEADER or \p CWS_NO_HEADER.
121 *
122 * \return Standard.
123 */
124 int check_wav_post_select(struct check_wav_context *cwc)
125 {
126 struct btr_node *btrn = cwc->btrn;
127 unsigned char *a;
128 size_t sz;
129 int ret;
130 uint16_t bps; /* bits per sample */
131 const char *sample_formats[] = {SAMPLE_FORMATS};
132
133 if (!btrn)
134 return 0;
135 ret = btr_node_status(btrn, cwc->min_iqs, BTR_NT_INTERNAL);
136 if (ret <= 0)
137 goto out;
138 if (cwc->state != CWS_NEED_HEADER)
139 goto pushdown;
140 btr_merge(btrn, cwc->min_iqs);
141 sz = btr_next_buffer(btrn, (char **)&a);
142 if (sz < cwc->min_iqs) /* file size less than WAV_HEADER_SIZE */
143 goto pushdown;
144 cwc->min_iqs = 0;
145 /*
146 * The default byte ordering assumed for WAVE data files is
147 * little-endian. Files written using the big-endian byte ordering
148 * scheme have the identifier RIFX instead of RIFF.
149 */
150 if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' ||
151 (a[3] != 'F' && a[3] != 'X')) {
152 PARA_NOTICE_LOG("wav header not found\n");
153 cwc->state = CWS_NO_HEADER;
154 goto out;
155 }
156 PARA_INFO_LOG("found wav header\n");
157 cwc->state = CWS_HAVE_HEADER;
158 /* Only set those values which have not already been set. */
159 cwc->channels = a[22];
160 cwc->sample_rate = read_u32(a + 24);
161 bps = read_u16(a + 34);
162 if (bps != 8 && bps != 16) {
163 PARA_WARNING_LOG("%u bps not supported, assuming 16\n",
164 bps);
165 bps = 16;
166 }
167 /*
168 * 8-bit samples are stored as unsigned bytes, ranging from 0
169 * to 255. 16-bit samples are stored as 2's-complement signed
170 * integers, ranging from -32768 to 32767.
171 */
172 if (bps == 8)
173 cwc->sample_format = SF_U8;
174 else
175 cwc->sample_format = (a[3] == 'F')?
176 SF_S16_LE : SF_S16_BE;
177 PARA_NOTICE_LOG("%uHz, %s, %s\n", cwc->sample_rate,
178 cwc->channels == 1? "mono" : "stereo",
179 sample_formats[cwc->sample_format]);
180 btr_consume(btrn, WAV_HEADER_LEN);
181 pushdown:
182 btr_pushdown(btrn);
183 out:
184 if (ret < 0)
185 btr_remove_node(&cwc->btrn);
186 return ret;
187 }
188
189 /**
190 * Allocate and set up a new check_wav instance.
191 *
192 * \param parent This buffer tree node will be the parent of the new node.
193 * \param child The child of the new node.
194 * \param params Default values and options.
195 * \param cw_btrn A pointer to the check wav node is returned here.
196 *
197 * This function also sets up the ->execute handler of the btrn so that all
198 * children of this node can figure out channel count, sample rate, etc.
199 *
200 * \return The (opaque) handle of the newly created check_wav instance. It is
201 * supposed to be passed to \ref check_wav_pre_select() and \ref
202 * check_wav_post_select().
203 *
204 * \sa \ref btr_new_node.
205 */
206 struct check_wav_context *check_wav_init(struct btr_node *parent,
207 struct btr_node *child, struct wav_params *params,
208 struct btr_node **cw_btrn)
209 {
210 struct check_wav_context *cwc = para_calloc(sizeof(*cwc));
211
212 cwc->state = CWS_NEED_HEADER;
213 cwc->min_iqs = WAV_HEADER_LEN;
214 cwc->params = *params;
215 cwc->btrn = btr_new_node(&(struct btr_node_description)
216 EMBRACE(.name = "check_wav", .parent = parent, .child = child,
217 .handler = check_wav_exec, .context = cwc));
218 if (cw_btrn)
219 *cw_btrn = cwc->btrn;
220 return cwc;
221 }
222
223 /**
224 * Dellocate all ressources of a check_wav instance.
225 *
226 * \param cwc Determines the instance to shut down.
227 *
228 * This function may only be called after check_wav_post_select() has returned
229 * negative.
230 */
231 void check_wav_shutdown(struct check_wav_context *cwc)
232 {
233 free(cwc);
234 }