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.

531 lines
18KB

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