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.

510 lines
14KB

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