Simplify PCM codec; replace switch() statements with av_get_bits_per_sample().
[libav.git] / libavcodec / pcm.c
1 /*
2 * PCM codecs
3 * Copyright (c) 2001 Fabrice Bellard.
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 /**
23 * @file pcm.c
24 * PCM codecs
25 */
26
27 #include "avcodec.h"
28 #include "bitstream.h" // for ff_reverse
29 #include "bytestream.h"
30
31 #define MAX_CHANNELS 64
32
33 /* from g711.c by SUN microsystems (unrestricted use) */
34
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. */
40
41 #define BIAS (0x84) /* Bias for linear code. */
42
43 /*
44 * alaw2linear() - Convert an A-law value to 16-bit linear PCM
45 *
46 */
47 static av_cold int alaw2linear(unsigned char a_val)
48 {
49 int t;
50 int seg;
51
52 a_val ^= 0x55;
53
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;
58
59 return (a_val & SIGN_BIT) ? t : -t;
60 }
61
62 static av_cold int ulaw2linear(unsigned char u_val)
63 {
64 int t;
65
66 /* Complement to obtain normal u-law value. */
67 u_val = ~u_val;
68
69 /*
70 * Extract and bias the quantization bits. Then
71 * shift up by the segment number and subtract out the bias.
72 */
73 t = ((u_val & QUANT_MASK) << 3) + BIAS;
74 t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
75
76 return (u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS);
77 }
78
79 /* 16384 entries per table */
80 static uint8_t linear_to_alaw[16384];
81 static uint8_t linear_to_ulaw[16384];
82
83 static av_cold void build_xlaw_table(uint8_t *linear_to_xlaw,
84 int (*xlaw2linear)(unsigned char),
85 int mask)
86 {
87 int i, j, v, v1, v2;
88
89 j = 0;
90 for(i=0;i<128;i++) {
91 if (i != 127) {
92 v1 = xlaw2linear(i ^ mask);
93 v2 = xlaw2linear((i + 1) ^ mask);
94 v = (v1 + v2 + 4) >> 3;
95 } else {
96 v = 8192;
97 }
98 for(;j<v;j++) {
99 linear_to_xlaw[8192 + j] = (i ^ mask);
100 if (j > 0)
101 linear_to_xlaw[8192 - j] = (i ^ (mask ^ 0x80));
102 }
103 }
104 linear_to_xlaw[0] = linear_to_xlaw[1];
105 }
106
107 static av_cold int pcm_encode_init(AVCodecContext *avctx)
108 {
109 avctx->frame_size = 1;
110 if (avctx->codec->id==CODEC_ID_PCM_F32BE && avctx->sample_fmt!=SAMPLE_FMT_FLT) {
111 return -1;
112 }
113 switch(avctx->codec->id) {
114 case CODEC_ID_PCM_ALAW:
115 build_xlaw_table(linear_to_alaw, alaw2linear, 0xd5);
116 break;
117 case CODEC_ID_PCM_MULAW:
118 build_xlaw_table(linear_to_ulaw, ulaw2linear, 0xff);
119 break;
120 default:
121 break;
122 }
123
124 avctx->block_align = avctx->channels * av_get_bits_per_sample(avctx->codec->id)/8;
125 avctx->coded_frame= avcodec_alloc_frame();
126 avctx->coded_frame->key_frame= 1;
127
128 return 0;
129 }
130
131 static av_cold int pcm_encode_close(AVCodecContext *avctx)
132 {
133 av_freep(&avctx->coded_frame);
134
135 return 0;
136 }
137
138 /**
139 * \brief convert samples from 16 bit
140 * \param bps byte per sample for the destination format, must be >= 2
141 * \param le 0 for big-, 1 for little-endian
142 * \param us 0 for signed, 1 for unsigned output
143 * \param samples input samples
144 * \param dst output samples
145 * \param n number of samples in samples buffer.
146 */
147 static inline void encode_from16(int bps, int le, int us,
148 short **samples, uint8_t **dst, int n) {
149 int usum = us ? 0x8000 : 0;
150 if (bps > 2)
151 memset(*dst, 0, n * bps);
152 if (le) *dst += bps - 2;
153 for(;n>0;n--) {
154 register int v = *(*samples)++;
155 v += usum;
156 if (le) AV_WL16(*dst, v);
157 else AV_WB16(*dst, v);
158 *dst += bps;
159 }
160 if (le) *dst -= bps - 2;
161 }
162
163 static int pcm_encode_frame(AVCodecContext *avctx,
164 unsigned char *frame, int buf_size, void *data)
165 {
166 int n, sample_size, v;
167 short *samples;
168 unsigned char *dst;
169
170 sample_size = av_get_bits_per_sample(avctx->codec->id)/8;
171 n = buf_size / sample_size;
172 samples = data;
173 dst = frame;
174
175 switch(avctx->codec->id) {
176 case CODEC_ID_PCM_F32BE:
177 {
178 float *fsamples = data;
179 for(;n>0;n--) {
180 float fv = *fsamples++;
181 bytestream_put_be32(&dst, av_flt2int(fv));
182 }
183 samples = (void*)fsamples;
184 }
185 break;
186 case CODEC_ID_PCM_S32LE:
187 encode_from16(4, 1, 0, &samples, &dst, n);
188 break;
189 case CODEC_ID_PCM_S32BE:
190 encode_from16(4, 0, 0, &samples, &dst, n);
191 break;
192 case CODEC_ID_PCM_U32LE:
193 encode_from16(4, 1, 1, &samples, &dst, n);
194 break;
195 case CODEC_ID_PCM_U32BE:
196 encode_from16(4, 0, 1, &samples, &dst, n);
197 break;
198 case CODEC_ID_PCM_S24LE:
199 encode_from16(3, 1, 0, &samples, &dst, n);
200 break;
201 case CODEC_ID_PCM_S24BE:
202 encode_from16(3, 0, 0, &samples, &dst, n);
203 break;
204 case CODEC_ID_PCM_U24LE:
205 encode_from16(3, 1, 1, &samples, &dst, n);
206 break;
207 case CODEC_ID_PCM_U24BE:
208 encode_from16(3, 0, 1, &samples, &dst, n);
209 break;
210 case CODEC_ID_PCM_S24DAUD:
211 for(;n>0;n--) {
212 uint32_t tmp = ff_reverse[*samples >> 8] +
213 (ff_reverse[*samples & 0xff] << 8);
214 tmp <<= 4; // sync flags would go here
215 bytestream_put_be24(&dst, tmp);
216 samples++;
217 }
218 break;
219 case CODEC_ID_PCM_S16LE:
220 for(;n>0;n--) {
221 v = *samples++;
222 bytestream_put_le16(&dst, v);
223 }
224 break;
225 case CODEC_ID_PCM_S16BE:
226 for(;n>0;n--) {
227 v = *samples++;
228 bytestream_put_be16(&dst, v);
229 }
230 break;
231 case CODEC_ID_PCM_U16LE:
232 for(;n>0;n--) {
233 v = *samples++;
234 v += 0x8000;
235 bytestream_put_le16(&dst, v);
236 }
237 break;
238 case CODEC_ID_PCM_U16BE:
239 for(;n>0;n--) {
240 v = *samples++;
241 v += 0x8000;
242 bytestream_put_be16(&dst, v);
243 }
244 break;
245 case CODEC_ID_PCM_S8:
246 for(;n>0;n--) {
247 v = *samples++;
248 *dst++ = v >> 8;
249 }
250 break;
251 case CODEC_ID_PCM_U8:
252 for(;n>0;n--) {
253 v = *samples++;
254 *dst++ = (v >> 8) + 128;
255 }
256 break;
257 case CODEC_ID_PCM_ZORK:
258 for(;n>0;n--) {
259 v= *samples++ >> 8;
260 if(v<0) v = -v;
261 else v+= 128;
262 *dst++ = v;
263 }
264 break;
265 case CODEC_ID_PCM_ALAW:
266 for(;n>0;n--) {
267 v = *samples++;
268 *dst++ = linear_to_alaw[(v + 32768) >> 2];
269 }
270 break;
271 case CODEC_ID_PCM_MULAW:
272 for(;n>0;n--) {
273 v = *samples++;
274 *dst++ = linear_to_ulaw[(v + 32768) >> 2];
275 }
276 break;
277 default:
278 return -1;
279 }
280 //avctx->frame_size = (dst - frame) / (sample_size * avctx->channels);
281
282 return dst - frame;
283 }
284
285 typedef struct PCMDecode {
286 short table[256];
287 } PCMDecode;
288
289 static av_cold int pcm_decode_init(AVCodecContext * avctx)
290 {
291 PCMDecode *s = avctx->priv_data;
292 int i;
293
294 switch(avctx->codec->id) {
295 case CODEC_ID_PCM_ALAW:
296 for(i=0;i<256;i++)
297 s->table[i] = alaw2linear(i);
298 break;
299 case CODEC_ID_PCM_MULAW:
300 for(i=0;i<256;i++)
301 s->table[i] = ulaw2linear(i);
302 break;
303 default:
304 break;
305 }
306
307 avctx->sample_fmt = avctx->codec->sample_fmts[0];
308 return 0;
309 }
310
311 /**
312 * \brief convert samples to 16 bit
313 * \param bps byte per sample for the source format, must be >= 2
314 * \param le 0 for big-, 1 for little-endian
315 * \param us 0 for signed, 1 for unsigned input
316 * \param src input samples
317 * \param samples output samples
318 * \param src_len number of bytes in src
319 */
320 static inline void decode_to16(int bps, int le, int us,
321 const uint8_t **src, short **samples, int src_len)
322 {
323 int usum = us ? -0x8000 : 0;
324 register int n = src_len / bps;
325 if (le) *src += bps - 2;
326 for(;n>0;n--) {
327 register int v;
328 if (le) v = AV_RL16(*src);
329 else v = AV_RB16(*src);
330 v += usum;
331 *(*samples)++ = v;
332 *src += bps;
333 }
334 if (le) *src -= bps - 2;
335 }
336
337 static int pcm_decode_frame(AVCodecContext *avctx,
338 void *data, int *data_size,
339 const uint8_t *buf, int buf_size)
340 {
341 PCMDecode *s = avctx->priv_data;
342 int c, n;
343 short *samples;
344 const uint8_t *src, *src2[MAX_CHANNELS];
345
346 samples = data;
347 src = buf;
348
349 if(avctx->channels <= 0 || avctx->channels > MAX_CHANNELS){
350 av_log(avctx, AV_LOG_ERROR, "PCM channels out of bounds\n");
351 return -1;
352 }
353
354 n = avctx->channels * av_get_bits_per_sample(avctx->codec_id)/8;
355 /* av_get_bits_per_sample returns 0 for CODEC_ID_PCM_DVD */
356 if (CODEC_ID_PCM_DVD == avctx->codec_id)
357 /* 2 samples are interleaved per block in PCM_DVD */
358 n = 2 * avctx->channels * avctx->bits_per_sample/8;
359
360 if(n && buf_size % n){
361 av_log(avctx, AV_LOG_ERROR, "invalid PCM packet\n");
362 return -1;
363 }
364
365 buf_size= FFMIN(buf_size, *data_size/2);
366 *data_size=0;
367
368 n = buf_size/avctx->channels;
369 for(c=0;c<avctx->channels;c++)
370 src2[c] = &src[c*n];
371
372 switch(avctx->codec->id) {
373 case CODEC_ID_PCM_F32BE:
374 {
375 float *fsamples = data;
376 n = buf_size >> 2;
377 for(;n>0;n--)
378 *fsamples++ = av_int2flt(bytestream_get_be32(&src));
379 samples = (void*)fsamples;
380 break;
381 }
382 case CODEC_ID_PCM_S32LE:
383 decode_to16(4, 1, 0, &src, &samples, buf_size);
384 break;
385 case CODEC_ID_PCM_S32BE:
386 decode_to16(4, 0, 0, &src, &samples, buf_size);
387 break;
388 case CODEC_ID_PCM_U32LE:
389 decode_to16(4, 1, 1, &src, &samples, buf_size);
390 break;
391 case CODEC_ID_PCM_U32BE:
392 decode_to16(4, 0, 1, &src, &samples, buf_size);
393 break;
394 case CODEC_ID_PCM_S24LE:
395 decode_to16(3, 1, 0, &src, &samples, buf_size);
396 break;
397 case CODEC_ID_PCM_S24BE:
398 decode_to16(3, 0, 0, &src, &samples, buf_size);
399 break;
400 case CODEC_ID_PCM_U24LE:
401 decode_to16(3, 1, 1, &src, &samples, buf_size);
402 break;
403 case CODEC_ID_PCM_U24BE:
404 decode_to16(3, 0, 1, &src, &samples, buf_size);
405 break;
406 case CODEC_ID_PCM_S24DAUD:
407 n = buf_size / 3;
408 for(;n>0;n--) {
409 uint32_t v = bytestream_get_be24(&src);
410 v >>= 4; // sync flags are here
411 *samples++ = ff_reverse[(v >> 8) & 0xff] +
412 (ff_reverse[v & 0xff] << 8);
413 }
414 break;
415 case CODEC_ID_PCM_S16LE:
416 n = buf_size >> 1;
417 for(;n>0;n--) {
418 *samples++ = bytestream_get_le16(&src);
419 }
420 break;
421 case CODEC_ID_PCM_S16LE_PLANAR:
422 for(n>>=1;n>0;n--)
423 for(c=0;c<avctx->channels;c++)
424 *samples++ = bytestream_get_le16(&src2[c]);
425 src = src2[avctx->channels-1];
426 break;
427 case CODEC_ID_PCM_S16BE:
428 n = buf_size >> 1;
429 for(;n>0;n--) {
430 *samples++ = bytestream_get_be16(&src);
431 }
432 break;
433 case CODEC_ID_PCM_U16LE:
434 n = buf_size >> 1;
435 for(;n>0;n--) {
436 *samples++ = bytestream_get_le16(&src) - 0x8000;
437 }
438 break;
439 case CODEC_ID_PCM_U16BE:
440 n = buf_size >> 1;
441 for(;n>0;n--) {
442 *samples++ = bytestream_get_be16(&src) - 0x8000;
443 }
444 break;
445 case CODEC_ID_PCM_S8:
446 n = buf_size;
447 for(;n>0;n--) {
448 *samples++ = *src++ << 8;
449 }
450 break;
451 case CODEC_ID_PCM_U8:
452 n = buf_size;
453 for(;n>0;n--) {
454 *samples++ = ((int)*src++ - 128) << 8;
455 }
456 break;
457 case CODEC_ID_PCM_ZORK:
458 n = buf_size;
459 for(;n>0;n--) {
460 int x= *src++;
461 if(x&128) x-= 128;
462 else x = -x;
463 *samples++ = x << 8;
464 }
465 break;
466 case CODEC_ID_PCM_ALAW:
467 case CODEC_ID_PCM_MULAW:
468 n = buf_size;
469 for(;n>0;n--) {
470 *samples++ = s->table[*src++];
471 }
472 break;
473 case CODEC_ID_PCM_DVD:
474 if(avctx->bits_per_sample != 20 && avctx->bits_per_sample != 24) {
475 av_log(avctx, AV_LOG_ERROR, "PCM DVD unsupported sample depth\n");
476 return -1;
477 } else {
478 int jump = avctx->channels * (avctx->bits_per_sample-16) / 4;
479 n = buf_size / (avctx->channels * 2 * avctx->bits_per_sample / 8);
480 while (n--) {
481 for (c=0; c < 2*avctx->channels; c++)
482 *samples++ = bytestream_get_be16(&src);
483 src += jump;
484 }
485 }
486 break;
487 default:
488 return -1;
489 }
490 *data_size = (uint8_t *)samples - (uint8_t *)data;
491 return src - buf;
492 }
493
494 #ifdef CONFIG_ENCODERS
495 #define PCM_ENCODER(id,sample_fmt_,name,long_name_) \
496 AVCodec name ## _encoder = { \
497 #name, \
498 CODEC_TYPE_AUDIO, \
499 id, \
500 0, \
501 pcm_encode_init, \
502 pcm_encode_frame, \
503 pcm_encode_close, \
504 NULL, \
505 .sample_fmts = (enum SampleFormat[]){sample_fmt_,SAMPLE_FMT_NONE}, \
506 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
507 };
508 #else
509 #define PCM_ENCODER(id,sample_fmt_,name,long_name_)
510 #endif
511
512 #ifdef CONFIG_DECODERS
513 #define PCM_DECODER(id,sample_fmt_,name,long_name_) \
514 AVCodec name ## _decoder = { \
515 #name, \
516 CODEC_TYPE_AUDIO, \
517 id, \
518 sizeof(PCMDecode), \
519 pcm_decode_init, \
520 NULL, \
521 NULL, \
522 pcm_decode_frame, \
523 .sample_fmts = (enum SampleFormat[]){sample_fmt_,SAMPLE_FMT_NONE}, \
524 .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
525 };
526 #else
527 #define PCM_DECODER(id,sample_fmt_,name,long_name_)
528 #endif
529
530 #define PCM_CODEC(id, sample_fmt_, name, long_name_) \
531 PCM_ENCODER(id,sample_fmt_,name,long_name_) PCM_DECODER(id,sample_fmt_,name,long_name_)
532
533 /* Note: Do not forget to add new entries to the Makefile as well. */
534 PCM_CODEC (CODEC_ID_PCM_ALAW, SAMPLE_FMT_S16, pcm_alaw, "A-law PCM");
535 PCM_CODEC (CODEC_ID_PCM_DVD, SAMPLE_FMT_S16, pcm_dvd, "signed 16|20|24-bit big-endian PCM");
536 PCM_CODEC (CODEC_ID_PCM_F32BE, SAMPLE_FMT_FLT, pcm_f32be, "32-bit floating point big-endian PCM");
537 PCM_CODEC (CODEC_ID_PCM_MULAW, SAMPLE_FMT_S16, pcm_mulaw, "mu-law PCM");
538 PCM_CODEC (CODEC_ID_PCM_S8, SAMPLE_FMT_S16, pcm_s8, "signed 8-bit PCM");
539 PCM_CODEC (CODEC_ID_PCM_S16BE, SAMPLE_FMT_S16, pcm_s16be, "signed 16-bit big-endian PCM");
540 PCM_CODEC (CODEC_ID_PCM_S16LE, SAMPLE_FMT_S16, pcm_s16le, "signed 16-bit little-endian PCM");
541 PCM_DECODER(CODEC_ID_PCM_S16LE_PLANAR, SAMPLE_FMT_S16, pcm_s16le_planar, "16-bit little-endian planar PCM");
542 PCM_CODEC (CODEC_ID_PCM_S24BE, SAMPLE_FMT_S16, pcm_s24be, "signed 24-bit big-endian PCM");
543 PCM_CODEC (CODEC_ID_PCM_S24DAUD, SAMPLE_FMT_S16, pcm_s24daud, "D-Cinema audio signed 24-bit PCM");
544 PCM_CODEC (CODEC_ID_PCM_S24LE, SAMPLE_FMT_S16, pcm_s24le, "signed 24-bit little-endian PCM");
545 PCM_CODEC (CODEC_ID_PCM_S32BE, SAMPLE_FMT_S16, pcm_s32be, "signed 32-bit big-endian PCM");
546 PCM_CODEC (CODEC_ID_PCM_S32LE, SAMPLE_FMT_S16, pcm_s32le, "signed 32-bit little-endian PCM");
547 PCM_CODEC (CODEC_ID_PCM_U8, SAMPLE_FMT_S16, pcm_u8, "unsigned 8-bit PCM");
548 PCM_CODEC (CODEC_ID_PCM_U16BE, SAMPLE_FMT_S16, pcm_u16be, "unsigned 16-bit big-endian PCM");
549 PCM_CODEC (CODEC_ID_PCM_U16LE, SAMPLE_FMT_S16, pcm_u16le, "unsigned 16-bit little-endian PCM");
550 PCM_CODEC (CODEC_ID_PCM_U24BE, SAMPLE_FMT_S16, pcm_u24be, "unsigned 24-bit big-endian PCM");
551 PCM_CODEC (CODEC_ID_PCM_U24LE, SAMPLE_FMT_S16, pcm_u24le, "unsigned 24-bit little-endian PCM");
552 PCM_CODEC (CODEC_ID_PCM_U32BE, SAMPLE_FMT_S16, pcm_u32be, "unsigned 32-bit big-endian PCM");
553 PCM_CODEC (CODEC_ID_PCM_U32LE, SAMPLE_FMT_S16, pcm_u32le, "unsigned 32-bit little-endian PCM");
554 PCM_CODEC (CODEC_ID_PCM_ZORK, SAMPLE_FMT_S16, pcm_zork, "Zork PCM");