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.

512 lines
16KB

  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. switch(avctx->codec->id) {
  93. case CODEC_ID_PCM_ALAW:
  94. build_xlaw_table(linear_to_alaw, alaw2linear, 0xd5);
  95. break;
  96. case CODEC_ID_PCM_MULAW:
  97. build_xlaw_table(linear_to_ulaw, ulaw2linear, 0xff);
  98. break;
  99. default:
  100. break;
  101. }
  102. avctx->block_align = avctx->channels * av_get_bits_per_sample(avctx->codec->id)/8;
  103. avctx->coded_frame= avcodec_alloc_frame();
  104. avctx->coded_frame->key_frame= 1;
  105. return 0;
  106. }
  107. static av_cold int pcm_encode_close(AVCodecContext *avctx)
  108. {
  109. av_freep(&avctx->coded_frame);
  110. return 0;
  111. }
  112. /**
  113. * Write PCM samples macro
  114. * @param type Datatype of native machine format
  115. * @param endian bytestream_put_xxx() suffix
  116. * @param src Source pointer (variable name)
  117. * @param dst Destination pointer (variable name)
  118. * @param n Total number of samples (variable name)
  119. * @param shift Bitshift (bits)
  120. * @param offset Sample value offset
  121. */
  122. #define ENCODE(type, endian, src, dst, n, shift, offset) \
  123. samples_##type = (type*)src; \
  124. for(;n>0;n--) { \
  125. register type v = (*samples_##type++ >> shift) + offset; \
  126. bytestream_put_##endian(&dst, v); \
  127. }
  128. static int pcm_encode_frame(AVCodecContext *avctx,
  129. unsigned char *frame, int buf_size, void *data)
  130. {
  131. int n, sample_size, v;
  132. short *samples;
  133. unsigned char *dst;
  134. uint8_t *srcu8;
  135. int16_t *samples_int16_t;
  136. int32_t *samples_int32_t;
  137. uint16_t *samples_uint16_t;
  138. uint32_t *samples_uint32_t;
  139. sample_size = av_get_bits_per_sample(avctx->codec->id)/8;
  140. n = buf_size / sample_size;
  141. samples = data;
  142. dst = frame;
  143. if (avctx->sample_fmt!=avctx->codec->sample_fmts[0]) {
  144. av_log(avctx, AV_LOG_ERROR, "invalid sample_fmt\n");
  145. return -1;
  146. }
  147. switch(avctx->codec->id) {
  148. case CODEC_ID_PCM_F32BE:
  149. case CODEC_ID_PCM_S32BE:
  150. ENCODE(int32_t, be32, samples, dst, n, 0, 0)
  151. break;
  152. case CODEC_ID_PCM_S32LE:
  153. ENCODE(int32_t, le32, samples, dst, n, 0, 0)
  154. break;
  155. case CODEC_ID_PCM_U32LE:
  156. ENCODE(uint32_t, le32, samples, dst, n, 0, 0x80000000)
  157. break;
  158. case CODEC_ID_PCM_U32BE:
  159. ENCODE(uint32_t, be32, samples, dst, n, 0, 0x80000000)
  160. break;
  161. case CODEC_ID_PCM_S24LE:
  162. ENCODE(int32_t, le24, samples, dst, n, 8, 0)
  163. break;
  164. case CODEC_ID_PCM_S24BE:
  165. ENCODE(int32_t, be24, samples, dst, n, 8, 0)
  166. break;
  167. case CODEC_ID_PCM_U24LE:
  168. ENCODE(uint32_t, le24, samples, dst, n, 8, 0x800000)
  169. break;
  170. case CODEC_ID_PCM_U24BE:
  171. ENCODE(uint32_t, be24, samples, dst, n, 8, 0x800000)
  172. break;
  173. case CODEC_ID_PCM_S24DAUD:
  174. for(;n>0;n--) {
  175. uint32_t tmp = ff_reverse[(*samples >> 8) & 0xff] +
  176. (ff_reverse[*samples & 0xff] << 8);
  177. tmp <<= 4; // sync flags would go here
  178. bytestream_put_be24(&dst, tmp);
  179. samples++;
  180. }
  181. break;
  182. case CODEC_ID_PCM_S16LE:
  183. ENCODE(int16_t, le16, samples, dst, n, 0, 0)
  184. break;
  185. case CODEC_ID_PCM_S16BE:
  186. ENCODE(int16_t, be16, samples, dst, n, 0, 0)
  187. break;
  188. case CODEC_ID_PCM_U16LE:
  189. ENCODE(uint16_t, le16, samples, dst, n, 0, 0x8000)
  190. break;
  191. case CODEC_ID_PCM_U16BE:
  192. ENCODE(uint16_t, be16, samples, dst, n, 0, 0x8000)
  193. break;
  194. case CODEC_ID_PCM_S8:
  195. srcu8= data;
  196. for(;n>0;n--) {
  197. v = *srcu8++;
  198. *dst++ = v - 128;
  199. }
  200. break;
  201. case CODEC_ID_PCM_U8:
  202. memcpy(dst, samples, n);
  203. dst += n;
  204. break;
  205. case CODEC_ID_PCM_ZORK:
  206. for(;n>0;n--) {
  207. v= *samples++ >> 8;
  208. if(v<0) v = -v;
  209. else v+= 128;
  210. *dst++ = v;
  211. }
  212. break;
  213. case CODEC_ID_PCM_ALAW:
  214. for(;n>0;n--) {
  215. v = *samples++;
  216. *dst++ = linear_to_alaw[(v + 32768) >> 2];
  217. }
  218. break;
  219. case CODEC_ID_PCM_MULAW:
  220. for(;n>0;n--) {
  221. v = *samples++;
  222. *dst++ = linear_to_ulaw[(v + 32768) >> 2];
  223. }
  224. break;
  225. default:
  226. return -1;
  227. }
  228. //avctx->frame_size = (dst - frame) / (sample_size * avctx->channels);
  229. return dst - frame;
  230. }
  231. typedef struct PCMDecode {
  232. short table[256];
  233. } PCMDecode;
  234. static av_cold int pcm_decode_init(AVCodecContext * avctx)
  235. {
  236. PCMDecode *s = avctx->priv_data;
  237. int i;
  238. switch(avctx->codec->id) {
  239. case CODEC_ID_PCM_ALAW:
  240. for(i=0;i<256;i++)
  241. s->table[i] = alaw2linear(i);
  242. break;
  243. case CODEC_ID_PCM_MULAW:
  244. for(i=0;i<256;i++)
  245. s->table[i] = ulaw2linear(i);
  246. break;
  247. default:
  248. break;
  249. }
  250. avctx->sample_fmt = avctx->codec->sample_fmts[0];
  251. return 0;
  252. }
  253. /**
  254. * Read PCM samples macro
  255. * @param type Datatype of native machine format
  256. * @param endian bytestream_get_xxx() endian suffix
  257. * @param src Source pointer (variable name)
  258. * @param dst Destination pointer (variable name)
  259. * @param n Total number of samples (variable name)
  260. * @param shift Bitshift (bits)
  261. * @param offset Sample value offset
  262. */
  263. #define DECODE(type, endian, src, dst, n, shift, offset) \
  264. dst_##type = (type*)dst; \
  265. for(;n>0;n--) { \
  266. register type v = bytestream_get_##endian(&src); \
  267. *dst_##type++ = (v - offset) << shift; \
  268. } \
  269. dst = (short*)dst_##type;
  270. static int pcm_decode_frame(AVCodecContext *avctx,
  271. void *data, int *data_size,
  272. const uint8_t *buf, int buf_size)
  273. {
  274. PCMDecode *s = avctx->priv_data;
  275. int sample_size, c, n;
  276. short *samples;
  277. const uint8_t *src, *src2[MAX_CHANNELS];
  278. uint8_t *dstu8;
  279. int16_t *dst_int16_t;
  280. int32_t *dst_int32_t;
  281. uint16_t *dst_uint16_t;
  282. uint32_t *dst_uint32_t;
  283. samples = data;
  284. src = buf;
  285. if (avctx->sample_fmt!=avctx->codec->sample_fmts[0]) {
  286. av_log(avctx, AV_LOG_ERROR, "invalid sample_fmt\n");
  287. return -1;
  288. }
  289. if(avctx->channels <= 0 || avctx->channels > MAX_CHANNELS){
  290. av_log(avctx, AV_LOG_ERROR, "PCM channels out of bounds\n");
  291. return -1;
  292. }
  293. sample_size = av_get_bits_per_sample(avctx->codec_id)/8;
  294. n = avctx->channels * sample_size;
  295. /* av_get_bits_per_sample returns 0 for CODEC_ID_PCM_DVD */
  296. if (CODEC_ID_PCM_DVD == avctx->codec_id)
  297. /* 2 samples are interleaved per block in PCM_DVD */
  298. n = 2 * avctx->channels * avctx->bits_per_sample/8;
  299. if(n && buf_size % n){
  300. av_log(avctx, AV_LOG_ERROR, "invalid PCM packet\n");
  301. return -1;
  302. }
  303. buf_size= FFMIN(buf_size, *data_size/2);
  304. *data_size=0;
  305. n = buf_size/sample_size;
  306. switch(avctx->codec->id) {
  307. case CODEC_ID_PCM_F32BE:
  308. case CODEC_ID_PCM_S32BE:
  309. DECODE(int32_t, be32, src, samples, n, 0, 0)
  310. break;
  311. case CODEC_ID_PCM_S32LE:
  312. DECODE(int32_t, le32, src, samples, n, 0, 0)
  313. break;
  314. case CODEC_ID_PCM_U32LE:
  315. DECODE(uint32_t, le32, src, samples, n, 0, 0x80000000)
  316. break;
  317. case CODEC_ID_PCM_U32BE:
  318. DECODE(uint32_t, be32, src, samples, n, 0, 0x80000000)
  319. break;
  320. case CODEC_ID_PCM_S24LE:
  321. DECODE(int32_t, le24, src, samples, n, 8, 0)
  322. break;
  323. case CODEC_ID_PCM_S24BE:
  324. DECODE(int32_t, be24, src, samples, n, 8, 0)
  325. break;
  326. case CODEC_ID_PCM_U24LE:
  327. DECODE(uint32_t, le24, src, samples, n, 8, 0x800000)
  328. break;
  329. case CODEC_ID_PCM_U24BE:
  330. DECODE(uint32_t, be24, src, samples, n, 8, 0x800000)
  331. break;
  332. case CODEC_ID_PCM_S24DAUD:
  333. for(;n>0;n--) {
  334. uint32_t v = bytestream_get_be24(&src);
  335. v >>= 4; // sync flags are here
  336. *samples++ = ff_reverse[(v >> 8) & 0xff] +
  337. (ff_reverse[v & 0xff] << 8);
  338. }
  339. break;
  340. case CODEC_ID_PCM_S16LE:
  341. DECODE(int16_t, le16, src, samples, n, 0, 0)
  342. break;
  343. case CODEC_ID_PCM_S16LE_PLANAR:
  344. n /= avctx->channels;
  345. for(c=0;c<avctx->channels;c++)
  346. src2[c] = &src[c*n];
  347. for(n>>=1;n>0;n--)
  348. for(c=0;c<avctx->channels;c++)
  349. *samples++ = bytestream_get_le16(&src2[c]);
  350. src = src2[avctx->channels-1];
  351. break;
  352. case CODEC_ID_PCM_S16BE:
  353. DECODE(int16_t, be16, src, samples, n, 0, 0)
  354. break;
  355. case CODEC_ID_PCM_U16LE:
  356. DECODE(uint16_t, le16, src, samples, n, 0, 0x8000)
  357. break;
  358. case CODEC_ID_PCM_U16BE:
  359. DECODE(uint16_t, be16, src, samples, n, 0, 0x8000)
  360. break;
  361. case CODEC_ID_PCM_S8:
  362. dstu8= (uint8_t*)samples;
  363. for(;n>0;n--) {
  364. *dstu8++ = *src++ + 128;
  365. }
  366. samples= (short*)dstu8;
  367. break;
  368. case CODEC_ID_PCM_U8:
  369. memcpy(samples, src, n);
  370. src += n;
  371. samples = (short*)((uint8_t*)data + n);
  372. break;
  373. case CODEC_ID_PCM_ZORK:
  374. for(;n>0;n--) {
  375. int x= *src++;
  376. if(x&128) x-= 128;
  377. else x = -x;
  378. *samples++ = x << 8;
  379. }
  380. break;
  381. case CODEC_ID_PCM_ALAW:
  382. case CODEC_ID_PCM_MULAW:
  383. for(;n>0;n--) {
  384. *samples++ = s->table[*src++];
  385. }
  386. break;
  387. case CODEC_ID_PCM_DVD:
  388. if(avctx->bits_per_sample != 20 && avctx->bits_per_sample != 24) {
  389. av_log(avctx, AV_LOG_ERROR, "PCM DVD unsupported sample depth\n");
  390. return -1;
  391. } else {
  392. int jump = avctx->channels * (avctx->bits_per_sample-16) / 4;
  393. n = buf_size / (avctx->channels * 2 * avctx->bits_per_sample / 8);
  394. while (n--) {
  395. for (c=0; c < 2*avctx->channels; c++)
  396. *samples++ = bytestream_get_be16(&src);
  397. src += jump;
  398. }
  399. }
  400. break;
  401. default:
  402. return -1;
  403. }
  404. *data_size = (uint8_t *)samples - (uint8_t *)data;
  405. return src - buf;
  406. }
  407. #ifdef CONFIG_ENCODERS
  408. #define PCM_ENCODER(id,sample_fmt_,name,long_name_) \
  409. AVCodec name ## _encoder = { \
  410. #name, \
  411. CODEC_TYPE_AUDIO, \
  412. id, \
  413. 0, \
  414. pcm_encode_init, \
  415. pcm_encode_frame, \
  416. pcm_encode_close, \
  417. NULL, \
  418. .sample_fmts = (enum SampleFormat[]){sample_fmt_,SAMPLE_FMT_NONE}, \
  419. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  420. };
  421. #else
  422. #define PCM_ENCODER(id,sample_fmt_,name,long_name_)
  423. #endif
  424. #ifdef CONFIG_DECODERS
  425. #define PCM_DECODER(id,sample_fmt_,name,long_name_) \
  426. AVCodec name ## _decoder = { \
  427. #name, \
  428. CODEC_TYPE_AUDIO, \
  429. id, \
  430. sizeof(PCMDecode), \
  431. pcm_decode_init, \
  432. NULL, \
  433. NULL, \
  434. pcm_decode_frame, \
  435. .sample_fmts = (enum SampleFormat[]){sample_fmt_,SAMPLE_FMT_NONE}, \
  436. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  437. };
  438. #else
  439. #define PCM_DECODER(id,sample_fmt_,name,long_name_)
  440. #endif
  441. #define PCM_CODEC(id, sample_fmt_, name, long_name_) \
  442. PCM_ENCODER(id,sample_fmt_,name,long_name_) PCM_DECODER(id,sample_fmt_,name,long_name_)
  443. /* Note: Do not forget to add new entries to the Makefile as well. */
  444. PCM_CODEC (CODEC_ID_PCM_ALAW, SAMPLE_FMT_S16, pcm_alaw, "A-law PCM");
  445. PCM_CODEC (CODEC_ID_PCM_DVD, SAMPLE_FMT_S16, pcm_dvd, "signed 16|20|24-bit big-endian PCM");
  446. PCM_CODEC (CODEC_ID_PCM_F32BE, SAMPLE_FMT_FLT, pcm_f32be, "32-bit floating point big-endian PCM");
  447. PCM_CODEC (CODEC_ID_PCM_MULAW, SAMPLE_FMT_S16, pcm_mulaw, "mu-law PCM");
  448. PCM_CODEC (CODEC_ID_PCM_S8, SAMPLE_FMT_U8, pcm_s8, "signed 8-bit PCM");
  449. PCM_CODEC (CODEC_ID_PCM_S16BE, SAMPLE_FMT_S16, pcm_s16be, "signed 16-bit big-endian PCM");
  450. PCM_CODEC (CODEC_ID_PCM_S16LE, SAMPLE_FMT_S16, pcm_s16le, "signed 16-bit little-endian PCM");
  451. PCM_DECODER(CODEC_ID_PCM_S16LE_PLANAR, SAMPLE_FMT_S16, pcm_s16le_planar, "16-bit little-endian planar PCM");
  452. PCM_CODEC (CODEC_ID_PCM_S24BE, SAMPLE_FMT_S32, pcm_s24be, "signed 24-bit big-endian PCM");
  453. PCM_CODEC (CODEC_ID_PCM_S24DAUD, SAMPLE_FMT_S16, pcm_s24daud, "D-Cinema audio signed 24-bit PCM");
  454. PCM_CODEC (CODEC_ID_PCM_S24LE, SAMPLE_FMT_S32, pcm_s24le, "signed 24-bit little-endian PCM");
  455. PCM_CODEC (CODEC_ID_PCM_S32BE, SAMPLE_FMT_S32, pcm_s32be, "signed 32-bit big-endian PCM");
  456. PCM_CODEC (CODEC_ID_PCM_S32LE, SAMPLE_FMT_S32, pcm_s32le, "signed 32-bit little-endian PCM");
  457. PCM_CODEC (CODEC_ID_PCM_U8, SAMPLE_FMT_U8, pcm_u8, "unsigned 8-bit PCM");
  458. PCM_CODEC (CODEC_ID_PCM_U16BE, SAMPLE_FMT_S16, pcm_u16be, "unsigned 16-bit big-endian PCM");
  459. PCM_CODEC (CODEC_ID_PCM_U16LE, SAMPLE_FMT_S16, pcm_u16le, "unsigned 16-bit little-endian PCM");
  460. PCM_CODEC (CODEC_ID_PCM_U24BE, SAMPLE_FMT_S32, pcm_u24be, "unsigned 24-bit big-endian PCM");
  461. PCM_CODEC (CODEC_ID_PCM_U24LE, SAMPLE_FMT_S32, pcm_u24le, "unsigned 24-bit little-endian PCM");
  462. PCM_CODEC (CODEC_ID_PCM_U32BE, SAMPLE_FMT_S32, pcm_u32be, "unsigned 32-bit big-endian PCM");
  463. PCM_CODEC (CODEC_ID_PCM_U32LE, SAMPLE_FMT_S32, pcm_u32le, "unsigned 32-bit little-endian PCM");
  464. PCM_CODEC (CODEC_ID_PCM_ZORK, SAMPLE_FMT_S16, pcm_zork, "Zork PCM");