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.

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