2 * Copyright (C) 2002-2006 Jean-Marc Valin
3 * Copyright (C) 2010-2013 Andre Noll <maan@systemlinux.org>
5 * Licensed under the GPL v2. For licencing details see COPYING.
9 * \file spx_common.c Functions used by the speex decoder and the speex audio
13 /* This file is based on speexdec.c, by Jean-Marc Valin, see below. */
15 /* Copyright (C) 2002-2006 Jean-Marc Valin
18 Redistribution and use in source and binary forms, with or without
19 modification, are permitted provided that the following conditions
22 - Redistributions of source code must retain the above copyright
23 notice, this list of conditions and the following disclaimer.
25 - Redistributions in binary form must reproduce the above copyright
26 notice, this list of conditions and the following disclaimer in the
27 documentation and/or other materials provided with the distribution.
29 - Neither the name of the Xiph.org Foundation nor the names of its
30 contributors may be used to endorse or promote products derived from
31 this software without specific prior written permission.
33 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
36 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
37 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
38 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
39 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
40 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
41 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
42 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
43 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 #include <speex/speex_header.h>
47 #include <speex/speex_stereo.h>
48 #include <speex/speex_callbacks.h>
55 * Wrapper for speex_decoder_ctl().
57 * \param state Decoder state.
58 * \param request ioctl-type request.
59 * \param ptr Value-result pointer.
63 static int spx_ctl(void *state, int request, void *ptr)
65 int ret = speex_decoder_ctl(state, request, ptr);
67 if (ret == 0) /* success */
70 return -E_SPX_CTL_BAD_RQ;
71 return -E_SPX_CTL_INVAL;
75 * Obtain information about a speex file from an ogg packet.
77 * \param packet Start of the ogg packet.
78 * \param bytes Length of the ogg packet.
79 * \param shi Result pointer.
83 int spx_process_header(unsigned char *packet, long bytes,
84 struct spx_header_info *shi)
87 spx_int32_t enh_enabled = 1;
88 SpeexHeader *h = speex_packet_to_header((char *)packet, bytes);
92 ret = -E_SPX_HEADER_MODE;
93 if (h->mode >= SPEEX_NB_MODES || h->mode < 0)
95 shi->mode = speex_lib_get_mode(h->mode);
98 if (h->speex_version_id > 1)
100 if (shi->mode->bitstream_version < h->mode_bitstream_version)
102 if (shi->mode->bitstream_version > h->mode_bitstream_version)
105 ret = -E_SPX_DECODER_INIT;
106 shi->state = speex_decoder_init(shi->mode);
110 ret = spx_ctl(shi->state, SPEEX_SET_ENH, &enh_enabled);
113 ret = spx_ctl(shi->state, SPEEX_GET_FRAME_SIZE, &shi->frame_size);
116 shi->sample_rate = h->rate;
117 ret = spx_ctl(shi->state, SPEEX_SET_SAMPLING_RATE, &shi->sample_rate);
120 shi->nframes = h->frames_per_packet;
121 shi->channels = h->nb_channels;
122 if (shi->channels != 1) {
123 SpeexCallback callback = {
124 .callback_id = SPEEX_INBAND_STEREO,
125 .func = speex_std_stereo_request_handler,
126 .data = &shi->stereo,
128 shi->stereo = (SpeexStereoState)SPEEX_STEREO_STATE_INIT;
129 ret = spx_ctl(shi->state, SPEEX_SET_HANDLER, &callback);
134 ret = spx_ctl(shi->state, SPEEX_GET_BITRATE, &shi->bitrate);
137 PARA_NOTICE_LOG("%d Hz, %s, %s, %s, %d bits/s\n",
138 shi->sample_rate, shi->mode->modeName,
139 shi->channels == 1? "mono" : "stereo",
140 h->vbr? "vbr" : "cbr",
143 shi->extra_headers = h->extra_headers;