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
26
#include "bitstream.h" // for ff_reverse
28
/* from g711.c by SUN microsystems (unrestricted use) */
30
#define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */
31
#define QUANT_MASK (0xf) /* Quantization field mask. */
32
#define NSEGS (8) /* Number of A-law segments. */
33
#define SEG_SHIFT (4) /* Left shift for segment number. */
34
#define SEG_MASK (0x70) /* Segment field mask. */
36
#define BIAS (0x84) /* Bias for linear code. */
39
* alaw2linear() - Convert an A-law value to 16-bit linear PCM
42
static int alaw2linear(unsigned char a_val)
49
t = a_val & QUANT_MASK;
50
seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT;
51
if(seg) t= (t + t + 1 + 32) << (seg + 2);
52
else t= (t + t + 1 ) << 3;
54
return ((a_val & SIGN_BIT) ? t : -t);
57
static int ulaw2linear(unsigned char u_val)
61
/* Complement to obtain normal u-law value. */
65
* Extract and bias the quantization bits. Then
66
* shift up by the segment number and subtract out the bias.
68
t = ((u_val & QUANT_MASK) << 3) + BIAS;
69
t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
71
return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS));
74
/* 16384 entries per table */
75
static uint8_t *linear_to_alaw = NULL;
76
static int linear_to_alaw_ref = 0;
78
static uint8_t *linear_to_ulaw = NULL;
79
static int linear_to_ulaw_ref = 0;
81
static void build_xlaw_table(uint8_t *linear_to_xlaw,
82
int (*xlaw2linear)(unsigned char),
90
v1 = xlaw2linear(i ^ mask);
91
v2 = xlaw2linear((i + 1) ^ mask);
92
v = (v1 + v2 + 4) >> 3;
97
linear_to_xlaw[8192 + j] = (i ^ mask);
99
linear_to_xlaw[8192 - j] = (i ^ (mask ^ 0x80));
102
linear_to_xlaw[0] = linear_to_xlaw[1];
105
static int pcm_encode_init(AVCodecContext *avctx)
107
avctx->frame_size = 1;
108
switch(avctx->codec->id) {
109
case CODEC_ID_PCM_ALAW:
110
if (linear_to_alaw_ref == 0) {
111
linear_to_alaw = av_malloc(16384);
114
build_xlaw_table(linear_to_alaw, alaw2linear, 0xd5);
116
linear_to_alaw_ref++;
118
case CODEC_ID_PCM_MULAW:
119
if (linear_to_ulaw_ref == 0) {
120
linear_to_ulaw = av_malloc(16384);
123
build_xlaw_table(linear_to_ulaw, ulaw2linear, 0xff);
125
linear_to_ulaw_ref++;
131
switch(avctx->codec->id) {
132
case CODEC_ID_PCM_S32LE:
133
case CODEC_ID_PCM_S32BE:
134
case CODEC_ID_PCM_U32LE:
135
case CODEC_ID_PCM_U32BE:
136
avctx->block_align = 4 * avctx->channels;
138
case CODEC_ID_PCM_S24LE:
139
case CODEC_ID_PCM_S24BE:
140
case CODEC_ID_PCM_U24LE:
141
case CODEC_ID_PCM_U24BE:
142
case CODEC_ID_PCM_S24DAUD:
143
avctx->block_align = 3 * avctx->channels;
145
case CODEC_ID_PCM_S16LE:
146
case CODEC_ID_PCM_S16BE:
147
case CODEC_ID_PCM_U16LE:
148
case CODEC_ID_PCM_U16BE:
149
avctx->block_align = 2 * avctx->channels;
151
case CODEC_ID_PCM_S8:
152
case CODEC_ID_PCM_U8:
153
case CODEC_ID_PCM_MULAW:
154
case CODEC_ID_PCM_ALAW:
155
avctx->block_align = avctx->channels;
161
avctx->coded_frame= avcodec_alloc_frame();
162
avctx->coded_frame->key_frame= 1;
167
static int pcm_encode_close(AVCodecContext *avctx)
169
av_freep(&avctx->coded_frame);
171
switch(avctx->codec->id) {
172
case CODEC_ID_PCM_ALAW:
173
if (--linear_to_alaw_ref == 0)
174
av_free(linear_to_alaw);
176
case CODEC_ID_PCM_MULAW:
177
if (--linear_to_ulaw_ref == 0)
178
av_free(linear_to_ulaw);
181
/* nothing to free */
188
* \brief convert samples from 16 bit
189
* \param bps byte per sample for the destination format, must be >= 2
190
* \param le 0 for big-, 1 for little-endian
191
* \param us 0 for signed, 1 for unsigned output
192
* \param samples input samples
193
* \param dst output samples
194
* \param n number of samples in samples buffer.
196
static inline void encode_from16(int bps, int le, int us,
197
short **samples, uint8_t **dst, int n) {
199
memset(*dst, 0, n * bps);
200
if (le) *dst += bps - 2;
202
register int v = *(*samples)++;
208
if (le) *dst -= bps - 2;
211
static int pcm_encode_frame(AVCodecContext *avctx,
212
unsigned char *frame, int buf_size, void *data)
214
int n, sample_size, v;
218
switch(avctx->codec->id) {
219
case CODEC_ID_PCM_S32LE:
220
case CODEC_ID_PCM_S32BE:
221
case CODEC_ID_PCM_U32LE:
222
case CODEC_ID_PCM_U32BE:
225
case CODEC_ID_PCM_S24LE:
226
case CODEC_ID_PCM_S24BE:
227
case CODEC_ID_PCM_U24LE:
228
case CODEC_ID_PCM_U24BE:
229
case CODEC_ID_PCM_S24DAUD:
232
case CODEC_ID_PCM_S16LE:
233
case CODEC_ID_PCM_S16BE:
234
case CODEC_ID_PCM_U16LE:
235
case CODEC_ID_PCM_U16BE:
242
n = buf_size / sample_size;
246
switch(avctx->codec->id) {
247
case CODEC_ID_PCM_S32LE:
248
encode_from16(4, 1, 0, &samples, &dst, n);
250
case CODEC_ID_PCM_S32BE:
251
encode_from16(4, 0, 0, &samples, &dst, n);
253
case CODEC_ID_PCM_U32LE:
254
encode_from16(4, 1, 1, &samples, &dst, n);
256
case CODEC_ID_PCM_U32BE:
257
encode_from16(4, 0, 1, &samples, &dst, n);
259
case CODEC_ID_PCM_S24LE:
260
encode_from16(3, 1, 0, &samples, &dst, n);
262
case CODEC_ID_PCM_S24BE:
263
encode_from16(3, 0, 0, &samples, &dst, n);
265
case CODEC_ID_PCM_U24LE:
266
encode_from16(3, 1, 1, &samples, &dst, n);
268
case CODEC_ID_PCM_U24BE:
269
encode_from16(3, 0, 1, &samples, &dst, n);
271
case CODEC_ID_PCM_S24DAUD:
273
uint32_t tmp = ff_reverse[*samples >> 8] +
274
(ff_reverse[*samples & 0xff] << 8);
275
tmp <<= 4; // sync flags would go here
284
case CODEC_ID_PCM_S16LE:
292
case CODEC_ID_PCM_S16BE:
300
case CODEC_ID_PCM_U16LE:
309
case CODEC_ID_PCM_U16BE:
318
case CODEC_ID_PCM_S8:
325
case CODEC_ID_PCM_U8:
328
dst[0] = (v >> 8) + 128;
332
case CODEC_ID_PCM_ALAW:
335
dst[0] = linear_to_alaw[(v + 32768) >> 2];
339
case CODEC_ID_PCM_MULAW:
342
dst[0] = linear_to_ulaw[(v + 32768) >> 2];
349
//avctx->frame_size = (dst - frame) / (sample_size * avctx->channels);
354
typedef struct PCMDecode {
358
static int pcm_decode_init(AVCodecContext * avctx)
360
PCMDecode *s = avctx->priv_data;
363
switch(avctx->codec->id) {
364
case CODEC_ID_PCM_ALAW:
366
s->table[i] = alaw2linear(i);
368
case CODEC_ID_PCM_MULAW:
370
s->table[i] = ulaw2linear(i);
379
* \brief convert samples to 16 bit
380
* \param bps byte per sample for the source format, must be >= 2
381
* \param le 0 for big-, 1 for little-endian
382
* \param us 0 for signed, 1 for unsigned input
383
* \param src input samples
384
* \param samples output samples
385
* \param src_len number of bytes in src
387
static inline void decode_to16(int bps, int le, int us,
388
uint8_t **src, short **samples, int src_len)
390
register int n = src_len / bps;
391
if (le) *src += bps - 2;
393
*(*samples)++ = ((*src)[le] << 8 | (*src)[1 - le]) - (us?0x8000:0);
396
if (le) *src -= bps - 2;
399
static int pcm_decode_frame(AVCodecContext *avctx,
400
void *data, int *data_size,
401
uint8_t *buf, int buf_size)
403
PCMDecode *s = avctx->priv_data;
411
if(buf_size > AVCODEC_MAX_AUDIO_FRAME_SIZE/2)
412
buf_size = AVCODEC_MAX_AUDIO_FRAME_SIZE/2;
414
switch(avctx->codec->id) {
415
case CODEC_ID_PCM_S32LE:
416
decode_to16(4, 1, 0, &src, &samples, buf_size);
418
case CODEC_ID_PCM_S32BE:
419
decode_to16(4, 0, 0, &src, &samples, buf_size);
421
case CODEC_ID_PCM_U32LE:
422
decode_to16(4, 1, 1, &src, &samples, buf_size);
424
case CODEC_ID_PCM_U32BE:
425
decode_to16(4, 0, 1, &src, &samples, buf_size);
427
case CODEC_ID_PCM_S24LE:
428
decode_to16(3, 1, 0, &src, &samples, buf_size);
430
case CODEC_ID_PCM_S24BE:
431
decode_to16(3, 0, 0, &src, &samples, buf_size);
433
case CODEC_ID_PCM_U24LE:
434
decode_to16(3, 1, 1, &src, &samples, buf_size);
436
case CODEC_ID_PCM_U24BE:
437
decode_to16(3, 0, 1, &src, &samples, buf_size);
439
case CODEC_ID_PCM_S24DAUD:
442
uint32_t v = src[0] << 16 | src[1] << 8 | src[2];
443
v >>= 4; // sync flags are here
444
*samples++ = ff_reverse[(v >> 8) & 0xff] +
445
(ff_reverse[v & 0xff] << 8);
449
case CODEC_ID_PCM_S16LE:
452
*samples++ = src[0] | (src[1] << 8);
456
case CODEC_ID_PCM_S16BE:
459
*samples++ = (src[0] << 8) | src[1];
463
case CODEC_ID_PCM_U16LE:
466
*samples++ = (src[0] | (src[1] << 8)) - 0x8000;
470
case CODEC_ID_PCM_U16BE:
473
*samples++ = ((src[0] << 8) | src[1]) - 0x8000;
477
case CODEC_ID_PCM_S8:
480
*samples++ = src[0] << 8;
484
case CODEC_ID_PCM_U8:
487
*samples++ = ((int)src[0] - 128) << 8;
491
case CODEC_ID_PCM_ALAW:
492
case CODEC_ID_PCM_MULAW:
495
*samples++ = s->table[src[0]];
502
*data_size = (uint8_t *)samples - (uint8_t *)data;
506
#define PCM_CODEC(id, name) \
507
AVCodec name ## _encoder = { \
517
AVCodec name ## _decoder = { \
528
PCM_CODEC(CODEC_ID_PCM_S32LE, pcm_s32le);
529
PCM_CODEC(CODEC_ID_PCM_S32BE, pcm_s32be);
530
PCM_CODEC(CODEC_ID_PCM_U32LE, pcm_u32le);
531
PCM_CODEC(CODEC_ID_PCM_U32BE, pcm_u32be);
532
PCM_CODEC(CODEC_ID_PCM_S24LE, pcm_s24le);
533
PCM_CODEC(CODEC_ID_PCM_S24BE, pcm_s24be);
534
PCM_CODEC(CODEC_ID_PCM_U24LE, pcm_u24le);
535
PCM_CODEC(CODEC_ID_PCM_U24BE, pcm_u24be);
536
PCM_CODEC(CODEC_ID_PCM_S24DAUD, pcm_s24daud);
537
PCM_CODEC(CODEC_ID_PCM_S16LE, pcm_s16le);
538
PCM_CODEC(CODEC_ID_PCM_S16BE, pcm_s16be);
539
PCM_CODEC(CODEC_ID_PCM_U16LE, pcm_u16le);
540
PCM_CODEC(CODEC_ID_PCM_U16BE, pcm_u16be);
541
PCM_CODEC(CODEC_ID_PCM_S8, pcm_s8);
542
PCM_CODEC(CODEC_ID_PCM_U8, pcm_u8);
543
PCM_CODEC(CODEC_ID_PCM_ALAW, pcm_alaw);
544
PCM_CODEC(CODEC_ID_PCM_MULAW, pcm_mulaw);