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