setting timebase in the encoder doesnt seem like a good idea
[libav.git] / libavcodec / pcm.c
CommitLineData
a96b68b7
FB
1/*
2 * PCM codecs
ff4ec49e 3 * Copyright (c) 2001 Fabrice Bellard.
a96b68b7 4 *
ff4ec49e
FB
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.
a96b68b7 9 *
ff4ec49e 10 * This library is distributed in the hope that it will be useful,
a96b68b7 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
a96b68b7 14 *
ff4ec49e
FB
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
a96b68b7 18 */
1ab3d669
MN
19
20/**
21 * @file pcm.c
22 * PCM codecs
23 */
24
a96b68b7
FB
25#include "avcodec.h"
26
27/* from g711.c by SUN microsystems (unrestricted use) */
28
29#define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */
30#define QUANT_MASK (0xf) /* Quantization field mask. */
31#define NSEGS (8) /* Number of A-law segments. */
32#define SEG_SHIFT (4) /* Left shift for segment number. */
33#define SEG_MASK (0x70) /* Segment field mask. */
34
35#define BIAS (0x84) /* Bias for linear code. */
36
37/*
38 * alaw2linear() - Convert an A-law value to 16-bit linear PCM
39 *
40 */
41static int alaw2linear(unsigned char a_val)
42{
43 int t;
44 int seg;
45
46 a_val ^= 0x55;
47
cd1f22f9 48 t = a_val & QUANT_MASK;
a96b68b7 49 seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT;
cd1f22f9
MN
50 if(seg) t= (t + t + 1 + 32) << (seg + 2);
51 else t= (t + t + 1 ) << 3;
52
a96b68b7
FB
53 return ((a_val & SIGN_BIT) ? t : -t);
54}
55
56static int ulaw2linear(unsigned char u_val)
57{
58 int t;
59
60 /* Complement to obtain normal u-law value. */
61 u_val = ~u_val;
62
63 /*
64 * Extract and bias the quantization bits. Then
65 * shift up by the segment number and subtract out the bias.
66 */
67 t = ((u_val & QUANT_MASK) << 3) + BIAS;
68 t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
69
70 return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS));
71}
72
73/* 16384 entries per table */
0c1a9eda 74static uint8_t *linear_to_alaw = NULL;
a96b68b7
FB
75static int linear_to_alaw_ref = 0;
76
0c1a9eda 77static uint8_t *linear_to_ulaw = NULL;
a96b68b7
FB
78static int linear_to_ulaw_ref = 0;
79
0c1a9eda 80static void build_xlaw_table(uint8_t *linear_to_xlaw,
a96b68b7
FB
81 int (*xlaw2linear)(unsigned char),
82 int mask)
83{
84 int i, j, v, v1, v2;
85
86 j = 0;
87 for(i=0;i<128;i++) {
88 if (i != 127) {
89 v1 = xlaw2linear(i ^ mask);
90 v2 = xlaw2linear((i + 1) ^ mask);
91 v = (v1 + v2 + 4) >> 3;
92 } else {
93 v = 8192;
94 }
95 for(;j<v;j++) {
96 linear_to_xlaw[8192 + j] = (i ^ mask);
97 if (j > 0)
98 linear_to_xlaw[8192 - j] = (i ^ (mask ^ 0x80));
99 }
100 }
101 linear_to_xlaw[0] = linear_to_xlaw[1];
102}
103
cd4af68a 104static int pcm_encode_init(AVCodecContext *avctx)
a96b68b7
FB
105{
106 avctx->frame_size = 1;
107 switch(avctx->codec->id) {
108 case CODEC_ID_PCM_ALAW:
109 if (linear_to_alaw_ref == 0) {
6000abfa 110 linear_to_alaw = av_malloc(16384);
a96b68b7
FB
111 if (!linear_to_alaw)
112 return -1;
113 build_xlaw_table(linear_to_alaw, alaw2linear, 0xd5);
114 }
115 linear_to_alaw_ref++;
116 break;
117 case CODEC_ID_PCM_MULAW:
118 if (linear_to_ulaw_ref == 0) {
6000abfa 119 linear_to_ulaw = av_malloc(16384);
a96b68b7
FB
120 if (!linear_to_ulaw)
121 return -1;
122 build_xlaw_table(linear_to_ulaw, ulaw2linear, 0xff);
123 }
124 linear_to_ulaw_ref++;
125 break;
126 default:
127 break;
128 }
492cd3a9 129
359fa0fe
MN
130 switch(avctx->codec->id) {
131 case CODEC_ID_PCM_S16LE:
132 case CODEC_ID_PCM_S16BE:
133 case CODEC_ID_PCM_U16LE:
134 case CODEC_ID_PCM_U16BE:
135 avctx->block_align = 2 * avctx->channels;
136 break;
137 case CODEC_ID_PCM_S8:
138 case CODEC_ID_PCM_U8:
139 case CODEC_ID_PCM_MULAW:
140 case CODEC_ID_PCM_ALAW:
141 avctx->block_align = avctx->channels;
142 break;
143 default:
144 break;
145 }
146
492cd3a9
MN
147 avctx->coded_frame= avcodec_alloc_frame();
148 avctx->coded_frame->key_frame= 1;
149
a96b68b7
FB
150 return 0;
151}
152
cd4af68a 153static int pcm_encode_close(AVCodecContext *avctx)
a96b68b7 154{
492cd3a9
MN
155 av_freep(&avctx->coded_frame);
156
a96b68b7
FB
157 switch(avctx->codec->id) {
158 case CODEC_ID_PCM_ALAW:
159 if (--linear_to_alaw_ref == 0)
6000abfa 160 av_free(linear_to_alaw);
a96b68b7
FB
161 break;
162 case CODEC_ID_PCM_MULAW:
163 if (--linear_to_ulaw_ref == 0)
6000abfa 164 av_free(linear_to_ulaw);
a96b68b7
FB
165 break;
166 default:
167 /* nothing to free */
168 break;
169 }
170 return 0;
171}
172
cd4af68a
ZK
173static int pcm_encode_frame(AVCodecContext *avctx,
174 unsigned char *frame, int buf_size, void *data)
a96b68b7
FB
175{
176 int n, sample_size, v;
177 short *samples;
178 unsigned char *dst;
179
180 switch(avctx->codec->id) {
181 case CODEC_ID_PCM_S16LE:
182 case CODEC_ID_PCM_S16BE:
183 case CODEC_ID_PCM_U16LE:
184 case CODEC_ID_PCM_U16BE:
185 sample_size = 2;
186 break;
187 default:
188 sample_size = 1;
189 break;
190 }
191 n = buf_size / sample_size;
192 samples = data;
193 dst = frame;
194
195 switch(avctx->codec->id) {
196 case CODEC_ID_PCM_S16LE:
197 for(;n>0;n--) {
198 v = *samples++;
199 dst[0] = v & 0xff;
200 dst[1] = v >> 8;
201 dst += 2;
202 }
203 break;
204 case CODEC_ID_PCM_S16BE:
205 for(;n>0;n--) {
206 v = *samples++;
207 dst[0] = v >> 8;
208 dst[1] = v;
209 dst += 2;
210 }
211 break;
212 case CODEC_ID_PCM_U16LE:
213 for(;n>0;n--) {
214 v = *samples++;
215 v += 0x8000;
216 dst[0] = v & 0xff;
217 dst[1] = v >> 8;
218 dst += 2;
219 }
220 break;
221 case CODEC_ID_PCM_U16BE:
222 for(;n>0;n--) {
223 v = *samples++;
224 v += 0x8000;
225 dst[0] = v >> 8;
226 dst[1] = v;
227 dst += 2;
228 }
229 break;
230 case CODEC_ID_PCM_S8:
231 for(;n>0;n--) {
232 v = *samples++;
0eaec105 233 dst[0] = v >> 8;
a96b68b7
FB
234 dst++;
235 }
236 break;
237 case CODEC_ID_PCM_U8:
238 for(;n>0;n--) {
239 v = *samples++;
0eaec105 240 dst[0] = (v >> 8) + 128;
a96b68b7
FB
241 dst++;
242 }
243 break;
244 case CODEC_ID_PCM_ALAW:
245 for(;n>0;n--) {
246 v = *samples++;
247 dst[0] = linear_to_alaw[(v + 32768) >> 2];
248 dst++;
249 }
250 break;
251 case CODEC_ID_PCM_MULAW:
252 for(;n>0;n--) {
253 v = *samples++;
254 dst[0] = linear_to_ulaw[(v + 32768) >> 2];
255 dst++;
256 }
257 break;
258 default:
259 return -1;
260 }
13a0314f 261 //avctx->frame_size = (dst - frame) / (sample_size * avctx->channels);
4c3d2e5f 262
a96b68b7
FB
263 return dst - frame;
264}
265
266typedef struct PCMDecode {
267 short table[256];
268} PCMDecode;
269
cd4af68a 270static int pcm_decode_init(AVCodecContext * avctx)
a96b68b7
FB
271{
272 PCMDecode *s = avctx->priv_data;
273 int i;
274
275 switch(avctx->codec->id) {
276 case CODEC_ID_PCM_ALAW:
277 for(i=0;i<256;i++)
278 s->table[i] = alaw2linear(i);
279 break;
280 case CODEC_ID_PCM_MULAW:
281 for(i=0;i<256;i++)
282 s->table[i] = ulaw2linear(i);
283 break;
284 default:
285 break;
286 }
287 return 0;
288}
289
cd4af68a
ZK
290static int pcm_decode_frame(AVCodecContext *avctx,
291 void *data, int *data_size,
0c1a9eda 292 uint8_t *buf, int buf_size)
a96b68b7
FB
293{
294 PCMDecode *s = avctx->priv_data;
295 int n;
296 short *samples;
0c1a9eda 297 uint8_t *src;
a96b68b7
FB
298
299 samples = data;
300 src = buf;
301
a8d02f2b
MN
302 if(buf_size > AVCODEC_MAX_AUDIO_FRAME_SIZE/2)
303 buf_size = AVCODEC_MAX_AUDIO_FRAME_SIZE/2;
304
a96b68b7
FB
305 switch(avctx->codec->id) {
306 case CODEC_ID_PCM_S16LE:
307 n = buf_size >> 1;
308 for(;n>0;n--) {
309 *samples++ = src[0] | (src[1] << 8);
310 src += 2;
311 }
312 break;
313 case CODEC_ID_PCM_S16BE:
314 n = buf_size >> 1;
315 for(;n>0;n--) {
316 *samples++ = (src[0] << 8) | src[1];
317 src += 2;
318 }
319 break;
320 case CODEC_ID_PCM_U16LE:
321 n = buf_size >> 1;
322 for(;n>0;n--) {
323 *samples++ = (src[0] | (src[1] << 8)) - 0x8000;
324 src += 2;
325 }
326 break;
327 case CODEC_ID_PCM_U16BE:
328 n = buf_size >> 1;
329 for(;n>0;n--) {
330 *samples++ = ((src[0] << 8) | src[1]) - 0x8000;
331 src += 2;
332 }
333 break;
334 case CODEC_ID_PCM_S8:
335 n = buf_size;
336 for(;n>0;n--) {
337 *samples++ = src[0] << 8;
338 src++;
339 }
340 break;
341 case CODEC_ID_PCM_U8:
342 n = buf_size;
343 for(;n>0;n--) {
344 *samples++ = ((int)src[0] - 128) << 8;
345 src++;
346 }
347 break;
348 case CODEC_ID_PCM_ALAW:
349 case CODEC_ID_PCM_MULAW:
350 n = buf_size;
351 for(;n>0;n--) {
352 *samples++ = s->table[src[0]];
353 src++;
354 }
355 break;
356 default:
a96b68b7
FB
357 return -1;
358 }
0c1a9eda 359 *data_size = (uint8_t *)samples - (uint8_t *)data;
a96b68b7
FB
360 return src - buf;
361}
362
363#define PCM_CODEC(id, name) \
364AVCodec name ## _encoder = { \
365 #name, \
366 CODEC_TYPE_AUDIO, \
367 id, \
368 0, \
cd4af68a
ZK
369 pcm_encode_init, \
370 pcm_encode_frame, \
371 pcm_encode_close, \
a96b68b7
FB
372 NULL, \
373}; \
374AVCodec name ## _decoder = { \
375 #name, \
376 CODEC_TYPE_AUDIO, \
377 id, \
378 sizeof(PCMDecode), \
cd4af68a 379 pcm_decode_init, \
a96b68b7
FB
380 NULL, \
381 NULL, \
cd4af68a 382 pcm_decode_frame, \
ef9f7306 383}
a96b68b7
FB
384
385PCM_CODEC(CODEC_ID_PCM_S16LE, pcm_s16le);
386PCM_CODEC(CODEC_ID_PCM_S16BE, pcm_s16be);
387PCM_CODEC(CODEC_ID_PCM_U16LE, pcm_u16le);
388PCM_CODEC(CODEC_ID_PCM_U16BE, pcm_u16be);
389PCM_CODEC(CODEC_ID_PCM_S8, pcm_s8);
390PCM_CODEC(CODEC_ID_PCM_U8, pcm_u8);
391PCM_CODEC(CODEC_ID_PCM_ALAW, pcm_alaw);
392PCM_CODEC(CODEC_ID_PCM_MULAW, pcm_mulaw);
cd4af68a
ZK
393
394#undef PCM_CODEC