audiod: Fix writer setup info message.
[paraslash.git] / spx_common.c
1 /*
2  * Copyright (C) 2002-2006 Jean-Marc Valin
3  * Copyright (C) 2010-2011 Andre Noll <maan@systemlinux.org>
4  *
5  * Licensed under the GPL v2. For licencing details see COPYING.
6  */
7
8 /**
9  * \file spx_common.c Functions used by the speex decoder and the speex audio
10  * format handler.
11  */
12
13 /* This file is based on speexdec.c, by Jean-Marc Valin, see below. */
14
15 /* Copyright (C) 2002-2006 Jean-Marc Valin
16    File: speexdec.c
17
18    Redistribution and use in source and binary forms, with or without
19    modification, are permitted provided that the following conditions
20    are met:
21    
22    - Redistributions of source code must retain the above copyright
23    notice, this list of conditions and the following disclaimer.
24    
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.
28    
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.
32    
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.
44 */
45 #include <regex.h>
46 #include <speex/speex_header.h>
47 #include <speex/speex_stereo.h>
48 #include <speex/speex_callbacks.h>
49
50 #include "para.h"
51 #include "error.h"
52 #include "spx.h"
53
54 /**
55  * Wrapper for speex_decoder_ctl().
56  *
57  * \param state Decoder state.
58  * \param request ioctl-type request.
59  * \param ptr Value-result pointer.
60  *
61  * \return Standard.
62  */
63 int spx_ctl(void *state, int request, void *ptr)
64 {
65         int ret = speex_decoder_ctl(state, request, ptr);
66
67         if (ret == 0) /* success */
68                 return 1;
69         if (ret == -1)
70                 return -E_SPX_CTL_BAD_RQ;
71         return -E_SPX_CTL_INVAL;
72 }
73
74 /**
75  * Obtain information about a speex file from an ogg packet.
76  *
77  * \param packet Start of the ogg packet.
78  * \param bytes Length of the ogg packet.
79  * \param shi Result pointer.
80  *
81  * \return Standard.
82  */
83 int spx_process_header(unsigned char *packet, long bytes,
84                 struct spx_header_info *shi)
85 {
86         int ret;
87         spx_int32_t enh_enabled = 1;
88         SpeexHeader *h = speex_packet_to_header((char *)packet, bytes);
89
90         if (!h)
91                 return -E_SPX_HEADER;
92         ret = -E_SPX_HEADER_MODE;
93         if (h->mode >= SPEEX_NB_MODES || h->mode < 0)
94                 goto out;
95         shi->mode = speex_lib_get_mode(h->mode);
96
97         ret = -E_SPX_VERSION;
98         if (h->speex_version_id > 1)
99                 goto out;
100         if (shi->mode->bitstream_version < h->mode_bitstream_version)
101                 goto out;
102         if (shi->mode->bitstream_version > h->mode_bitstream_version)
103                 goto out;
104
105         ret = -E_SPX_DECODER_INIT;
106         shi->state = speex_decoder_init(shi->mode);
107         if (!shi->state)
108                 goto out;
109
110         ret = spx_ctl(shi->state, SPEEX_SET_ENH, &enh_enabled);
111         if (ret < 0)
112                 goto out;
113         ret = spx_ctl(shi->state, SPEEX_GET_FRAME_SIZE, &shi->frame_size);
114         if (ret < 0)
115                 goto out;
116         shi->sample_rate = h->rate;
117         ret = spx_ctl(shi->state, SPEEX_SET_SAMPLING_RATE, &shi->sample_rate);
118         if (ret < 0)
119                 goto out;
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,
127                 };
128                 shi->stereo = (SpeexStereoState)SPEEX_STEREO_STATE_INIT;
129                 ret = spx_ctl(shi->state, SPEEX_SET_HANDLER, &callback);
130                 if (ret < 0)
131                         goto out;
132                 shi->channels = 2;
133         }
134         ret = spx_ctl(shi->state, SPEEX_GET_BITRATE, &shi->bitrate);
135         if (ret < 0)
136                 goto out;
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",
141                 shi->bitrate
142         );
143         shi->extra_headers = h->extra_headers;
144         ret = 1;
145 out:
146         free(h);
147         return ret;
148 }