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.

527 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
  23. * PCM codecs
  24. */
  25. #include "avcodec.h"
  26. #include "libavutil/common.h" /* for av_reverse */
  27. #include "bytestream.h"
  28. #include "pcm_tablegen.h"
  29. #define MAX_CHANNELS 64
  30. static av_cold int pcm_encode_init(AVCodecContext *avctx)
  31. {
  32. avctx->frame_size = 1;
  33. switch(avctx->codec->id) {
  34. case CODEC_ID_PCM_ALAW:
  35. pcm_alaw_tableinit();
  36. break;
  37. case CODEC_ID_PCM_MULAW:
  38. pcm_ulaw_tableinit();
  39. break;
  40. default:
  41. break;
  42. }
  43. avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id);
  44. avctx->block_align = avctx->channels * avctx->bits_per_coded_sample/8;
  45. avctx->coded_frame= avcodec_alloc_frame();
  46. avctx->coded_frame->key_frame= 1;
  47. return 0;
  48. }
  49. static av_cold int pcm_encode_close(AVCodecContext *avctx)
  50. {
  51. av_freep(&avctx->coded_frame);
  52. return 0;
  53. }
  54. /**
  55. * Write PCM samples macro
  56. * @param type Datatype of native machine format
  57. * @param endian bytestream_put_xxx() suffix
  58. * @param src Source pointer (variable name)
  59. * @param dst Destination pointer (variable name)
  60. * @param n Total number of samples (variable name)
  61. * @param shift Bitshift (bits)
  62. * @param offset Sample value offset
  63. */
  64. #define ENCODE(type, endian, src, dst, n, shift, offset) \
  65. samples_##type = (const type*) src; \
  66. for(;n>0;n--) { \
  67. register type v = (*samples_##type++ >> shift) + offset; \
  68. bytestream_put_##endian(&dst, v); \
  69. }
  70. static int pcm_encode_frame(AVCodecContext *avctx,
  71. unsigned char *frame, int buf_size, void *data)
  72. {
  73. int n, sample_size, v;
  74. const short *samples;
  75. unsigned char *dst;
  76. const uint8_t *srcu8;
  77. const int16_t *samples_int16_t;
  78. const int32_t *samples_int32_t;
  79. const int64_t *samples_int64_t;
  80. const uint16_t *samples_uint16_t;
  81. const uint32_t *samples_uint32_t;
  82. sample_size = av_get_bits_per_sample(avctx->codec->id)/8;
  83. n = buf_size / sample_size;
  84. samples = data;
  85. dst = frame;
  86. switch(avctx->codec->id) {
  87. case CODEC_ID_PCM_U32LE:
  88. ENCODE(uint32_t, le32, samples, dst, n, 0, 0x80000000)
  89. break;
  90. case CODEC_ID_PCM_U32BE:
  91. ENCODE(uint32_t, be32, samples, dst, n, 0, 0x80000000)
  92. break;
  93. case CODEC_ID_PCM_S24LE:
  94. ENCODE(int32_t, le24, samples, dst, n, 8, 0)
  95. break;
  96. case CODEC_ID_PCM_S24BE:
  97. ENCODE(int32_t, be24, samples, dst, n, 8, 0)
  98. break;
  99. case CODEC_ID_PCM_U24LE:
  100. ENCODE(uint32_t, le24, samples, dst, n, 8, 0x800000)
  101. break;
  102. case CODEC_ID_PCM_U24BE:
  103. ENCODE(uint32_t, be24, samples, dst, n, 8, 0x800000)
  104. break;
  105. case CODEC_ID_PCM_S24DAUD:
  106. for(;n>0;n--) {
  107. uint32_t tmp = av_reverse[(*samples >> 8) & 0xff] +
  108. (av_reverse[*samples & 0xff] << 8);
  109. tmp <<= 4; // sync flags would go here
  110. bytestream_put_be24(&dst, tmp);
  111. samples++;
  112. }
  113. break;
  114. case CODEC_ID_PCM_U16LE:
  115. ENCODE(uint16_t, le16, samples, dst, n, 0, 0x8000)
  116. break;
  117. case CODEC_ID_PCM_U16BE:
  118. ENCODE(uint16_t, be16, samples, dst, n, 0, 0x8000)
  119. break;
  120. case CODEC_ID_PCM_S8:
  121. srcu8= data;
  122. for(;n>0;n--) {
  123. v = *srcu8++;
  124. *dst++ = v - 128;
  125. }
  126. break;
  127. #if HAVE_BIGENDIAN
  128. case CODEC_ID_PCM_F64LE:
  129. ENCODE(int64_t, le64, samples, dst, n, 0, 0)
  130. break;
  131. case CODEC_ID_PCM_S32LE:
  132. case CODEC_ID_PCM_F32LE:
  133. ENCODE(int32_t, le32, samples, dst, n, 0, 0)
  134. break;
  135. case CODEC_ID_PCM_S16LE:
  136. ENCODE(int16_t, le16, samples, dst, n, 0, 0)
  137. break;
  138. case CODEC_ID_PCM_F64BE:
  139. case CODEC_ID_PCM_F32BE:
  140. case CODEC_ID_PCM_S32BE:
  141. case CODEC_ID_PCM_S16BE:
  142. #else
  143. case CODEC_ID_PCM_F64BE:
  144. ENCODE(int64_t, be64, samples, dst, n, 0, 0)
  145. break;
  146. case CODEC_ID_PCM_F32BE:
  147. case CODEC_ID_PCM_S32BE:
  148. ENCODE(int32_t, be32, samples, dst, n, 0, 0)
  149. break;
  150. case CODEC_ID_PCM_S16BE:
  151. ENCODE(int16_t, be16, samples, dst, n, 0, 0)
  152. break;
  153. case CODEC_ID_PCM_F64LE:
  154. case CODEC_ID_PCM_F32LE:
  155. case CODEC_ID_PCM_S32LE:
  156. case CODEC_ID_PCM_S16LE:
  157. #endif /* HAVE_BIGENDIAN */
  158. case CODEC_ID_PCM_U8:
  159. memcpy(dst, samples, n*sample_size);
  160. dst += n*sample_size;
  161. break;
  162. case CODEC_ID_PCM_ALAW:
  163. for(;n>0;n--) {
  164. v = *samples++;
  165. *dst++ = linear_to_alaw[(v + 32768) >> 2];
  166. }
  167. break;
  168. case CODEC_ID_PCM_MULAW:
  169. for(;n>0;n--) {
  170. v = *samples++;
  171. *dst++ = linear_to_ulaw[(v + 32768) >> 2];
  172. }
  173. break;
  174. default:
  175. return -1;
  176. }
  177. //avctx->frame_size = (dst - frame) / (sample_size * avctx->channels);
  178. return dst - frame;
  179. }
  180. typedef struct PCMDecode {
  181. short table[256];
  182. } PCMDecode;
  183. static av_cold int pcm_decode_init(AVCodecContext * avctx)
  184. {
  185. PCMDecode *s = avctx->priv_data;
  186. int i;
  187. if (avctx->channels <= 0 || avctx->channels > MAX_CHANNELS) {
  188. av_log(avctx, AV_LOG_ERROR, "PCM channels out of bounds\n");
  189. return AVERROR(EINVAL);
  190. }
  191. switch(avctx->codec->id) {
  192. case CODEC_ID_PCM_ALAW:
  193. for(i=0;i<256;i++)
  194. s->table[i] = alaw2linear(i);
  195. break;
  196. case CODEC_ID_PCM_MULAW:
  197. for(i=0;i<256;i++)
  198. s->table[i] = ulaw2linear(i);
  199. break;
  200. default:
  201. break;
  202. }
  203. avctx->sample_fmt = avctx->codec->sample_fmts[0];
  204. if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
  205. avctx->bits_per_raw_sample = av_get_bits_per_sample(avctx->codec->id);
  206. return 0;
  207. }
  208. /**
  209. * Read PCM samples macro
  210. * @param size Data size of native machine format
  211. * @param endian bytestream_get_xxx() endian suffix
  212. * @param src Source pointer (variable name)
  213. * @param dst Destination pointer (variable name)
  214. * @param n Total number of samples (variable name)
  215. * @param shift Bitshift (bits)
  216. * @param offset Sample value offset
  217. */
  218. #define DECODE(size, endian, src, dst, n, shift, offset) \
  219. for(;n>0;n--) { \
  220. uint##size##_t v = bytestream_get_##endian(&src); \
  221. AV_WN##size##A(dst, (v - offset) << shift); \
  222. dst += size / 8; \
  223. }
  224. static int pcm_decode_frame(AVCodecContext *avctx,
  225. void *data, int *data_size,
  226. AVPacket *avpkt)
  227. {
  228. const uint8_t *src = avpkt->data;
  229. int buf_size = avpkt->size;
  230. PCMDecode *s = avctx->priv_data;
  231. int sample_size, c, n, out_size;
  232. uint8_t *samples;
  233. int32_t *dst_int32_t;
  234. samples = data;
  235. sample_size = av_get_bits_per_sample(avctx->codec_id)/8;
  236. /* av_get_bits_per_sample returns 0 for CODEC_ID_PCM_DVD */
  237. if (CODEC_ID_PCM_DVD == avctx->codec_id) {
  238. if (avctx->bits_per_coded_sample != 20 &&
  239. avctx->bits_per_coded_sample != 24) {
  240. av_log(avctx, AV_LOG_ERROR,
  241. "PCM DVD unsupported sample depth %i\n",
  242. avctx->bits_per_coded_sample);
  243. return AVERROR(EINVAL);
  244. }
  245. /* 2 samples are interleaved per block in PCM_DVD */
  246. sample_size = avctx->bits_per_coded_sample * 2 / 8;
  247. } else if (avctx->codec_id == CODEC_ID_PCM_LXF)
  248. /* we process 40-bit blocks per channel for LXF */
  249. sample_size = 5;
  250. if (sample_size == 0) {
  251. av_log(avctx, AV_LOG_ERROR, "Invalid sample_size\n");
  252. return AVERROR(EINVAL);
  253. }
  254. n = avctx->channels * sample_size;
  255. if(n && buf_size % n){
  256. if (buf_size < n) {
  257. av_log(avctx, AV_LOG_ERROR, "invalid PCM packet\n");
  258. return -1;
  259. }else
  260. buf_size -= buf_size % n;
  261. }
  262. n = buf_size/sample_size;
  263. out_size = n * av_get_bytes_per_sample(avctx->sample_fmt);
  264. if (avctx->codec_id == CODEC_ID_PCM_DVD ||
  265. avctx->codec_id == CODEC_ID_PCM_LXF)
  266. out_size *= 2;
  267. if (*data_size < out_size) {
  268. av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
  269. return AVERROR(EINVAL);
  270. }
  271. switch(avctx->codec->id) {
  272. case CODEC_ID_PCM_U32LE:
  273. DECODE(32, le32, src, samples, n, 0, 0x80000000)
  274. break;
  275. case CODEC_ID_PCM_U32BE:
  276. DECODE(32, be32, src, samples, n, 0, 0x80000000)
  277. break;
  278. case CODEC_ID_PCM_S24LE:
  279. DECODE(32, le24, src, samples, n, 8, 0)
  280. break;
  281. case CODEC_ID_PCM_S24BE:
  282. DECODE(32, be24, src, samples, n, 8, 0)
  283. break;
  284. case CODEC_ID_PCM_U24LE:
  285. DECODE(32, le24, src, samples, n, 8, 0x800000)
  286. break;
  287. case CODEC_ID_PCM_U24BE:
  288. DECODE(32, be24, src, samples, n, 8, 0x800000)
  289. break;
  290. case CODEC_ID_PCM_S24DAUD:
  291. for(;n>0;n--) {
  292. uint32_t v = bytestream_get_be24(&src);
  293. v >>= 4; // sync flags are here
  294. AV_WN16A(samples, av_reverse[(v >> 8) & 0xff] +
  295. (av_reverse[v & 0xff] << 8));
  296. samples += 2;
  297. }
  298. break;
  299. case CODEC_ID_PCM_S16LE_PLANAR:
  300. {
  301. const uint8_t *src2[MAX_CHANNELS];
  302. n /= avctx->channels;
  303. for(c=0;c<avctx->channels;c++)
  304. src2[c] = &src[c*n*2];
  305. for(;n>0;n--)
  306. for(c=0;c<avctx->channels;c++) {
  307. AV_WN16A(samples, bytestream_get_le16(&src2[c]));
  308. samples += 2;
  309. }
  310. break;
  311. }
  312. case CODEC_ID_PCM_U16LE:
  313. DECODE(16, le16, src, samples, n, 0, 0x8000)
  314. break;
  315. case CODEC_ID_PCM_U16BE:
  316. DECODE(16, be16, src, samples, n, 0, 0x8000)
  317. break;
  318. case CODEC_ID_PCM_S8:
  319. for(;n>0;n--) {
  320. *samples++ = *src++ + 128;
  321. }
  322. break;
  323. #if HAVE_BIGENDIAN
  324. case CODEC_ID_PCM_F64LE:
  325. DECODE(64, le64, src, samples, n, 0, 0)
  326. break;
  327. case CODEC_ID_PCM_S32LE:
  328. case CODEC_ID_PCM_F32LE:
  329. DECODE(32, le32, src, samples, n, 0, 0)
  330. break;
  331. case CODEC_ID_PCM_S16LE:
  332. DECODE(16, le16, src, samples, n, 0, 0)
  333. break;
  334. case CODEC_ID_PCM_F64BE:
  335. case CODEC_ID_PCM_F32BE:
  336. case CODEC_ID_PCM_S32BE:
  337. case CODEC_ID_PCM_S16BE:
  338. #else
  339. case CODEC_ID_PCM_F64BE:
  340. DECODE(64, be64, src, samples, n, 0, 0)
  341. break;
  342. case CODEC_ID_PCM_F32BE:
  343. case CODEC_ID_PCM_S32BE:
  344. DECODE(32, be32, src, samples, n, 0, 0)
  345. break;
  346. case CODEC_ID_PCM_S16BE:
  347. DECODE(16, be16, src, samples, n, 0, 0)
  348. break;
  349. case CODEC_ID_PCM_F64LE:
  350. case CODEC_ID_PCM_F32LE:
  351. case CODEC_ID_PCM_S32LE:
  352. case CODEC_ID_PCM_S16LE:
  353. #endif /* HAVE_BIGENDIAN */
  354. case CODEC_ID_PCM_U8:
  355. memcpy(samples, src, n*sample_size);
  356. samples += n * sample_size;
  357. break;
  358. case CODEC_ID_PCM_ZORK:
  359. for (; n > 0; n--) {
  360. int v = *src++;
  361. if (v < 128)
  362. v = 128 - v;
  363. *samples++ = v;
  364. }
  365. break;
  366. case CODEC_ID_PCM_ALAW:
  367. case CODEC_ID_PCM_MULAW:
  368. for(;n>0;n--) {
  369. AV_WN16A(samples, s->table[*src++]);
  370. samples += 2;
  371. }
  372. break;
  373. case CODEC_ID_PCM_DVD:
  374. {
  375. const uint8_t *src8;
  376. dst_int32_t = data;
  377. n /= avctx->channels;
  378. switch (avctx->bits_per_coded_sample) {
  379. case 20:
  380. while (n--) {
  381. c = avctx->channels;
  382. src8 = src + 4*c;
  383. while (c--) {
  384. *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8 &0xf0) << 8);
  385. *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++ &0x0f) << 12);
  386. }
  387. src = src8;
  388. }
  389. break;
  390. case 24:
  391. while (n--) {
  392. c = avctx->channels;
  393. src8 = src + 4*c;
  394. while (c--) {
  395. *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++) << 8);
  396. *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++) << 8);
  397. }
  398. src = src8;
  399. }
  400. break;
  401. }
  402. samples = (uint8_t *) dst_int32_t;
  403. break;
  404. }
  405. case CODEC_ID_PCM_LXF:
  406. {
  407. int i;
  408. const uint8_t *src8;
  409. dst_int32_t = data;
  410. n /= avctx->channels;
  411. //unpack and de-planerize
  412. for (i = 0; i < n; i++) {
  413. for (c = 0, src8 = src + i*5; c < avctx->channels; c++, src8 += n*5) {
  414. //extract low 20 bits and expand to 32 bits
  415. *dst_int32_t++ = (src8[2] << 28) | (src8[1] << 20) | (src8[0] << 12) |
  416. ((src8[2] & 0xF) << 8) | src8[1];
  417. }
  418. for (c = 0, src8 = src + i*5; c < avctx->channels; c++, src8 += n*5) {
  419. //extract high 20 bits and expand to 32 bits
  420. *dst_int32_t++ = (src8[4] << 24) | (src8[3] << 16) |
  421. ((src8[2] & 0xF0) << 8) | (src8[4] << 4) | (src8[3] >> 4);
  422. }
  423. }
  424. samples = (uint8_t *) dst_int32_t;
  425. break;
  426. }
  427. default:
  428. return -1;
  429. }
  430. *data_size = out_size;
  431. return buf_size;
  432. }
  433. #if CONFIG_ENCODERS
  434. #define PCM_ENCODER(id_,sample_fmt_,name_,long_name_) \
  435. AVCodec ff_ ## name_ ## _encoder = { \
  436. .name = #name_, \
  437. .type = AVMEDIA_TYPE_AUDIO, \
  438. .id = id_, \
  439. .init = pcm_encode_init, \
  440. .encode = pcm_encode_frame, \
  441. .close = pcm_encode_close, \
  442. .sample_fmts = (const enum AVSampleFormat[]){sample_fmt_,AV_SAMPLE_FMT_NONE}, \
  443. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  444. }
  445. #else
  446. #define PCM_ENCODER(id,sample_fmt_,name,long_name_)
  447. #endif
  448. #if CONFIG_DECODERS
  449. #define PCM_DECODER(id_,sample_fmt_,name_,long_name_) \
  450. AVCodec ff_ ## name_ ## _decoder = { \
  451. .name = #name_, \
  452. .type = AVMEDIA_TYPE_AUDIO, \
  453. .id = id_, \
  454. .priv_data_size = sizeof(PCMDecode), \
  455. .init = pcm_decode_init, \
  456. .decode = pcm_decode_frame, \
  457. .sample_fmts = (const enum AVSampleFormat[]){sample_fmt_,AV_SAMPLE_FMT_NONE}, \
  458. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  459. }
  460. #else
  461. #define PCM_DECODER(id,sample_fmt_,name,long_name_)
  462. #endif
  463. #define PCM_CODEC(id, sample_fmt_, name, long_name_) \
  464. PCM_ENCODER(id,sample_fmt_,name,long_name_); PCM_DECODER(id,sample_fmt_,name,long_name_)
  465. /* Note: Do not forget to add new entries to the Makefile as well. */
  466. PCM_CODEC (CODEC_ID_PCM_ALAW, AV_SAMPLE_FMT_S16, pcm_alaw, "PCM A-law");
  467. PCM_DECODER(CODEC_ID_PCM_DVD, AV_SAMPLE_FMT_S32, pcm_dvd, "PCM signed 20|24-bit big-endian");
  468. PCM_CODEC (CODEC_ID_PCM_F32BE, AV_SAMPLE_FMT_FLT, pcm_f32be, "PCM 32-bit floating point big-endian");
  469. PCM_CODEC (CODEC_ID_PCM_F32LE, AV_SAMPLE_FMT_FLT, pcm_f32le, "PCM 32-bit floating point little-endian");
  470. PCM_CODEC (CODEC_ID_PCM_F64BE, AV_SAMPLE_FMT_DBL, pcm_f64be, "PCM 64-bit floating point big-endian");
  471. PCM_CODEC (CODEC_ID_PCM_F64LE, AV_SAMPLE_FMT_DBL, pcm_f64le, "PCM 64-bit floating point little-endian");
  472. PCM_DECODER(CODEC_ID_PCM_LXF, AV_SAMPLE_FMT_S32, pcm_lxf, "PCM signed 20-bit little-endian planar");
  473. PCM_CODEC (CODEC_ID_PCM_MULAW, AV_SAMPLE_FMT_S16, pcm_mulaw, "PCM mu-law");
  474. PCM_CODEC (CODEC_ID_PCM_S8, AV_SAMPLE_FMT_U8, pcm_s8, "PCM signed 8-bit");
  475. PCM_CODEC (CODEC_ID_PCM_S16BE, AV_SAMPLE_FMT_S16, pcm_s16be, "PCM signed 16-bit big-endian");
  476. PCM_CODEC (CODEC_ID_PCM_S16LE, AV_SAMPLE_FMT_S16, pcm_s16le, "PCM signed 16-bit little-endian");
  477. PCM_DECODER(CODEC_ID_PCM_S16LE_PLANAR, AV_SAMPLE_FMT_S16, pcm_s16le_planar, "PCM 16-bit little-endian planar");
  478. PCM_CODEC (CODEC_ID_PCM_S24BE, AV_SAMPLE_FMT_S32, pcm_s24be, "PCM signed 24-bit big-endian");
  479. PCM_CODEC (CODEC_ID_PCM_S24DAUD, AV_SAMPLE_FMT_S16, pcm_s24daud, "PCM D-Cinema audio signed 24-bit");
  480. PCM_CODEC (CODEC_ID_PCM_S24LE, AV_SAMPLE_FMT_S32, pcm_s24le, "PCM signed 24-bit little-endian");
  481. PCM_CODEC (CODEC_ID_PCM_S32BE, AV_SAMPLE_FMT_S32, pcm_s32be, "PCM signed 32-bit big-endian");
  482. PCM_CODEC (CODEC_ID_PCM_S32LE, AV_SAMPLE_FMT_S32, pcm_s32le, "PCM signed 32-bit little-endian");
  483. PCM_CODEC (CODEC_ID_PCM_U8, AV_SAMPLE_FMT_U8, pcm_u8, "PCM unsigned 8-bit");
  484. PCM_CODEC (CODEC_ID_PCM_U16BE, AV_SAMPLE_FMT_S16, pcm_u16be, "PCM unsigned 16-bit big-endian");
  485. PCM_CODEC (CODEC_ID_PCM_U16LE, AV_SAMPLE_FMT_S16, pcm_u16le, "PCM unsigned 16-bit little-endian");
  486. PCM_CODEC (CODEC_ID_PCM_U24BE, AV_SAMPLE_FMT_S32, pcm_u24be, "PCM unsigned 24-bit big-endian");
  487. PCM_CODEC (CODEC_ID_PCM_U24LE, AV_SAMPLE_FMT_S32, pcm_u24le, "PCM unsigned 24-bit little-endian");
  488. PCM_CODEC (CODEC_ID_PCM_U32BE, AV_SAMPLE_FMT_S32, pcm_u32be, "PCM unsigned 32-bit big-endian");
  489. PCM_CODEC (CODEC_ID_PCM_U32LE, AV_SAMPLE_FMT_S32, pcm_u32le, "PCM unsigned 32-bit little-endian");
  490. PCM_DECODER(CODEC_ID_PCM_ZORK, AV_SAMPLE_FMT_U8, pcm_zork, "PCM Zork");