pcm.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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 "config.h"
  26. #include "config_components.h"
  27. #include "libavutil/attributes.h"
  28. #include "libavutil/float_dsp.h"
  29. #include "libavutil/mem.h"
  30. #include "libavutil/reverse.h"
  31. #include "libavutil/thread.h"
  32. #include "avcodec.h"
  33. #include "bytestream.h"
  34. #include "codec_internal.h"
  35. #include "decode.h"
  36. #include "encode.h"
  37. #include "pcm_tablegen.h"
  38. av_unused av_cold static int pcm_encode_init(AVCodecContext *avctx)
  39. {
  40. avctx->frame_size = 0;
  41. #if !CONFIG_HARDCODED_TABLES
  42. switch (avctx->codec->id) {
  43. #if CONFIG_PCM_ALAW_ENCODER
  44. case AV_CODEC_ID_PCM_ALAW: {
  45. static AVOnce once_alaw = AV_ONCE_INIT;
  46. ff_thread_once(&once_alaw, pcm_alaw_tableinit);
  47. break;
  48. }
  49. #endif
  50. #if CONFIG_PCM_MULAW_ENCODER
  51. case AV_CODEC_ID_PCM_MULAW: {
  52. static AVOnce once_mulaw = AV_ONCE_INIT;
  53. ff_thread_once(&once_mulaw, pcm_ulaw_tableinit);
  54. break;
  55. }
  56. #endif
  57. #if CONFIG_PCM_VIDC_ENCODER
  58. case AV_CODEC_ID_PCM_VIDC: {
  59. static AVOnce once_vidc = AV_ONCE_INIT;
  60. ff_thread_once(&once_vidc, pcm_vidc_tableinit);
  61. break;
  62. }
  63. #endif
  64. default:
  65. break;
  66. }
  67. #endif
  68. avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id);
  69. avctx->block_align = avctx->ch_layout.nb_channels * avctx->bits_per_coded_sample / 8;
  70. avctx->bit_rate = avctx->block_align * 8LL * avctx->sample_rate;
  71. return 0;
  72. }
  73. /**
  74. * Write PCM samples macro
  75. * @param type Datatype of native machine format
  76. * @param endian bytestream_put_xxx() suffix
  77. * @param src Source pointer (variable name)
  78. * @param dst Destination pointer (variable name)
  79. * @param n Total number of samples (variable name)
  80. * @param shift Bitshift (bits)
  81. * @param offset Sample value offset
  82. */
  83. #define ENCODE(type, endian, src, dst, n, shift, offset) \
  84. samples_ ## type = (const type *) src; \
  85. for (; n > 0; n--) { \
  86. register type v = (*samples_ ## type++ >> shift) + offset; \
  87. bytestream_put_ ## endian(&dst, v); \
  88. }
  89. #define ENCODE_PLANAR(type, endian, dst, n, shift, offset) \
  90. n /= avctx->ch_layout.nb_channels; \
  91. for (c = 0; c < avctx->ch_layout.nb_channels; c++) { \
  92. int i; \
  93. samples_ ## type = (const type *) frame->extended_data[c]; \
  94. for (i = n; i > 0; i--) { \
  95. register type v = (*samples_ ## type++ >> shift) + offset; \
  96. bytestream_put_ ## endian(&dst, v); \
  97. } \
  98. }
  99. av_unused static int pcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  100. const AVFrame *frame, int *got_packet_ptr)
  101. {
  102. int n, c, sample_size, ret;
  103. const short *samples;
  104. unsigned char *dst;
  105. const uint8_t *samples_uint8_t;
  106. const int16_t *samples_int16_t;
  107. const int32_t *samples_int32_t;
  108. const int64_t *samples_int64_t;
  109. const uint16_t *samples_uint16_t;
  110. const uint32_t *samples_uint32_t;
  111. sample_size = av_get_bits_per_sample(avctx->codec->id) / 8;
  112. n = frame->nb_samples * avctx->ch_layout.nb_channels;
  113. samples = (const short *)frame->data[0];
  114. if ((ret = ff_get_encode_buffer(avctx, avpkt, n * sample_size, 0)) < 0)
  115. return ret;
  116. dst = avpkt->data;
  117. switch (avctx->codec->id) {
  118. case AV_CODEC_ID_PCM_U32LE:
  119. ENCODE(uint32_t, le32, samples, dst, n, 0, 0x80000000)
  120. break;
  121. case AV_CODEC_ID_PCM_U32BE:
  122. ENCODE(uint32_t, be32, samples, dst, n, 0, 0x80000000)
  123. break;
  124. case AV_CODEC_ID_PCM_S24LE:
  125. ENCODE(int32_t, le24, samples, dst, n, 8, 0)
  126. break;
  127. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  128. ENCODE_PLANAR(int32_t, le24, dst, n, 8, 0)
  129. break;
  130. case AV_CODEC_ID_PCM_S24BE:
  131. ENCODE(int32_t, be24, samples, dst, n, 8, 0)
  132. break;
  133. case AV_CODEC_ID_PCM_U24LE:
  134. ENCODE(uint32_t, le24, samples, dst, n, 8, 0x800000)
  135. break;
  136. case AV_CODEC_ID_PCM_U24BE:
  137. ENCODE(uint32_t, be24, samples, dst, n, 8, 0x800000)
  138. break;
  139. case AV_CODEC_ID_PCM_S24DAUD:
  140. for (; n > 0; n--) {
  141. uint32_t tmp = ff_reverse[(*samples >> 8) & 0xff] +
  142. (ff_reverse[*samples & 0xff] << 8);
  143. tmp <<= 4; // sync flags would go here
  144. bytestream_put_be24(&dst, tmp);
  145. samples++;
  146. }
  147. break;
  148. case AV_CODEC_ID_PCM_U16LE:
  149. ENCODE(uint16_t, le16, samples, dst, n, 0, 0x8000)
  150. break;
  151. case AV_CODEC_ID_PCM_U16BE:
  152. ENCODE(uint16_t, be16, samples, dst, n, 0, 0x8000)
  153. break;
  154. case AV_CODEC_ID_PCM_S8:
  155. ENCODE(uint8_t, byte, samples, dst, n, 0, -128)
  156. break;
  157. case AV_CODEC_ID_PCM_S8_PLANAR:
  158. ENCODE_PLANAR(uint8_t, byte, dst, n, 0, -128)
  159. break;
  160. #if HAVE_BIGENDIAN
  161. case AV_CODEC_ID_PCM_S64LE:
  162. case AV_CODEC_ID_PCM_F64LE:
  163. ENCODE(int64_t, le64, samples, dst, n, 0, 0)
  164. break;
  165. case AV_CODEC_ID_PCM_S32LE:
  166. case AV_CODEC_ID_PCM_F32LE:
  167. ENCODE(int32_t, le32, samples, dst, n, 0, 0)
  168. break;
  169. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  170. ENCODE_PLANAR(int32_t, le32, dst, n, 0, 0)
  171. break;
  172. case AV_CODEC_ID_PCM_S16LE:
  173. ENCODE(int16_t, le16, samples, dst, n, 0, 0)
  174. break;
  175. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  176. ENCODE_PLANAR(int16_t, le16, dst, n, 0, 0)
  177. break;
  178. case AV_CODEC_ID_PCM_F64BE:
  179. case AV_CODEC_ID_PCM_F32BE:
  180. case AV_CODEC_ID_PCM_S64BE:
  181. case AV_CODEC_ID_PCM_S32BE:
  182. case AV_CODEC_ID_PCM_S16BE:
  183. #else
  184. case AV_CODEC_ID_PCM_S64BE:
  185. case AV_CODEC_ID_PCM_F64BE:
  186. ENCODE(int64_t, be64, samples, dst, n, 0, 0)
  187. break;
  188. case AV_CODEC_ID_PCM_F32BE:
  189. case AV_CODEC_ID_PCM_S32BE:
  190. ENCODE(int32_t, be32, samples, dst, n, 0, 0)
  191. break;
  192. case AV_CODEC_ID_PCM_S16BE:
  193. ENCODE(int16_t, be16, samples, dst, n, 0, 0)
  194. break;
  195. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  196. ENCODE_PLANAR(int16_t, be16, dst, n, 0, 0)
  197. break;
  198. case AV_CODEC_ID_PCM_F64LE:
  199. case AV_CODEC_ID_PCM_F32LE:
  200. case AV_CODEC_ID_PCM_S64LE:
  201. case AV_CODEC_ID_PCM_S32LE:
  202. case AV_CODEC_ID_PCM_S16LE:
  203. #endif /* HAVE_BIGENDIAN */
  204. case AV_CODEC_ID_PCM_U8:
  205. memcpy(dst, samples, n * sample_size);
  206. break;
  207. #if HAVE_BIGENDIAN
  208. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  209. #else
  210. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  211. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  212. #endif /* HAVE_BIGENDIAN */
  213. n /= avctx->ch_layout.nb_channels;
  214. for (c = 0; c < avctx->ch_layout.nb_channels; c++) {
  215. const uint8_t *src = frame->extended_data[c];
  216. bytestream_put_buffer(&dst, src, n * sample_size);
  217. }
  218. break;
  219. #if CONFIG_PCM_ALAW_ENCODER
  220. case AV_CODEC_ID_PCM_ALAW:
  221. for (; n > 0; n--) {
  222. int v = *samples++;
  223. *dst++ = linear_to_alaw[(v + 32768) >> 2];
  224. }
  225. break;
  226. #endif
  227. #if CONFIG_PCM_MULAW_ENCODER
  228. case AV_CODEC_ID_PCM_MULAW:
  229. for (; n > 0; n--) {
  230. int v = *samples++;
  231. *dst++ = linear_to_ulaw[(v + 32768) >> 2];
  232. }
  233. break;
  234. #endif
  235. #if CONFIG_PCM_VIDC_ENCODER
  236. case AV_CODEC_ID_PCM_VIDC:
  237. for (; n > 0; n--) {
  238. int v = *samples++;
  239. *dst++ = linear_to_vidc[(v + 32768) >> 2];
  240. }
  241. break;
  242. #endif
  243. default:
  244. return -1;
  245. }
  246. *got_packet_ptr = 1;
  247. return 0;
  248. }
  249. typedef struct PCMDecode {
  250. int sample_size;
  251. } PCMDecode;
  252. av_unused av_cold static int pcm_decode_init(AVCodecContext *avctx)
  253. {
  254. PCMDecode *s = avctx->priv_data;
  255. static const struct {
  256. enum AVCodecID codec_id;
  257. int8_t sample_fmt;
  258. uint8_t sample_size;
  259. uint8_t bits_per_sample;
  260. } codec_id_to_samplefmt[] = {
  261. #define ENTRY(CODEC_ID, SAMPLE_FMT, BITS_PER_SAMPLE) \
  262. { AV_CODEC_ID_PCM_ ## CODEC_ID, AV_SAMPLE_FMT_ ## SAMPLE_FMT, \
  263. BITS_PER_SAMPLE / 8, BITS_PER_SAMPLE }
  264. ENTRY(S8, U8, 8), ENTRY(S8_PLANAR, U8P, 8),
  265. ENTRY(S16BE, S16, 16), ENTRY(S16BE_PLANAR, S16P, 16),
  266. ENTRY(S16LE, S16, 16), ENTRY(S16LE_PLANAR, S16P, 16),
  267. ENTRY(S24DAUD, S16, 24), ENTRY(S24BE, S32, 24),
  268. ENTRY(S24LE, S32, 24), ENTRY(S24LE_PLANAR, S32P, 24),
  269. ENTRY(S32BE, S32, 32), ENTRY(S32LE, S32, 32),
  270. ENTRY(S32LE_PLANAR, S32P, 32),
  271. ENTRY(S64BE, S64, 64), ENTRY(S64LE, S64, 64),
  272. ENTRY(SGA, U8, 8), ENTRY(U8, U8, 8),
  273. ENTRY(U16BE, S16, 16), ENTRY(U16LE, S16, 16),
  274. ENTRY(U24BE, S32, 24), ENTRY(U24LE, S32, 24),
  275. ENTRY(U32BE, S32, 32), ENTRY(U32LE, S32, 32),
  276. ENTRY(F32BE, FLT, 32), ENTRY(F32LE, FLT, 32),
  277. ENTRY(F64BE, DBL, 64), ENTRY(F64LE, DBL, 64),
  278. { .codec_id = AV_CODEC_ID_PCM_LXF, .sample_fmt = AV_SAMPLE_FMT_S32P, .sample_size = 5 },
  279. };
  280. for (unsigned i = 0; i < FF_ARRAY_ELEMS(codec_id_to_samplefmt); ++i) {
  281. if (codec_id_to_samplefmt[i].codec_id == avctx->codec_id) {
  282. s->sample_size = codec_id_to_samplefmt[i].sample_size;
  283. avctx->sample_fmt = codec_id_to_samplefmt[i].sample_fmt;
  284. if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
  285. avctx->bits_per_raw_sample = codec_id_to_samplefmt[i].bits_per_sample;
  286. break;
  287. }
  288. av_assert1(i + 1 < FF_ARRAY_ELEMS(codec_id_to_samplefmt));
  289. }
  290. return 0;
  291. }
  292. typedef struct PCMScaleDecode {
  293. PCMDecode base;
  294. void (*vector_fmul_scalar)(float *dst, const float *src, float mul,
  295. int len);
  296. float scale;
  297. } PCMScaleDecode;
  298. av_unused av_cold static int pcm_scale_decode_init(AVCodecContext *avctx)
  299. {
  300. PCMScaleDecode *s = avctx->priv_data;
  301. AVFloatDSPContext *fdsp;
  302. avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
  303. s->base.sample_size = 4;
  304. if (avctx->bits_per_coded_sample < 1 || avctx->bits_per_coded_sample > 24)
  305. return AVERROR_INVALIDDATA;
  306. s->scale = 1. / (1 << (avctx->bits_per_coded_sample - 1));
  307. fdsp = avpriv_float_dsp_alloc(0);
  308. if (!fdsp)
  309. return AVERROR(ENOMEM);
  310. s->vector_fmul_scalar = fdsp->vector_fmul_scalar;
  311. av_free(fdsp);
  312. return 0;
  313. }
  314. typedef struct PCMLUTDecode {
  315. PCMDecode base;
  316. int16_t table[256];
  317. } PCMLUTDecode;
  318. av_unused av_cold static int pcm_lut_decode_init(AVCodecContext *avctx)
  319. {
  320. PCMLUTDecode *s = avctx->priv_data;
  321. switch (avctx->codec_id) {
  322. default:
  323. av_unreachable("pcm_lut_decode_init() only used with alaw, mulaw and vidc");
  324. #if CONFIG_PCM_ALAW_DECODER
  325. case AV_CODEC_ID_PCM_ALAW:
  326. for (int i = 0; i < 256; i++)
  327. s->table[i] = alaw2linear(i);
  328. break;
  329. #endif
  330. #if CONFIG_PCM_MULAW_DECODER
  331. case AV_CODEC_ID_PCM_MULAW:
  332. for (int i = 0; i < 256; i++)
  333. s->table[i] = ulaw2linear(i);
  334. break;
  335. #endif
  336. #if CONFIG_PCM_VIDC_DECODER
  337. case AV_CODEC_ID_PCM_VIDC:
  338. for (int i = 0; i < 256; i++)
  339. s->table[i] = vidc2linear(i);
  340. break;
  341. #endif
  342. }
  343. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  344. s->base.sample_size = 1;
  345. return 0;
  346. }
  347. /**
  348. * Read PCM samples macro
  349. * @param size Data size of native machine format
  350. * @param endian bytestream_get_xxx() endian suffix
  351. * @param src Source pointer (variable name)
  352. * @param dst Destination pointer (variable name)
  353. * @param n Total number of samples (variable name)
  354. * @param shift Bitshift (bits)
  355. * @param offset Sample value offset
  356. */
  357. #define DECODE(size, endian, src, dst, n, shift, offset) \
  358. for (; n > 0; n--) { \
  359. uint ## size ## _t v = bytestream_get_ ## endian(&src); \
  360. AV_WN ## size ## A(dst, (uint ## size ## _t)(v - offset) << shift); \
  361. dst += size / 8; \
  362. }
  363. #define DECODE_PLANAR(size, endian, src, dst, n, shift, offset) \
  364. n /= channels; \
  365. for (c = 0; c < avctx->ch_layout.nb_channels; c++) { \
  366. int i; \
  367. dst = frame->extended_data[c]; \
  368. for (i = n; i > 0; i--) { \
  369. uint ## size ## _t v = bytestream_get_ ## endian(&src); \
  370. AV_WN ## size ## A(dst, (uint ## size ##_t)(v - offset) << shift); \
  371. dst += size / 8; \
  372. } \
  373. }
  374. static int pcm_decode_frame(AVCodecContext *avctx, AVFrame *frame,
  375. int *got_frame_ptr, AVPacket *avpkt)
  376. {
  377. const uint8_t *src = avpkt->data;
  378. int buf_size = avpkt->size;
  379. PCMDecode *s = avctx->priv_data;
  380. int channels = avctx->ch_layout.nb_channels;
  381. int sample_size = s->sample_size;
  382. int c, n, ret, samples_per_block;
  383. uint8_t *samples;
  384. int32_t *dst_int32_t;
  385. samples_per_block = 1;
  386. if (avctx->codec_id == AV_CODEC_ID_PCM_LXF) {
  387. /* we process 40-bit blocks per channel for LXF */
  388. samples_per_block = 2;
  389. }
  390. if (channels == 0) {
  391. av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
  392. return AVERROR(EINVAL);
  393. }
  394. if (avctx->codec_id != avctx->codec->id) {
  395. av_log(avctx, AV_LOG_ERROR, "codec ids mismatch\n");
  396. return AVERROR(EINVAL);
  397. }
  398. n = channels * sample_size;
  399. if (n && buf_size % n) {
  400. if (buf_size < n) {
  401. av_log(avctx, AV_LOG_ERROR,
  402. "Invalid PCM packet, data has size %d but at least a size of %d was expected\n",
  403. buf_size, n);
  404. return AVERROR_INVALIDDATA;
  405. } else
  406. buf_size -= buf_size % n;
  407. }
  408. n = buf_size / sample_size;
  409. /* get output buffer */
  410. frame->nb_samples = n * samples_per_block / channels;
  411. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  412. return ret;
  413. samples = frame->data[0];
  414. switch (avctx->codec_id) {
  415. case AV_CODEC_ID_PCM_U32LE:
  416. DECODE(32, le32, src, samples, n, 0, 0x80000000)
  417. break;
  418. case AV_CODEC_ID_PCM_U32BE:
  419. DECODE(32, be32, src, samples, n, 0, 0x80000000)
  420. break;
  421. case AV_CODEC_ID_PCM_S24LE:
  422. DECODE(32, le24, src, samples, n, 8, 0)
  423. break;
  424. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  425. DECODE_PLANAR(32, le24, src, samples, n, 8, 0);
  426. break;
  427. case AV_CODEC_ID_PCM_S24BE:
  428. DECODE(32, be24, src, samples, n, 8, 0)
  429. break;
  430. case AV_CODEC_ID_PCM_U24LE:
  431. DECODE(32, le24, src, samples, n, 8, 0x800000)
  432. break;
  433. case AV_CODEC_ID_PCM_U24BE:
  434. DECODE(32, be24, src, samples, n, 8, 0x800000)
  435. break;
  436. case AV_CODEC_ID_PCM_S24DAUD:
  437. for (; n > 0; n--) {
  438. uint32_t v = bytestream_get_be24(&src);
  439. v >>= 4; // sync flags are here
  440. AV_WN16A(samples, ff_reverse[(v >> 8) & 0xff] +
  441. (ff_reverse[v & 0xff] << 8));
  442. samples += 2;
  443. }
  444. break;
  445. case AV_CODEC_ID_PCM_U16LE:
  446. DECODE(16, le16, src, samples, n, 0, 0x8000)
  447. break;
  448. case AV_CODEC_ID_PCM_U16BE:
  449. DECODE(16, be16, src, samples, n, 0, 0x8000)
  450. break;
  451. case AV_CODEC_ID_PCM_S8:
  452. for (; n > 0; n--)
  453. *samples++ = *src++ + 128;
  454. break;
  455. case AV_CODEC_ID_PCM_SGA:
  456. for (; n > 0; n--) {
  457. int sign = *src >> 7;
  458. int magn = *src & 0x7f;
  459. *samples++ = sign ? 128 - magn : 128 + magn;
  460. src++;
  461. }
  462. break;
  463. case AV_CODEC_ID_PCM_S8_PLANAR:
  464. n /= avctx->ch_layout.nb_channels;
  465. for (c = 0; c < avctx->ch_layout.nb_channels; c++) {
  466. int i;
  467. samples = frame->extended_data[c];
  468. for (i = n; i > 0; i--)
  469. *samples++ = *src++ + 128;
  470. }
  471. break;
  472. #if HAVE_BIGENDIAN
  473. case AV_CODEC_ID_PCM_S64LE:
  474. case AV_CODEC_ID_PCM_F64LE:
  475. DECODE(64, le64, src, samples, n, 0, 0)
  476. break;
  477. case AV_CODEC_ID_PCM_S32LE:
  478. case AV_CODEC_ID_PCM_F32LE:
  479. case AV_CODEC_ID_PCM_F24LE:
  480. case AV_CODEC_ID_PCM_F16LE:
  481. DECODE(32, le32, src, samples, n, 0, 0)
  482. break;
  483. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  484. DECODE_PLANAR(32, le32, src, samples, n, 0, 0);
  485. break;
  486. case AV_CODEC_ID_PCM_S16LE:
  487. DECODE(16, le16, src, samples, n, 0, 0)
  488. break;
  489. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  490. DECODE_PLANAR(16, le16, src, samples, n, 0, 0);
  491. break;
  492. case AV_CODEC_ID_PCM_F64BE:
  493. case AV_CODEC_ID_PCM_F32BE:
  494. case AV_CODEC_ID_PCM_S64BE:
  495. case AV_CODEC_ID_PCM_S32BE:
  496. case AV_CODEC_ID_PCM_S16BE:
  497. #else
  498. case AV_CODEC_ID_PCM_S64BE:
  499. case AV_CODEC_ID_PCM_F64BE:
  500. DECODE(64, be64, src, samples, n, 0, 0)
  501. break;
  502. case AV_CODEC_ID_PCM_F32BE:
  503. case AV_CODEC_ID_PCM_S32BE:
  504. DECODE(32, be32, src, samples, n, 0, 0)
  505. break;
  506. case AV_CODEC_ID_PCM_S16BE:
  507. DECODE(16, be16, src, samples, n, 0, 0)
  508. break;
  509. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  510. DECODE_PLANAR(16, be16, src, samples, n, 0, 0);
  511. break;
  512. case AV_CODEC_ID_PCM_F64LE:
  513. case AV_CODEC_ID_PCM_F32LE:
  514. case AV_CODEC_ID_PCM_F24LE:
  515. case AV_CODEC_ID_PCM_F16LE:
  516. case AV_CODEC_ID_PCM_S64LE:
  517. case AV_CODEC_ID_PCM_S32LE:
  518. case AV_CODEC_ID_PCM_S16LE:
  519. #endif /* HAVE_BIGENDIAN */
  520. case AV_CODEC_ID_PCM_U8:
  521. memcpy(samples, src, n * sample_size);
  522. break;
  523. #if HAVE_BIGENDIAN
  524. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  525. #else
  526. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  527. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  528. #endif /* HAVE_BIGENDIAN */
  529. n /= avctx->ch_layout.nb_channels;
  530. for (c = 0; c < avctx->ch_layout.nb_channels; c++) {
  531. samples = frame->extended_data[c];
  532. bytestream_get_buffer(&src, samples, n * sample_size);
  533. }
  534. break;
  535. #if CONFIG_PCM_ALAW_DECODER || CONFIG_PCM_MULAW_DECODER || \
  536. CONFIG_PCM_VIDC_DECODER
  537. case AV_CODEC_ID_PCM_ALAW:
  538. case AV_CODEC_ID_PCM_MULAW:
  539. case AV_CODEC_ID_PCM_VIDC: {
  540. const int16_t *const lut = ((PCMLUTDecode*)avctx->priv_data)->table;
  541. int16_t *restrict samples_16 = (int16_t*)samples;
  542. for (; n > 0; n--)
  543. *samples_16++ = lut[*src++];
  544. break;
  545. }
  546. #endif
  547. case AV_CODEC_ID_PCM_LXF:
  548. {
  549. int i;
  550. n /= channels;
  551. for (c = 0; c < channels; c++) {
  552. dst_int32_t = (int32_t *)frame->extended_data[c];
  553. for (i = 0; i < n; i++) {
  554. // extract low 20 bits and expand to 32 bits
  555. *dst_int32_t++ = ((uint32_t)src[2]<<28) |
  556. (src[1] << 20) |
  557. (src[0] << 12) |
  558. ((src[2] & 0x0F) << 8) |
  559. src[1];
  560. // extract high 20 bits and expand to 32 bits
  561. *dst_int32_t++ = ((uint32_t)src[4]<<24) |
  562. (src[3] << 16) |
  563. ((src[2] & 0xF0) << 8) |
  564. (src[4] << 4) |
  565. (src[3] >> 4);
  566. src += 5;
  567. }
  568. }
  569. break;
  570. }
  571. default:
  572. return -1;
  573. }
  574. if (avctx->codec_id == AV_CODEC_ID_PCM_F16LE ||
  575. avctx->codec_id == AV_CODEC_ID_PCM_F24LE) {
  576. PCMScaleDecode *s2 = avctx->priv_data;
  577. s2->vector_fmul_scalar((float *)frame->extended_data[0],
  578. (const float *)frame->extended_data[0],
  579. s2->scale, FFALIGN(frame->nb_samples * avctx->ch_layout.nb_channels, 4));
  580. }
  581. *got_frame_ptr = 1;
  582. return buf_size;
  583. }
  584. #define PCM_ENCODER_0(id_, sample_fmt_, name_, long_name_)
  585. #define PCM_ENCODER_1(id_, sample_fmt_, name_, long_name_) \
  586. const FFCodec ff_ ## name_ ## _encoder = { \
  587. .p.name = #name_, \
  588. CODEC_LONG_NAME(long_name_), \
  589. .p.type = AVMEDIA_TYPE_AUDIO, \
  590. .p.id = id_, \
  591. .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_VARIABLE_FRAME_SIZE | \
  592. AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, \
  593. .init = pcm_encode_init, \
  594. FF_CODEC_ENCODE_CB(pcm_encode_frame), \
  595. CODEC_SAMPLEFMTS(sample_fmt_), \
  596. }
  597. #define PCM_ENCODER_2(cf, id, sample_fmt, name, long_name) \
  598. PCM_ENCODER_ ## cf(id, sample_fmt, name, long_name)
  599. #define PCM_ENCODER_3(cf, id, sample_fmt, name, long_name) \
  600. PCM_ENCODER_2(cf, id, sample_fmt, name, long_name)
  601. #define PCM_ENCODER(id, sample_fmt, name, long_name) \
  602. PCM_ENCODER_3(CONFIG_PCM_ ## id ## _ENCODER, AV_CODEC_ID_PCM_ ## id, \
  603. AV_SAMPLE_FMT_ ## sample_fmt, pcm_ ## name, long_name)
  604. #define PCM_DECODER_0(id, sample_fmt, name, long_name, Context, init_func)
  605. #define PCM_DECODER_1(id_, sample_fmt, name_, long_name, Context, init_func)\
  606. const FFCodec ff_ ## name_ ## _decoder = { \
  607. .p.name = #name_, \
  608. CODEC_LONG_NAME(long_name), \
  609. .p.type = AVMEDIA_TYPE_AUDIO, \
  610. .p.id = id_, \
  611. .priv_data_size = sizeof(Context), \
  612. .init = init_func, \
  613. FF_CODEC_DECODE_CB(pcm_decode_frame), \
  614. .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_PARAM_CHANGE, \
  615. }
  616. #define PCM_DECODER_2(cf, id, sample_fmt, name, long_name, Context, init_func) \
  617. PCM_DECODER_ ## cf(id, sample_fmt, name, long_name, Context, init_func)
  618. #define PCM_DECODER_3(cf, id, sample_fmt, name, long_name, Context, init_func) \
  619. PCM_DECODER_2(cf, id, sample_fmt, name, long_name, Context, init_func)
  620. #define PCM_DEC_EXT(id, sample_fmt, name, long_name, Context, init_func) \
  621. PCM_DECODER_3(CONFIG_PCM_ ## id ## _DECODER, AV_CODEC_ID_PCM_ ## id, \
  622. AV_SAMPLE_FMT_ ## sample_fmt, pcm_ ## name, long_name, \
  623. Context, init_func)
  624. #define PCM_DECODER(id, sample_fmt, name, long_name) \
  625. PCM_DEC_EXT(id, sample_fmt, name, long_name, PCMDecode, pcm_decode_init)
  626. #define PCM_CODEC(id, sample_fmt_, name, long_name_) \
  627. PCM_ENCODER(id, sample_fmt_, name, long_name_); \
  628. PCM_DECODER(id, sample_fmt_, name, long_name_)
  629. #define PCM_CODEC_EXT(id, sample_fmt, name, long_name, DecContext, dec_init_func) \
  630. PCM_DEC_EXT(id, sample_fmt, name, long_name, DecContext, dec_init_func); \
  631. PCM_ENCODER(id, sample_fmt, name, long_name)
  632. /* Note: Do not forget to add new entries to the Makefile and
  633. * to the table in pcm_decode_init() as well. */
  634. // AV_CODEC_ID_* pcm_* name
  635. // AV_SAMPLE_FMT_* long name DecodeContext decode init func
  636. PCM_CODEC_EXT(ALAW, S16, alaw, "PCM A-law / G.711 A-law", PCMLUTDecode, pcm_lut_decode_init);
  637. PCM_DEC_EXT (F16LE, FLT, f16le, "PCM 16.8 floating point little-endian", PCMScaleDecode, pcm_scale_decode_init);
  638. PCM_DEC_EXT (F24LE, FLT, f24le, "PCM 24.0 floating point little-endian", PCMScaleDecode, pcm_scale_decode_init);
  639. PCM_CODEC (F32BE, FLT, f32be, "PCM 32-bit floating point big-endian");
  640. PCM_CODEC (F32LE, FLT, f32le, "PCM 32-bit floating point little-endian");
  641. PCM_CODEC (F64BE, DBL, f64be, "PCM 64-bit floating point big-endian");
  642. PCM_CODEC (F64LE, DBL, f64le, "PCM 64-bit floating point little-endian");
  643. PCM_DECODER (LXF, S32P,lxf, "PCM signed 20-bit little-endian planar");
  644. PCM_CODEC_EXT(MULAW, S16, mulaw, "PCM mu-law / G.711 mu-law", PCMLUTDecode, pcm_lut_decode_init);
  645. PCM_CODEC (S8, U8, s8, "PCM signed 8-bit");
  646. PCM_CODEC (S8_PLANAR, U8P, s8_planar, "PCM signed 8-bit planar");
  647. PCM_CODEC (S16BE, S16, s16be, "PCM signed 16-bit big-endian");
  648. PCM_CODEC (S16BE_PLANAR, S16P,s16be_planar, "PCM signed 16-bit big-endian planar");
  649. PCM_CODEC (S16LE, S16, s16le, "PCM signed 16-bit little-endian");
  650. PCM_CODEC (S16LE_PLANAR, S16P,s16le_planar, "PCM signed 16-bit little-endian planar");
  651. PCM_CODEC (S24BE, S32, s24be, "PCM signed 24-bit big-endian");
  652. PCM_CODEC (S24DAUD, S16, s24daud, "PCM D-Cinema audio signed 24-bit");
  653. PCM_CODEC (S24LE, S32, s24le, "PCM signed 24-bit little-endian");
  654. PCM_CODEC (S24LE_PLANAR, S32P,s24le_planar, "PCM signed 24-bit little-endian planar");
  655. PCM_CODEC (S32BE, S32, s32be, "PCM signed 32-bit big-endian");
  656. PCM_CODEC (S32LE, S32, s32le, "PCM signed 32-bit little-endian");
  657. PCM_CODEC (S32LE_PLANAR, S32P,s32le_planar, "PCM signed 32-bit little-endian planar");
  658. PCM_CODEC (U8, U8, u8, "PCM unsigned 8-bit");
  659. PCM_CODEC (U16BE, S16, u16be, "PCM unsigned 16-bit big-endian");
  660. PCM_CODEC (U16LE, S16, u16le, "PCM unsigned 16-bit little-endian");
  661. PCM_CODEC (U24BE, S32, u24be, "PCM unsigned 24-bit big-endian");
  662. PCM_CODEC (U24LE, S32, u24le, "PCM unsigned 24-bit little-endian");
  663. PCM_CODEC (U32BE, S32, u32be, "PCM unsigned 32-bit big-endian");
  664. PCM_CODEC (U32LE, S32, u32le, "PCM unsigned 32-bit little-endian");
  665. PCM_CODEC (S64BE, S64, s64be, "PCM signed 64-bit big-endian");
  666. PCM_CODEC (S64LE, S64, s64le, "PCM signed 64-bit little-endian");
  667. PCM_CODEC_EXT(VIDC, S16, vidc, "PCM Archimedes VIDC", PCMLUTDecode, pcm_lut_decode_init);
  668. PCM_DECODER (SGA, U8, sga, "PCM SGA");