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.

614 lines
18KB

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