FS#8961 - Anti-Aliased Fonts.
[kugel-rb/myfork.git] / apps / codecs / libspeex / speex_header.c
blobb0e98b7c9c0d7ca057ade12dcec572fe3037cdcf
1 /* Copyright (C) 2002 Jean-Marc Valin
2 File: speex_header.c
3 Describes the Speex header
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 - Neither the name of the Xiph.org Foundation nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #ifdef HAVE_CONFIG_H
35 #include "config-speex.h"
36 #endif
38 #include "arch.h"
39 #include "speex/speex_header.h"
40 #include "speex/speex.h"
41 #include "os_support.h"
43 #ifndef NULL
44 #define NULL 0
45 #endif
47 /** Convert little endian */
48 static inline spx_int32_t le_int(spx_int32_t i)
50 #ifdef ROCKBOX
51 return letoh32(i);
52 #elif !defined(__LITTLE_ENDIAN__) && ( defined(WORDS_BIGENDIAN) || defined(__BIG_ENDIAN__) )
53 spx_uint32_t ui, ret;
54 ui = i;
55 ret = ui>>24;
56 ret |= (ui>>8)&0x0000ff00;
57 ret |= (ui<<8)&0x00ff0000;
58 ret |= (ui<<24);
59 return ret;
60 #else
61 return i;
62 #endif
65 #define ENDIAN_SWITCH(x) {x=le_int(x);}
69 typedef struct SpeexHeader {
70 char speex_string[8];
71 char speex_version[SPEEX_HEADER_VERSION_LENGTH];
72 int speex_version_id;
73 int header_size;
74 int rate;
75 int mode;
76 int mode_bitstream_version;
77 int nb_channels;
78 int bitrate;
79 int frame_size;
80 int vbr;
81 int frames_per_packet;
82 int extra_headers;
83 int reserved1;
84 int reserved2;
85 } SpeexHeader;
88 #ifndef SPEEX_DISABLE_ENCODER
89 void speex_init_header(SpeexHeader *header, int rate, int nb_channels, const SpeexMode *m)
91 int i;
92 const char *h="Speex ";
94 strncpy(header->speex_string, "Speex ", 8);
95 strncpy(header->speex_version, SPEEX_VERSION, SPEEX_HEADER_VERSION_LENGTH-1);
96 header->speex_version[SPEEX_HEADER_VERSION_LENGTH-1]=0;
98 for (i=0;i<8;i++)
99 header->speex_string[i]=h[i];
100 for (i=0;i<SPEEX_HEADER_VERSION_LENGTH-1 && SPEEX_VERSION[i];i++)
101 header->speex_version[i]=SPEEX_VERSION[i];
102 for (;i<SPEEX_HEADER_VERSION_LENGTH;i++)
103 header->speex_version[i]=0;
105 header->speex_version_id = 1;
106 header->header_size = sizeof(SpeexHeader);
108 header->rate = rate;
109 header->mode = m->modeID;
110 header->mode_bitstream_version = m->bitstream_version;
111 if (m->modeID<0)
112 speex_warning("This mode is meant to be used alone");
113 header->nb_channels = nb_channels;
114 header->bitrate = -1;
115 speex_mode_query(m, SPEEX_MODE_FRAME_SIZE, &header->frame_size);
116 header->vbr = 0;
118 header->frames_per_packet = 0;
119 header->extra_headers = 0;
120 header->reserved1 = 0;
121 header->reserved2 = 0;
124 char *speex_header_to_packet(SpeexHeader *header, int *size)
126 SpeexHeader *le_header;
127 le_header = (SpeexHeader*)speex_alloc(sizeof(SpeexHeader));
129 SPEEX_COPY(le_header, header, 1);
131 /*Make sure everything is now little-endian*/
132 ENDIAN_SWITCH(le_header->speex_version_id);
133 ENDIAN_SWITCH(le_header->header_size);
134 ENDIAN_SWITCH(le_header->rate);
135 ENDIAN_SWITCH(le_header->mode);
136 ENDIAN_SWITCH(le_header->mode_bitstream_version);
137 ENDIAN_SWITCH(le_header->nb_channels);
138 ENDIAN_SWITCH(le_header->bitrate);
139 ENDIAN_SWITCH(le_header->frame_size);
140 ENDIAN_SWITCH(le_header->vbr);
141 ENDIAN_SWITCH(le_header->frames_per_packet);
142 ENDIAN_SWITCH(le_header->extra_headers);
144 *size = sizeof(SpeexHeader);
145 return (char *)le_header;
147 #endif /* SPEEX_DISABLE_ENCODER */
149 static SpeexHeader global_le_header; /* Avoid malloc */
150 SpeexHeader *speex_packet_to_header(char *packet, int size)
152 int i;
153 SpeexHeader *le_header = &global_le_header;
154 const char *h = "Speex ";
155 for (i=0;i<8;i++)
156 if (packet[i]!=h[i])
158 speex_notify("This doesn't look like a Speex file");
159 return NULL;
162 /*FIXME: Do we allow larger headers?*/
163 if (size < (int)sizeof(SpeexHeader))
165 speex_notify("Speex header too small");
166 return NULL;
169 /* le_header = (SpeexHeader*)speex_alloc(sizeof(SpeexHeader)); */
171 SPEEX_COPY(le_header, (SpeexHeader*)packet, 1);
173 /*Make sure everything is converted correctly from little-endian*/
174 ENDIAN_SWITCH(le_header->speex_version_id);
175 ENDIAN_SWITCH(le_header->header_size);
176 ENDIAN_SWITCH(le_header->rate);
177 ENDIAN_SWITCH(le_header->mode);
178 ENDIAN_SWITCH(le_header->mode_bitstream_version);
179 ENDIAN_SWITCH(le_header->nb_channels);
180 ENDIAN_SWITCH(le_header->bitrate);
181 ENDIAN_SWITCH(le_header->frame_size);
182 ENDIAN_SWITCH(le_header->vbr);
183 ENDIAN_SWITCH(le_header->frames_per_packet);
184 ENDIAN_SWITCH(le_header->extra_headers);
186 return le_header;