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
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
)
42 t
= (a_val
& QUANT_MASK
) << 4;
43 seg
= ((unsigned)a_val
& SEG_MASK
) >> SEG_SHIFT
;
55 return ((a_val
& SIGN_BIT
) ? t
: -t
);
58 static int ulaw2linear(unsigned char u_val
)
62 /* Complement to obtain normal u-law value. */
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),
91 v1
= xlaw2linear(i
^ mask
);
92 v2
= xlaw2linear((i
+ 1) ^ mask
);
93 v
= (v1
+ v2
+ 4) >> 3;
98 linear_to_xlaw
[8192 + j
] = (i
^ mask
);
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);
115 build_xlaw_table(linear_to_alaw
, alaw2linear
, 0xd5);
117 linear_to_alaw_ref
++;
119 case CODEC_ID_PCM_MULAW
:
120 if (linear_to_ulaw_ref
== 0) {
121 linear_to_ulaw
= av_malloc(16384);
124 build_xlaw_table(linear_to_ulaw
, ulaw2linear
, 0xff);
126 linear_to_ulaw_ref
++;
132 avctx
->coded_frame
= avcodec_alloc_frame();
133 avctx
->coded_frame
->key_frame
= 1;
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
);
147 case CODEC_ID_PCM_MULAW
:
148 if (--linear_to_ulaw_ref
== 0)
149 av_free(linear_to_ulaw
);
152 /* nothing to free */
158 static int pcm_encode_frame(AVCodecContext
*avctx
,
159 unsigned char *frame
, int buf_size
, void *data
)
161 int n
, sample_size
, v
;
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
:
176 n
= buf_size
/ sample_size
;
180 switch(avctx
->codec
->id
) {
181 case CODEC_ID_PCM_S16LE
:
189 case CODEC_ID_PCM_S16BE
:
197 case CODEC_ID_PCM_U16LE
:
206 case CODEC_ID_PCM_U16BE
:
215 case CODEC_ID_PCM_S8
:
222 case CODEC_ID_PCM_U8
:
225 dst
[0] = (v
>> 8) + 128;
229 case CODEC_ID_PCM_ALAW
:
232 dst
[0] = linear_to_alaw
[(v
+ 32768) >> 2];
236 case CODEC_ID_PCM_MULAW
:
239 dst
[0] = linear_to_ulaw
[(v
+ 32768) >> 2];
246 //avctx->frame_size = (dst - frame) / (sample_size * avctx->channels);
251 typedef struct PCMDecode
{
255 static int pcm_decode_init(AVCodecContext
* avctx
)
257 PCMDecode
*s
= avctx
->priv_data
;
260 switch(avctx
->codec
->id
) {
261 case CODEC_ID_PCM_ALAW
:
263 s
->table
[i
] = alaw2linear(i
);
265 case CODEC_ID_PCM_MULAW
:
267 s
->table
[i
] = ulaw2linear(i
);
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
;
287 switch(avctx
->codec
->id
) {
288 case CODEC_ID_PCM_S16LE
:
291 *samples
++ = src
[0] | (src
[1] << 8);
295 case CODEC_ID_PCM_S16BE
:
298 *samples
++ = (src
[0] << 8) | src
[1];
302 case CODEC_ID_PCM_U16LE
:
305 *samples
++ = (src
[0] | (src
[1] << 8)) - 0x8000;
309 case CODEC_ID_PCM_U16BE
:
312 *samples
++ = ((src
[0] << 8) | src
[1]) - 0x8000;
316 case CODEC_ID_PCM_S8
:
319 *samples
++ = src
[0] << 8;
323 case CODEC_ID_PCM_U8
:
326 *samples
++ = ((int)src
[0] - 128) << 8;
330 case CODEC_ID_PCM_ALAW
:
331 case CODEC_ID_PCM_MULAW
:
334 *samples
++ = s
->table
[src
[0]];
342 *data_size
= (UINT8
*)samples
- (UINT8
*)data
;
346 #define PCM_CODEC(id, name) \
347 AVCodec name ## _encoder = { \
357 AVCodec name ## _decoder = { \
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
);