You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

555 lines
17KB

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