872a7d0a0854d27061777b99c15bc98d6884a80f
2 * Copyright (C) 2005-2013 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file check_wav.c Detect and delete a wav header. */
15 #include "buffer_tree.h"
17 #include "check_wav.h"
19 /** Length of a standard wav header. */
20 #define WAV_HEADER_LEN 44
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. */
26 /** Wav hader was detected. */
28 /** First part of the stream did not look like a wav header. */
32 struct check_wav_context
{
33 enum check_wav_state state
;
34 struct btr_node
*btrn
;
36 /* Command line args. */
37 struct wav_params params
;
38 /* Extracted from the wav header.*/
40 unsigned sample_format
;
45 * Set select timeout according to the given context.
47 * \param s Contains the timeval that should be set.
48 * \param cwc Contains a pointer to the buffer tree node.
50 * This requests a minimal timeout from the scheduler if btrn of \a cwc is not
53 void check_wav_pre_select(struct sched
*s
, struct check_wav_context
*cwc
)
55 int ret
= btr_node_status(cwc
->btrn
, cwc
->min_iqs
, BTR_NT_INTERNAL
);
60 static int check_wav_exec(struct btr_node
*btrn
, const char *cmd
, char **result
)
62 struct check_wav_context
*cwc
= btr_context(btrn
);
63 int val
, header_val
, given
, arg
;
65 header_val
= cwc
->channels
;
66 arg
= cwc
->params
.channels_arg
;
67 given
= cwc
->params
.channels_given
;
68 if (!strcmp(cmd
, "channels"))
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"))
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"))
83 return -ERRNO_TO_PARA_ERROR(ENOTSUP
);
94 * No wav header available and no value specified at
95 * the command line. Maybe one of our parent nodes
98 if (btr_exec_up(btr_parent(cwc
->btrn
), cmd
, result
) >= 0)
100 /* Use default value */
104 return -E_BTR_NAVAIL
;
107 *result
= make_message("%d", val
);
112 * Filter out the wav header, pushdown everything else.
114 * \param cwc The context of this instance.
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 of \a btrn. In either case the rest of the
120 * input is pushed down as well.
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.
127 int check_wav_post_select(struct check_wav_context
*cwc
)
129 struct btr_node
*btrn
= cwc
->btrn
;
133 uint16_t bps
; /* bits per sample */
134 const char *sample_formats
[] = {SAMPLE_FORMATS
};
138 ret
= btr_node_status(btrn
, cwc
->min_iqs
, BTR_NT_INTERNAL
);
141 if (cwc
->state
!= CWS_NEED_HEADER
)
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 */
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.
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
;
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",
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.
176 cwc
->sample_format
= SF_U8
;
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
);
188 btr_remove_node(&cwc
->btrn
);
193 * Allocate and set up a new check_wav instance.
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.
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.
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().
207 * \sa \ref btr_new_node.
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
)
213 struct check_wav_context
*cwc
= para_calloc(sizeof(*cwc
));
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
));
222 *cw_btrn
= cwc
->btrn
;
227 * Dellocate all ressources of a check_wav instance.
229 * \param cwc Determines the instance to shut down.
231 * This function may only be called after check_wav_post_select() has returned
234 void check_wav_shutdown(struct check_wav_context
*cwc
)