3 * Copyright (c) 2001 Fabrice Bellard.
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "bitstream.h" // for ff_reverse
29 #include "bytestream.h"
31 #define MAX_CHANNELS 64
33 /* from g711.c by SUN microsystems (unrestricted use) */
35 #define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */
36 #define QUANT_MASK (0xf) /* Quantization field mask. */
37 #define NSEGS (8) /* Number of A-law segments. */
38 #define SEG_SHIFT (4) /* Left shift for segment number. */
39 #define SEG_MASK (0x70) /* Segment field mask. */
41 #define BIAS (0x84) /* Bias for linear code. */
44 * alaw2linear() - Convert an A-law value to 16-bit linear PCM
47 static av_cold int alaw2linear(unsigned char a_val)
54 t = a_val & QUANT_MASK;
55 seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT;
56 if(seg) t= (t + t + 1 + 32) << (seg + 2);
57 else t= (t + t + 1 ) << 3;
59 return (a_val & SIGN_BIT) ? t : -t;
62 static av_cold int ulaw2linear(unsigned char u_val)
66 /* Complement to obtain normal u-law value. */
70 * Extract and bias the quantization bits. Then
71 * shift up by the segment number and subtract out the bias.
73 t = ((u_val & QUANT_MASK) << 3) + BIAS;
74 t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
76 return (u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS);
79 /* 16384 entries per table */
80 static uint8_t linear_to_alaw[16384];
81 static uint8_t linear_to_ulaw[16384];
83 static av_cold void build_xlaw_table(uint8_t *linear_to_xlaw,
84 int (*xlaw2linear)(unsigned char),
92 v1 = xlaw2linear(i ^ mask);
93 v2 = xlaw2linear((i + 1) ^ mask);
94 v = (v1 + v2 + 4) >> 3;
99 linear_to_xlaw[8192 + j] = (i ^ mask);
101 linear_to_xlaw[8192 - j] = (i ^ (mask ^ 0x80));
104 linear_to_xlaw[0] = linear_to_xlaw[1];
107 static av_cold int pcm_encode_init(AVCodecContext *avctx)
109 avctx->frame_size = 1;
110 switch(avctx->codec->id) {
111 case CODEC_ID_PCM_ALAW:
112 build_xlaw_table(linear_to_alaw, alaw2linear, 0xd5);
114 case CODEC_ID_PCM_MULAW:
115 build_xlaw_table(linear_to_ulaw, ulaw2linear, 0xff);
121 avctx->block_align = avctx->channels * av_get_bits_per_sample(avctx->codec->id)/8;
122 avctx->coded_frame= avcodec_alloc_frame();
123 avctx->coded_frame->key_frame= 1;
128 static av_cold int pcm_encode_close(AVCodecContext *avctx)
130 av_freep(&avctx->coded_frame);
136 * Write PCM samples macro
137 * @param type Datatype of native machine format
138 * @param endian bytestream_put_xxx() suffix
139 * @param src Source pointer (variable name)
140 * @param dst Destination pointer (variable name)
141 * @param n Total number of samples (variable name)
142 * @param shift Bitshift (bits)
143 * @param offset Sample value offset
145 #define ENCODE(type, endian, src, dst, n, shift, offset) \
146 samples_##type = (type*)src; \
148 register type v = (*samples_##type++ >> shift) + offset; \
149 bytestream_put_##endian(&dst, v); \
152 static int pcm_encode_frame(AVCodecContext *avctx,
153 unsigned char *frame, int buf_size, void *data)
155 int n, sample_size, v;
159 int16_t *samples_int16_t;
160 int32_t *samples_int32_t;
161 uint16_t *samples_uint16_t;
162 uint32_t *samples_uint32_t;
164 sample_size = av_get_bits_per_sample(avctx->codec->id)/8;
165 n = buf_size / sample_size;
169 if (avctx->sample_fmt!=avctx->codec->sample_fmts[0]) {
170 av_log(avctx, AV_LOG_ERROR, "invalid sample_fmt\n");
174 switch(avctx->codec->id) {
175 case CODEC_ID_PCM_U32LE:
176 ENCODE(uint32_t, le32, samples, dst, n, 0, 0x80000000)
178 case CODEC_ID_PCM_U32BE:
179 ENCODE(uint32_t, be32, samples, dst, n, 0, 0x80000000)
181 case CODEC_ID_PCM_S24LE:
182 ENCODE(int32_t, le24, samples, dst, n, 8, 0)
184 case CODEC_ID_PCM_S24BE:
185 ENCODE(int32_t, be24, samples, dst, n, 8, 0)
187 case CODEC_ID_PCM_U24LE:
188 ENCODE(uint32_t, le24, samples, dst, n, 8, 0x800000)
190 case CODEC_ID_PCM_U24BE:
191 ENCODE(uint32_t, be24, samples, dst, n, 8, 0x800000)
193 case CODEC_ID_PCM_S24DAUD:
195 uint32_t tmp = ff_reverse[(*samples >> 8) & 0xff] +
196 (ff_reverse[*samples & 0xff] << 8);
197 tmp <<= 4; // sync flags would go here
198 bytestream_put_be24(&dst, tmp);
202 case CODEC_ID_PCM_U16LE:
203 ENCODE(uint16_t, le16, samples, dst, n, 0, 0x8000)
205 case CODEC_ID_PCM_U16BE:
206 ENCODE(uint16_t, be16, samples, dst, n, 0, 0x8000)
208 case CODEC_ID_PCM_S8:
216 case CODEC_ID_PCM_S32LE:
217 ENCODE(int32_t, le32, samples, dst, n, 0, 0)
219 case CODEC_ID_PCM_S16LE:
220 ENCODE(int16_t, le16, samples, dst, n, 0, 0)
222 case CODEC_ID_PCM_F32BE:
223 case CODEC_ID_PCM_S32BE:
224 case CODEC_ID_PCM_S16BE:
226 case CODEC_ID_PCM_F32BE:
227 case CODEC_ID_PCM_S32BE:
228 ENCODE(int32_t, be32, samples, dst, n, 0, 0)
230 case CODEC_ID_PCM_S16BE:
231 ENCODE(int16_t, be16, samples, dst, n, 0, 0)
233 case CODEC_ID_PCM_S32LE:
234 case CODEC_ID_PCM_S16LE:
235 #endif /* WORDS_BIGENDIAN */
236 case CODEC_ID_PCM_U8:
237 memcpy(dst, samples, n*sample_size);
238 dst += n*sample_size;
240 case CODEC_ID_PCM_ZORK:
248 case CODEC_ID_PCM_ALAW:
251 *dst++ = linear_to_alaw[(v + 32768) >> 2];
254 case CODEC_ID_PCM_MULAW:
257 *dst++ = linear_to_ulaw[(v + 32768) >> 2];
263 //avctx->frame_size = (dst - frame) / (sample_size * avctx->channels);
268 typedef struct PCMDecode {
272 static av_cold int pcm_decode_init(AVCodecContext * avctx)
274 PCMDecode *s = avctx->priv_data;
277 switch(avctx->codec->id) {
278 case CODEC_ID_PCM_ALAW:
280 s->table[i] = alaw2linear(i);
282 case CODEC_ID_PCM_MULAW:
284 s->table[i] = ulaw2linear(i);
290 avctx->sample_fmt = avctx->codec->sample_fmts[0];
295 * Read PCM samples macro
296 * @param type Datatype of native machine format
297 * @param endian bytestream_get_xxx() endian suffix
298 * @param src Source pointer (variable name)
299 * @param dst Destination pointer (variable name)
300 * @param n Total number of samples (variable name)
301 * @param shift Bitshift (bits)
302 * @param offset Sample value offset
304 #define DECODE(type, endian, src, dst, n, shift, offset) \
305 dst_##type = (type*)dst; \
307 register type v = bytestream_get_##endian(&src); \
308 *dst_##type++ = (v - offset) << shift; \
310 dst = (short*)dst_##type;
312 static int pcm_decode_frame(AVCodecContext *avctx,
313 void *data, int *data_size,
314 const uint8_t *buf, int buf_size)
316 PCMDecode *s = avctx->priv_data;
317 int sample_size, c, n;
319 const uint8_t *src, *src2[MAX_CHANNELS];
321 int16_t *dst_int16_t;
322 int32_t *dst_int32_t;
323 uint16_t *dst_uint16_t;
324 uint32_t *dst_uint32_t;
329 if (avctx->sample_fmt!=avctx->codec->sample_fmts[0]) {
330 av_log(avctx, AV_LOG_ERROR, "invalid sample_fmt\n");
334 if(avctx->channels <= 0 || avctx->channels > MAX_CHANNELS){
335 av_log(avctx, AV_LOG_ERROR, "PCM channels out of bounds\n");
339 sample_size = av_get_bits_per_sample(avctx->codec_id)/8;
341 n = avctx->channels * sample_size;
342 /* av_get_bits_per_sample returns 0 for CODEC_ID_PCM_DVD */
343 if (CODEC_ID_PCM_DVD == avctx->codec_id)
344 /* 2 samples are interleaved per block in PCM_DVD */
345 n = 2 * avctx->channels * avctx->bits_per_sample/8;
347 if(n && buf_size % n){
348 av_log(avctx, AV_LOG_ERROR, "invalid PCM packet\n");
352 buf_size= FFMIN(buf_size, *data_size/2);
355 n = buf_size/sample_size;
357 switch(avctx->codec->id) {
358 case CODEC_ID_PCM_U32LE:
359 DECODE(uint32_t, le32, src, samples, n, 0, 0x80000000)
361 case CODEC_ID_PCM_U32BE:
362 DECODE(uint32_t, be32, src, samples, n, 0, 0x80000000)
364 case CODEC_ID_PCM_S24LE:
365 DECODE(int32_t, le24, src, samples, n, 8, 0)
367 case CODEC_ID_PCM_S24BE:
368 DECODE(int32_t, be24, src, samples, n, 8, 0)
370 case CODEC_ID_PCM_U24LE:
371 DECODE(uint32_t, le24, src, samples, n, 8, 0x800000)
373 case CODEC_ID_PCM_U24BE:
374 DECODE(uint32_t, be24, src, samples, n, 8, 0x800000)
376 case CODEC_ID_PCM_S24DAUD:
378 uint32_t v = bytestream_get_be24(&src);
379 v >>= 4; // sync flags are here
380 *samples++ = ff_reverse[(v >> 8) & 0xff] +
381 (ff_reverse[v & 0xff] << 8);
384 case CODEC_ID_PCM_S16LE_PLANAR:
385 n /= avctx->channels;
386 for(c=0;c<avctx->channels;c++)
389 for(c=0;c<avctx->channels;c++)
390 *samples++ = bytestream_get_le16(&src2[c]);
391 src = src2[avctx->channels-1];
393 case CODEC_ID_PCM_U16LE:
394 DECODE(uint16_t, le16, src, samples, n, 0, 0x8000)
396 case CODEC_ID_PCM_U16BE:
397 DECODE(uint16_t, be16, src, samples, n, 0, 0x8000)
399 case CODEC_ID_PCM_S8:
400 dstu8= (uint8_t*)samples;
402 *dstu8++ = *src++ + 128;
404 samples= (short*)dstu8;
407 case CODEC_ID_PCM_S32LE:
408 DECODE(int32_t, le32, src, samples, n, 0, 0)
410 case CODEC_ID_PCM_S16LE:
411 DECODE(int16_t, le16, src, samples, n, 0, 0)
413 case CODEC_ID_PCM_F32BE:
414 case CODEC_ID_PCM_S32BE:
415 case CODEC_ID_PCM_S16BE:
417 case CODEC_ID_PCM_F32BE:
418 case CODEC_ID_PCM_S32BE:
419 DECODE(int32_t, be32, src, samples, n, 0, 0)
421 case CODEC_ID_PCM_S16BE:
422 DECODE(int16_t, be16, src, samples, n, 0, 0)
424 case CODEC_ID_PCM_S32LE:
425 case CODEC_ID_PCM_S16LE:
426 #endif /* WORDS_BIGENDIAN */
427 case CODEC_ID_PCM_U8:
428 memcpy(samples, src, n*sample_size);
429 src += n*sample_size;
430 samples = (short*)((uint8_t*)data + n*sample_size);
432 case CODEC_ID_PCM_ZORK:
440 case CODEC_ID_PCM_ALAW:
441 case CODEC_ID_PCM_MULAW:
443 *samples++ = s->table[*src++];
446 case CODEC_ID_PCM_DVD:
447 if(avctx->bits_per_sample != 20 && avctx->bits_per_sample != 24) {
448 av_log(avctx, AV_LOG_ERROR, "PCM DVD unsupported sample depth\n");
451 int jump = avctx->channels * (avctx->bits_per_sample-16) / 4;
452 n = buf_size / (avctx->channels * 2 * avctx->bits_per_sample / 8);
454 for (c=0; c < 2*avctx->channels; c++)
455 *samples++ = bytestream_get_be16(&src);
463 *data_size = (uint8_t *)samples - (uint8_t *)data;
467 #ifdef CONFIG_ENCODERS
468 #define PCM_ENCODER(id,sample_fmt_,name,long_name_) \
469 AVCodec name ## _encoder = { \
478 .sample_fmts = (enum SampleFormat[]){sample_fmt_,SAMPLE_FMT_NONE}, \
479 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
482 #define PCM_ENCODER(id,sample_fmt_,name,long_name_)
485 #ifdef CONFIG_DECODERS
486 #define PCM_DECODER(id,sample_fmt_,name,long_name_) \
487 AVCodec name ## _decoder = { \
496 .sample_fmts = (enum SampleFormat[]){sample_fmt_,SAMPLE_FMT_NONE}, \
497 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
500 #define PCM_DECODER(id,sample_fmt_,name,long_name_)
503 #define PCM_CODEC(id, sample_fmt_, name, long_name_) \
504 PCM_ENCODER(id,sample_fmt_,name,long_name_) PCM_DECODER(id,sample_fmt_,name,long_name_)
506 /* Note: Do not forget to add new entries to the Makefile as well. */
507 PCM_CODEC (CODEC_ID_PCM_ALAW, SAMPLE_FMT_S16, pcm_alaw, "A-law PCM");
508 PCM_CODEC (CODEC_ID_PCM_DVD, SAMPLE_FMT_S16, pcm_dvd, "signed 16|20|24-bit big-endian PCM");
509 PCM_CODEC (CODEC_ID_PCM_F32BE, SAMPLE_FMT_FLT, pcm_f32be, "32-bit floating point big-endian PCM");
510 PCM_CODEC (CODEC_ID_PCM_MULAW, SAMPLE_FMT_S16, pcm_mulaw, "mu-law PCM");
511 PCM_CODEC (CODEC_ID_PCM_S8, SAMPLE_FMT_U8, pcm_s8, "signed 8-bit PCM");
512 PCM_CODEC (CODEC_ID_PCM_S16BE, SAMPLE_FMT_S16, pcm_s16be, "signed 16-bit big-endian PCM");
513 PCM_CODEC (CODEC_ID_PCM_S16LE, SAMPLE_FMT_S16, pcm_s16le, "signed 16-bit little-endian PCM");
514 PCM_DECODER(CODEC_ID_PCM_S16LE_PLANAR, SAMPLE_FMT_S16, pcm_s16le_planar, "16-bit little-endian planar PCM");
515 PCM_CODEC (CODEC_ID_PCM_S24BE, SAMPLE_FMT_S32, pcm_s24be, "signed 24-bit big-endian PCM");
516 PCM_CODEC (CODEC_ID_PCM_S24DAUD, SAMPLE_FMT_S16, pcm_s24daud, "D-Cinema audio signed 24-bit PCM");
517 PCM_CODEC (CODEC_ID_PCM_S24LE, SAMPLE_FMT_S32, pcm_s24le, "signed 24-bit little-endian PCM");
518 PCM_CODEC (CODEC_ID_PCM_S32BE, SAMPLE_FMT_S32, pcm_s32be, "signed 32-bit big-endian PCM");
519 PCM_CODEC (CODEC_ID_PCM_S32LE, SAMPLE_FMT_S32, pcm_s32le, "signed 32-bit little-endian PCM");
520 PCM_CODEC (CODEC_ID_PCM_U8, SAMPLE_FMT_U8, pcm_u8, "unsigned 8-bit PCM");
521 PCM_CODEC (CODEC_ID_PCM_U16BE, SAMPLE_FMT_S16, pcm_u16be, "unsigned 16-bit big-endian PCM");
522 PCM_CODEC (CODEC_ID_PCM_U16LE, SAMPLE_FMT_S16, pcm_u16le, "unsigned 16-bit little-endian PCM");
523 PCM_CODEC (CODEC_ID_PCM_U24BE, SAMPLE_FMT_S32, pcm_u24be, "unsigned 24-bit big-endian PCM");
524 PCM_CODEC (CODEC_ID_PCM_U24LE, SAMPLE_FMT_S32, pcm_u24le, "unsigned 24-bit little-endian PCM");
525 PCM_CODEC (CODEC_ID_PCM_U32BE, SAMPLE_FMT_S32, pcm_u32be, "unsigned 32-bit big-endian PCM");
526 PCM_CODEC (CODEC_ID_PCM_U32LE, SAMPLE_FMT_S32, pcm_u32le, "unsigned 32-bit little-endian PCM");
527 PCM_CODEC (CODEC_ID_PCM_ZORK, SAMPLE_FMT_S16, pcm_zork, "Zork PCM");