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.

506 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
  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 = (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. short *samples;
  75. unsigned char *dst;
  76. uint8_t *srcu8;
  77. int16_t *samples_int16_t;
  78. int32_t *samples_int32_t;
  79. int64_t *samples_int64_t;
  80. uint16_t *samples_uint16_t;
  81. 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. return 0;
  213. }
  214. /**
  215. * Read PCM samples macro
  216. * @param type Datatype of native machine format
  217. * @param endian bytestream_get_xxx() endian suffix
  218. * @param src Source pointer (variable name)
  219. * @param dst Destination pointer (variable name)
  220. * @param n Total number of samples (variable name)
  221. * @param shift Bitshift (bits)
  222. * @param offset Sample value offset
  223. */
  224. #define DECODE(type, endian, src, dst, n, shift, offset) \
  225. dst_##type = (type*)dst; \
  226. for(;n>0;n--) { \
  227. register type v = bytestream_get_##endian(&src); \
  228. *dst_##type++ = (v - offset) << shift; \
  229. } \
  230. dst = (short*)dst_##type;
  231. static int pcm_decode_frame(AVCodecContext *avctx,
  232. void *data, int *data_size,
  233. AVPacket *avpkt)
  234. {
  235. const uint8_t *buf = avpkt->data;
  236. int buf_size = avpkt->size;
  237. PCMDecode *s = avctx->priv_data;
  238. int sample_size, c, n;
  239. short *samples;
  240. const uint8_t *src, *src8, *src2[MAX_CHANNELS];
  241. uint8_t *dstu8;
  242. int16_t *dst_int16_t;
  243. int32_t *dst_int32_t;
  244. int64_t *dst_int64_t;
  245. uint16_t *dst_uint16_t;
  246. uint32_t *dst_uint32_t;
  247. samples = data;
  248. src = buf;
  249. if (avctx->sample_fmt!=avctx->codec->sample_fmts[0]) {
  250. av_log(avctx, AV_LOG_ERROR, "invalid sample_fmt\n");
  251. return -1;
  252. }
  253. if(avctx->channels <= 0 || avctx->channels > MAX_CHANNELS){
  254. av_log(avctx, AV_LOG_ERROR, "PCM channels out of bounds\n");
  255. return -1;
  256. }
  257. sample_size = av_get_bits_per_sample(avctx->codec_id)/8;
  258. /* av_get_bits_per_sample returns 0 for CODEC_ID_PCM_DVD */
  259. if (CODEC_ID_PCM_DVD == avctx->codec_id)
  260. /* 2 samples are interleaved per block in PCM_DVD */
  261. sample_size = avctx->bits_per_coded_sample * 2 / 8;
  262. n = avctx->channels * sample_size;
  263. if(n && buf_size % n){
  264. if (buf_size < n) {
  265. av_log(avctx, AV_LOG_ERROR, "invalid PCM packet\n");
  266. return -1;
  267. }else
  268. buf_size -= buf_size % n;
  269. }
  270. buf_size= FFMIN(buf_size, *data_size/2);
  271. *data_size=0;
  272. n = buf_size/sample_size;
  273. switch(avctx->codec->id) {
  274. case CODEC_ID_PCM_U32LE:
  275. DECODE(uint32_t, le32, src, samples, n, 0, 0x80000000)
  276. break;
  277. case CODEC_ID_PCM_U32BE:
  278. DECODE(uint32_t, be32, src, samples, n, 0, 0x80000000)
  279. break;
  280. case CODEC_ID_PCM_S24LE:
  281. DECODE(int32_t, le24, src, samples, n, 8, 0)
  282. break;
  283. case CODEC_ID_PCM_S24BE:
  284. DECODE(int32_t, be24, src, samples, n, 8, 0)
  285. break;
  286. case CODEC_ID_PCM_U24LE:
  287. DECODE(uint32_t, le24, src, samples, n, 8, 0x800000)
  288. break;
  289. case CODEC_ID_PCM_U24BE:
  290. DECODE(uint32_t, be24, src, samples, n, 8, 0x800000)
  291. break;
  292. case CODEC_ID_PCM_S24DAUD:
  293. for(;n>0;n--) {
  294. uint32_t v = bytestream_get_be24(&src);
  295. v >>= 4; // sync flags are here
  296. *samples++ = av_reverse[(v >> 8) & 0xff] +
  297. (av_reverse[v & 0xff] << 8);
  298. }
  299. break;
  300. case CODEC_ID_PCM_S16LE_PLANAR:
  301. n /= avctx->channels;
  302. for(c=0;c<avctx->channels;c++)
  303. src2[c] = &src[c*n*2];
  304. for(;n>0;n--)
  305. for(c=0;c<avctx->channels;c++)
  306. *samples++ = bytestream_get_le16(&src2[c]);
  307. src = src2[avctx->channels-1];
  308. break;
  309. case CODEC_ID_PCM_U16LE:
  310. DECODE(uint16_t, le16, src, samples, n, 0, 0x8000)
  311. break;
  312. case CODEC_ID_PCM_U16BE:
  313. DECODE(uint16_t, be16, src, samples, n, 0, 0x8000)
  314. break;
  315. case CODEC_ID_PCM_S8:
  316. dstu8= (uint8_t*)samples;
  317. for(;n>0;n--) {
  318. *dstu8++ = *src++ + 128;
  319. }
  320. samples= (short*)dstu8;
  321. break;
  322. #if HAVE_BIGENDIAN
  323. case CODEC_ID_PCM_F64LE:
  324. DECODE(int64_t, le64, src, samples, n, 0, 0)
  325. break;
  326. case CODEC_ID_PCM_S32LE:
  327. case CODEC_ID_PCM_F32LE:
  328. DECODE(int32_t, le32, src, samples, n, 0, 0)
  329. break;
  330. case CODEC_ID_PCM_S16LE:
  331. DECODE(int16_t, le16, src, samples, n, 0, 0)
  332. break;
  333. case CODEC_ID_PCM_F64BE:
  334. case CODEC_ID_PCM_F32BE:
  335. case CODEC_ID_PCM_S32BE:
  336. case CODEC_ID_PCM_S16BE:
  337. #else
  338. case CODEC_ID_PCM_F64BE:
  339. DECODE(int64_t, be64, src, samples, n, 0, 0)
  340. break;
  341. case CODEC_ID_PCM_F32BE:
  342. case CODEC_ID_PCM_S32BE:
  343. DECODE(int32_t, be32, src, samples, n, 0, 0)
  344. break;
  345. case CODEC_ID_PCM_S16BE:
  346. DECODE(int16_t, be16, src, samples, n, 0, 0)
  347. break;
  348. case CODEC_ID_PCM_F64LE:
  349. case CODEC_ID_PCM_F32LE:
  350. case CODEC_ID_PCM_S32LE:
  351. case CODEC_ID_PCM_S16LE:
  352. #endif /* HAVE_BIGENDIAN */
  353. case CODEC_ID_PCM_U8:
  354. memcpy(samples, src, n*sample_size);
  355. src += n*sample_size;
  356. samples = (short*)((uint8_t*)data + n*sample_size);
  357. break;
  358. case CODEC_ID_PCM_ZORK:
  359. for(;n>0;n--) {
  360. int x= *src++;
  361. if(x&128) x-= 128;
  362. else x = -x;
  363. *samples++ = x << 8;
  364. }
  365. break;
  366. case CODEC_ID_PCM_ALAW:
  367. case CODEC_ID_PCM_MULAW:
  368. for(;n>0;n--) {
  369. *samples++ = s->table[*src++];
  370. }
  371. break;
  372. case CODEC_ID_PCM_DVD:
  373. dst_int32_t = data;
  374. n /= avctx->channels;
  375. switch (avctx->bits_per_coded_sample) {
  376. case 20:
  377. while (n--) {
  378. c = avctx->channels;
  379. src8 = src + 4*c;
  380. while (c--) {
  381. *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8 &0xf0) << 8);
  382. *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++ &0x0f) << 12);
  383. }
  384. src = src8;
  385. }
  386. break;
  387. case 24:
  388. while (n--) {
  389. c = avctx->channels;
  390. src8 = src + 4*c;
  391. while (c--) {
  392. *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++) << 8);
  393. *dst_int32_t++ = (bytestream_get_be16(&src) << 16) + ((*src8++) << 8);
  394. }
  395. src = src8;
  396. }
  397. break;
  398. default:
  399. av_log(avctx, AV_LOG_ERROR, "PCM DVD unsupported sample depth\n");
  400. return -1;
  401. break;
  402. }
  403. samples = (short *) dst_int32_t;
  404. break;
  405. default:
  406. return -1;
  407. }
  408. *data_size = (uint8_t *)samples - (uint8_t *)data;
  409. return src - buf;
  410. }
  411. #if CONFIG_ENCODERS
  412. #define PCM_ENCODER(id,sample_fmt_,name,long_name_) \
  413. AVCodec name ## _encoder = { \
  414. #name, \
  415. AVMEDIA_TYPE_AUDIO, \
  416. id, \
  417. 0, \
  418. pcm_encode_init, \
  419. pcm_encode_frame, \
  420. pcm_encode_close, \
  421. NULL, \
  422. .sample_fmts = (const enum SampleFormat[]){sample_fmt_,SAMPLE_FMT_NONE}, \
  423. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  424. };
  425. #else
  426. #define PCM_ENCODER(id,sample_fmt_,name,long_name_)
  427. #endif
  428. #if CONFIG_DECODERS
  429. #define PCM_DECODER(id,sample_fmt_,name,long_name_) \
  430. AVCodec name ## _decoder = { \
  431. #name, \
  432. AVMEDIA_TYPE_AUDIO, \
  433. id, \
  434. sizeof(PCMDecode), \
  435. pcm_decode_init, \
  436. NULL, \
  437. NULL, \
  438. pcm_decode_frame, \
  439. .sample_fmts = (const enum SampleFormat[]){sample_fmt_,SAMPLE_FMT_NONE}, \
  440. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  441. };
  442. #else
  443. #define PCM_DECODER(id,sample_fmt_,name,long_name_)
  444. #endif
  445. #define PCM_CODEC(id, sample_fmt_, name, long_name_) \
  446. PCM_ENCODER(id,sample_fmt_,name,long_name_) PCM_DECODER(id,sample_fmt_,name,long_name_)
  447. /* Note: Do not forget to add new entries to the Makefile as well. */
  448. PCM_CODEC (CODEC_ID_PCM_ALAW, SAMPLE_FMT_S16, pcm_alaw, "PCM A-law");
  449. PCM_CODEC (CODEC_ID_PCM_DVD, SAMPLE_FMT_S32, pcm_dvd, "PCM signed 20|24-bit big-endian");
  450. PCM_CODEC (CODEC_ID_PCM_F32BE, SAMPLE_FMT_FLT, pcm_f32be, "PCM 32-bit floating point big-endian");
  451. PCM_CODEC (CODEC_ID_PCM_F32LE, SAMPLE_FMT_FLT, pcm_f32le, "PCM 32-bit floating point little-endian");
  452. PCM_CODEC (CODEC_ID_PCM_F64BE, SAMPLE_FMT_DBL, pcm_f64be, "PCM 64-bit floating point big-endian");
  453. PCM_CODEC (CODEC_ID_PCM_F64LE, SAMPLE_FMT_DBL, pcm_f64le, "PCM 64-bit floating point little-endian");
  454. PCM_CODEC (CODEC_ID_PCM_MULAW, SAMPLE_FMT_S16, pcm_mulaw, "PCM mu-law");
  455. PCM_CODEC (CODEC_ID_PCM_S8, SAMPLE_FMT_U8, pcm_s8, "PCM signed 8-bit");
  456. PCM_CODEC (CODEC_ID_PCM_S16BE, SAMPLE_FMT_S16, pcm_s16be, "PCM signed 16-bit big-endian");
  457. PCM_CODEC (CODEC_ID_PCM_S16LE, SAMPLE_FMT_S16, pcm_s16le, "PCM signed 16-bit little-endian");
  458. PCM_DECODER(CODEC_ID_PCM_S16LE_PLANAR, SAMPLE_FMT_S16, pcm_s16le_planar, "PCM 16-bit little-endian planar");
  459. PCM_CODEC (CODEC_ID_PCM_S24BE, SAMPLE_FMT_S32, pcm_s24be, "PCM signed 24-bit big-endian");
  460. PCM_CODEC (CODEC_ID_PCM_S24DAUD, SAMPLE_FMT_S16, pcm_s24daud, "PCM D-Cinema audio signed 24-bit");
  461. PCM_CODEC (CODEC_ID_PCM_S24LE, SAMPLE_FMT_S32, pcm_s24le, "PCM signed 24-bit little-endian");
  462. PCM_CODEC (CODEC_ID_PCM_S32BE, SAMPLE_FMT_S32, pcm_s32be, "PCM signed 32-bit big-endian");
  463. PCM_CODEC (CODEC_ID_PCM_S32LE, SAMPLE_FMT_S32, pcm_s32le, "PCM signed 32-bit little-endian");
  464. PCM_CODEC (CODEC_ID_PCM_U8, SAMPLE_FMT_U8, pcm_u8, "PCM unsigned 8-bit");
  465. PCM_CODEC (CODEC_ID_PCM_U16BE, SAMPLE_FMT_S16, pcm_u16be, "PCM unsigned 16-bit big-endian");
  466. PCM_CODEC (CODEC_ID_PCM_U16LE, SAMPLE_FMT_S16, pcm_u16le, "PCM unsigned 16-bit little-endian");
  467. PCM_CODEC (CODEC_ID_PCM_U24BE, SAMPLE_FMT_S32, pcm_u24be, "PCM unsigned 24-bit big-endian");
  468. PCM_CODEC (CODEC_ID_PCM_U24LE, SAMPLE_FMT_S32, pcm_u24le, "PCM unsigned 24-bit little-endian");
  469. PCM_CODEC (CODEC_ID_PCM_U32BE, SAMPLE_FMT_S32, pcm_u32be, "PCM unsigned 32-bit big-endian");
  470. PCM_CODEC (CODEC_ID_PCM_U32LE, SAMPLE_FMT_S32, pcm_u32le, "PCM unsigned 32-bit little-endian");
  471. PCM_CODEC (CODEC_ID_PCM_ZORK, SAMPLE_FMT_S16, pcm_zork, "PCM Zork");