r105: This commit was manufactured by cvs2svn to create tag
[cinelerra_cv/mob.git] / hvirtual / quicktime / libavcodec / pcm.c
blob811264b550b2c1609683575cb14d02a193088dda
1 /*
2 * PCM codecs
3 * Copyright (c) 2001 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "avcodec.h"
21 /* from g711.c by SUN microsystems (unrestricted use) */
23 #define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */
24 #define QUANT_MASK (0xf) /* Quantization field mask. */
25 #define NSEGS (8) /* Number of A-law segments. */
26 #define SEG_SHIFT (4) /* Left shift for segment number. */
27 #define SEG_MASK (0x70) /* Segment field mask. */
29 #define BIAS (0x84) /* Bias for linear code. */
32 * alaw2linear() - Convert an A-law value to 16-bit linear PCM
35 static int alaw2linear(unsigned char a_val)
37 int t;
38 int seg;
40 a_val ^= 0x55;
42 t = (a_val & QUANT_MASK) << 4;
43 seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT;
44 switch (seg) {
45 case 0:
46 t += 8;
47 break;
48 case 1:
49 t += 0x108;
50 break;
51 default:
52 t += 0x108;
53 t <<= seg - 1;
55 return ((a_val & SIGN_BIT) ? t : -t);
58 static int ulaw2linear(unsigned char u_val)
60 int t;
62 /* Complement to obtain normal u-law value. */
63 u_val = ~u_val;
66 * Extract and bias the quantization bits. Then
67 * shift up by the segment number and subtract out the bias.
69 t = ((u_val & QUANT_MASK) << 3) + BIAS;
70 t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
72 return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS));
75 /* 16384 entries per table */
76 static UINT8 *linear_to_alaw = NULL;
77 static int linear_to_alaw_ref = 0;
79 static UINT8 *linear_to_ulaw = NULL;
80 static int linear_to_ulaw_ref = 0;
82 static void build_xlaw_table(UINT8 *linear_to_xlaw,
83 int (*xlaw2linear)(unsigned char),
84 int mask)
86 int i, j, v, v1, v2;
88 j = 0;
89 for(i=0;i<128;i++) {
90 if (i != 127) {
91 v1 = xlaw2linear(i ^ mask);
92 v2 = xlaw2linear((i + 1) ^ mask);
93 v = (v1 + v2 + 4) >> 3;
94 } else {
95 v = 8192;
97 for(;j<v;j++) {
98 linear_to_xlaw[8192 + j] = (i ^ mask);
99 if (j > 0)
100 linear_to_xlaw[8192 - j] = (i ^ (mask ^ 0x80));
103 linear_to_xlaw[0] = linear_to_xlaw[1];
106 static int pcm_encode_init(AVCodecContext *avctx)
108 avctx->frame_size = 1;
109 switch(avctx->codec->id) {
110 case CODEC_ID_PCM_ALAW:
111 if (linear_to_alaw_ref == 0) {
112 linear_to_alaw = av_malloc(16384);
113 if (!linear_to_alaw)
114 return -1;
115 build_xlaw_table(linear_to_alaw, alaw2linear, 0xd5);
117 linear_to_alaw_ref++;
118 break;
119 case CODEC_ID_PCM_MULAW:
120 if (linear_to_ulaw_ref == 0) {
121 linear_to_ulaw = av_malloc(16384);
122 if (!linear_to_ulaw)
123 return -1;
124 build_xlaw_table(linear_to_ulaw, ulaw2linear, 0xff);
126 linear_to_ulaw_ref++;
127 break;
128 default:
129 break;
132 avctx->coded_frame= avcodec_alloc_frame();
133 avctx->coded_frame->key_frame= 1;
135 return 0;
138 static int pcm_encode_close(AVCodecContext *avctx)
140 av_freep(&avctx->coded_frame);
142 switch(avctx->codec->id) {
143 case CODEC_ID_PCM_ALAW:
144 if (--linear_to_alaw_ref == 0)
145 av_free(linear_to_alaw);
146 break;
147 case CODEC_ID_PCM_MULAW:
148 if (--linear_to_ulaw_ref == 0)
149 av_free(linear_to_ulaw);
150 break;
151 default:
152 /* nothing to free */
153 break;
155 return 0;
158 static int pcm_encode_frame(AVCodecContext *avctx,
159 unsigned char *frame, int buf_size, void *data)
161 int n, sample_size, v;
162 short *samples;
163 unsigned char *dst;
165 switch(avctx->codec->id) {
166 case CODEC_ID_PCM_S16LE:
167 case CODEC_ID_PCM_S16BE:
168 case CODEC_ID_PCM_U16LE:
169 case CODEC_ID_PCM_U16BE:
170 sample_size = 2;
171 break;
172 default:
173 sample_size = 1;
174 break;
176 n = buf_size / sample_size;
177 samples = data;
178 dst = frame;
180 switch(avctx->codec->id) {
181 case CODEC_ID_PCM_S16LE:
182 for(;n>0;n--) {
183 v = *samples++;
184 dst[0] = v & 0xff;
185 dst[1] = v >> 8;
186 dst += 2;
188 break;
189 case CODEC_ID_PCM_S16BE:
190 for(;n>0;n--) {
191 v = *samples++;
192 dst[0] = v >> 8;
193 dst[1] = v;
194 dst += 2;
196 break;
197 case CODEC_ID_PCM_U16LE:
198 for(;n>0;n--) {
199 v = *samples++;
200 v += 0x8000;
201 dst[0] = v & 0xff;
202 dst[1] = v >> 8;
203 dst += 2;
205 break;
206 case CODEC_ID_PCM_U16BE:
207 for(;n>0;n--) {
208 v = *samples++;
209 v += 0x8000;
210 dst[0] = v >> 8;
211 dst[1] = v;
212 dst += 2;
214 break;
215 case CODEC_ID_PCM_S8:
216 for(;n>0;n--) {
217 v = *samples++;
218 dst[0] = v >> 8;
219 dst++;
221 break;
222 case CODEC_ID_PCM_U8:
223 for(;n>0;n--) {
224 v = *samples++;
225 dst[0] = (v >> 8) + 128;
226 dst++;
228 break;
229 case CODEC_ID_PCM_ALAW:
230 for(;n>0;n--) {
231 v = *samples++;
232 dst[0] = linear_to_alaw[(v + 32768) >> 2];
233 dst++;
235 break;
236 case CODEC_ID_PCM_MULAW:
237 for(;n>0;n--) {
238 v = *samples++;
239 dst[0] = linear_to_ulaw[(v + 32768) >> 2];
240 dst++;
242 break;
243 default:
244 return -1;
246 //avctx->frame_size = (dst - frame) / (sample_size * avctx->channels);
248 return dst - frame;
251 typedef struct PCMDecode {
252 short table[256];
253 } PCMDecode;
255 static int pcm_decode_init(AVCodecContext * avctx)
257 PCMDecode *s = avctx->priv_data;
258 int i;
260 switch(avctx->codec->id) {
261 case CODEC_ID_PCM_ALAW:
262 for(i=0;i<256;i++)
263 s->table[i] = alaw2linear(i);
264 break;
265 case CODEC_ID_PCM_MULAW:
266 for(i=0;i<256;i++)
267 s->table[i] = ulaw2linear(i);
268 break;
269 default:
270 break;
272 return 0;
275 static int pcm_decode_frame(AVCodecContext *avctx,
276 void *data, int *data_size,
277 UINT8 *buf, int buf_size)
279 PCMDecode *s = avctx->priv_data;
280 int n;
281 short *samples;
282 UINT8 *src;
284 samples = data;
285 src = buf;
287 switch(avctx->codec->id) {
288 case CODEC_ID_PCM_S16LE:
289 n = buf_size >> 1;
290 for(;n>0;n--) {
291 *samples++ = src[0] | (src[1] << 8);
292 src += 2;
294 break;
295 case CODEC_ID_PCM_S16BE:
296 n = buf_size >> 1;
297 for(;n>0;n--) {
298 *samples++ = (src[0] << 8) | src[1];
299 src += 2;
301 break;
302 case CODEC_ID_PCM_U16LE:
303 n = buf_size >> 1;
304 for(;n>0;n--) {
305 *samples++ = (src[0] | (src[1] << 8)) - 0x8000;
306 src += 2;
308 break;
309 case CODEC_ID_PCM_U16BE:
310 n = buf_size >> 1;
311 for(;n>0;n--) {
312 *samples++ = ((src[0] << 8) | src[1]) - 0x8000;
313 src += 2;
315 break;
316 case CODEC_ID_PCM_S8:
317 n = buf_size;
318 for(;n>0;n--) {
319 *samples++ = src[0] << 8;
320 src++;
322 break;
323 case CODEC_ID_PCM_U8:
324 n = buf_size;
325 for(;n>0;n--) {
326 *samples++ = ((int)src[0] - 128) << 8;
327 src++;
329 break;
330 case CODEC_ID_PCM_ALAW:
331 case CODEC_ID_PCM_MULAW:
332 n = buf_size;
333 for(;n>0;n--) {
334 *samples++ = s->table[src[0]];
335 src++;
337 break;
338 default:
339 *data_size = 0;
340 return -1;
342 *data_size = (UINT8 *)samples - (UINT8 *)data;
343 return src - buf;
346 #define PCM_CODEC(id, name) \
347 AVCodec name ## _encoder = { \
348 #name, \
349 CODEC_TYPE_AUDIO, \
350 id, \
351 0, \
352 pcm_encode_init, \
353 pcm_encode_frame, \
354 pcm_encode_close, \
355 NULL, \
356 }; \
357 AVCodec name ## _decoder = { \
358 #name, \
359 CODEC_TYPE_AUDIO, \
360 id, \
361 sizeof(PCMDecode), \
362 pcm_decode_init, \
363 NULL, \
364 NULL, \
365 pcm_decode_frame, \
368 PCM_CODEC(CODEC_ID_PCM_S16LE, pcm_s16le);
369 PCM_CODEC(CODEC_ID_PCM_S16BE, pcm_s16be);
370 PCM_CODEC(CODEC_ID_PCM_U16LE, pcm_u16le);
371 PCM_CODEC(CODEC_ID_PCM_U16BE, pcm_u16be);
372 PCM_CODEC(CODEC_ID_PCM_S8, pcm_s8);
373 PCM_CODEC(CODEC_ID_PCM_U8, pcm_u8);
374 PCM_CODEC(CODEC_ID_PCM_ALAW, pcm_alaw);
375 PCM_CODEC(CODEC_ID_PCM_MULAW, pcm_mulaw);
377 #undef PCM_CODEC