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
27
/* from g711.c by SUN microsystems (unrestricted use) */
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. */
35
#define BIAS (0x84) /* Bias for linear code. */
38
* alaw2linear() - Convert an A-law value to 16-bit linear PCM
41
static int alaw2linear(unsigned char a_val)
48
t = (a_val & QUANT_MASK) << 4;
49
seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT;
61
return ((a_val & SIGN_BIT) ? t : -t);
64
static int ulaw2linear(unsigned char u_val)
68
/* Complement to obtain normal u-law value. */
72
* Extract and bias the quantization bits. Then
73
* shift up by the segment number and subtract out the bias.
75
t = ((u_val & QUANT_MASK) << 3) + BIAS;
76
t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
78
return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS));
81
/* 16384 entries per table */
82
static uint8_t *linear_to_alaw = NULL;
83
static int linear_to_alaw_ref = 0;
85
static uint8_t *linear_to_ulaw = NULL;
86
static int linear_to_ulaw_ref = 0;
88
static void build_xlaw_table(uint8_t *linear_to_xlaw,
89
int (*xlaw2linear)(unsigned char),
97
v1 = xlaw2linear(i ^ mask);
98
v2 = xlaw2linear((i + 1) ^ mask);
99
v = (v1 + v2 + 4) >> 3;
104
linear_to_xlaw[8192 + j] = (i ^ mask);
106
linear_to_xlaw[8192 - j] = (i ^ (mask ^ 0x80));
109
linear_to_xlaw[0] = linear_to_xlaw[1];
112
static int pcm_encode_init(AVCodecContext *avctx)
114
avctx->frame_size = 1;
115
switch(avctx->codec->id) {
116
case CODEC_ID_PCM_ALAW:
117
if (linear_to_alaw_ref == 0) {
118
linear_to_alaw = av_malloc(16384);
121
build_xlaw_table(linear_to_alaw, alaw2linear, 0xd5);
123
linear_to_alaw_ref++;
125
case CODEC_ID_PCM_MULAW:
126
if (linear_to_ulaw_ref == 0) {
127
linear_to_ulaw = av_malloc(16384);
130
build_xlaw_table(linear_to_ulaw, ulaw2linear, 0xff);
132
linear_to_ulaw_ref++;
138
avctx->coded_frame= avcodec_alloc_frame();
139
avctx->coded_frame->key_frame= 1;
144
static int pcm_encode_close(AVCodecContext *avctx)
146
av_freep(&avctx->coded_frame);
148
switch(avctx->codec->id) {
149
case CODEC_ID_PCM_ALAW:
150
if (--linear_to_alaw_ref == 0)
151
av_free(linear_to_alaw);
153
case CODEC_ID_PCM_MULAW:
154
if (--linear_to_ulaw_ref == 0)
155
av_free(linear_to_ulaw);
158
/* nothing to free */
164
static int pcm_encode_frame(AVCodecContext *avctx,
165
unsigned char *frame, int buf_size, void *data)
167
int n, sample_size, v;
171
switch(avctx->codec->id) {
172
case CODEC_ID_PCM_S16LE:
173
case CODEC_ID_PCM_S16BE:
174
case CODEC_ID_PCM_U16LE:
175
case CODEC_ID_PCM_U16BE:
182
n = buf_size / sample_size;
186
switch(avctx->codec->id) {
187
case CODEC_ID_PCM_S16LE:
195
case CODEC_ID_PCM_S16BE:
203
case CODEC_ID_PCM_U16LE:
212
case CODEC_ID_PCM_U16BE:
221
case CODEC_ID_PCM_S8:
228
case CODEC_ID_PCM_U8:
231
dst[0] = (v >> 8) + 128;
235
case CODEC_ID_PCM_ALAW:
238
dst[0] = linear_to_alaw[(v + 32768) >> 2];
242
case CODEC_ID_PCM_MULAW:
245
dst[0] = linear_to_ulaw[(v + 32768) >> 2];
252
//avctx->frame_size = (dst - frame) / (sample_size * avctx->channels);
257
typedef struct PCMDecode {
261
static int pcm_decode_init(AVCodecContext * avctx)
263
PCMDecode *s = avctx->priv_data;
266
switch(avctx->codec->id) {
267
case CODEC_ID_PCM_ALAW:
269
s->table[i] = alaw2linear(i);
271
case CODEC_ID_PCM_MULAW:
273
s->table[i] = ulaw2linear(i);
281
static int pcm_decode_frame(AVCodecContext *avctx,
282
void *data, int *data_size,
283
uint8_t *buf, int buf_size)
285
PCMDecode *s = avctx->priv_data;
293
switch(avctx->codec->id) {
294
case CODEC_ID_PCM_S16LE:
297
*samples++ = src[0] | (src[1] << 8);
301
case CODEC_ID_PCM_S16BE:
304
*samples++ = (src[0] << 8) | src[1];
308
case CODEC_ID_PCM_U16LE:
311
*samples++ = (src[0] | (src[1] << 8)) - 0x8000;
315
case CODEC_ID_PCM_U16BE:
318
*samples++ = ((src[0] << 8) | src[1]) - 0x8000;
322
case CODEC_ID_PCM_S8:
325
*samples++ = src[0] << 8;
329
case CODEC_ID_PCM_U8:
332
*samples++ = ((int)src[0] - 128) << 8;
336
case CODEC_ID_PCM_ALAW:
337
case CODEC_ID_PCM_MULAW:
340
*samples++ = s->table[src[0]];
348
*data_size = (uint8_t *)samples - (uint8_t *)data;
352
#define PCM_CODEC(id, name) \
353
AVCodec name ## _encoder = { \
363
AVCodec name ## _decoder = { \
374
PCM_CODEC(CODEC_ID_PCM_S16LE, pcm_s16le);
375
PCM_CODEC(CODEC_ID_PCM_S16BE, pcm_s16be);
376
PCM_CODEC(CODEC_ID_PCM_U16LE, pcm_u16le);
377
PCM_CODEC(CODEC_ID_PCM_U16BE, pcm_u16be);
378
PCM_CODEC(CODEC_ID_PCM_S8, pcm_s8);
379
PCM_CODEC(CODEC_ID_PCM_U8, pcm_u8);
380
PCM_CODEC(CODEC_ID_PCM_ALAW, pcm_alaw);
381
PCM_CODEC(CODEC_ID_PCM_MULAW, pcm_mulaw);