/src/ffmpeg/libavcodec/pcm.c
Line | Count | Source (jump to first uncovered line) |
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 | | /** |
23 | | * @file |
24 | | * PCM codecs |
25 | | */ |
26 | | |
27 | | #include "config.h" |
28 | | #include "config_components.h" |
29 | | #include "libavutil/attributes.h" |
30 | | #include "libavutil/float_dsp.h" |
31 | | #include "libavutil/mem.h" |
32 | | #include "libavutil/reverse.h" |
33 | | #include "libavutil/thread.h" |
34 | | #include "avcodec.h" |
35 | | #include "bytestream.h" |
36 | | #include "codec_internal.h" |
37 | | #include "decode.h" |
38 | | #include "encode.h" |
39 | | #include "pcm_tablegen.h" |
40 | | |
41 | | static av_cold int pcm_encode_init(AVCodecContext *avctx) |
42 | 0 | { |
43 | 0 | avctx->frame_size = 0; |
44 | 0 | #if !CONFIG_HARDCODED_TABLES |
45 | 0 | switch (avctx->codec->id) { |
46 | 0 | #define INIT_ONCE(id, name) \ |
47 | 0 | case AV_CODEC_ID_PCM_ ## id: \ |
48 | 0 | if (CONFIG_PCM_ ## id ## _ENCODER) { \ |
49 | 0 | static AVOnce init_static_once = AV_ONCE_INIT; \ |
50 | 0 | ff_thread_once(&init_static_once, pcm_ ## name ## _tableinit); \ |
51 | 0 | } \ |
52 | 0 | break |
53 | 0 | INIT_ONCE(ALAW, alaw); |
54 | 0 | INIT_ONCE(MULAW, ulaw); |
55 | 0 | INIT_ONCE(VIDC, vidc); |
56 | 0 | default: |
57 | 0 | break; |
58 | 0 | } |
59 | 0 | #endif |
60 | | |
61 | 0 | avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id); |
62 | 0 | avctx->block_align = avctx->ch_layout.nb_channels * avctx->bits_per_coded_sample / 8; |
63 | 0 | avctx->bit_rate = avctx->block_align * 8LL * avctx->sample_rate; |
64 | |
|
65 | 0 | return 0; |
66 | 0 | } |
67 | | |
68 | | /** |
69 | | * Write PCM samples macro |
70 | | * @param type Datatype of native machine format |
71 | | * @param endian bytestream_put_xxx() suffix |
72 | | * @param src Source pointer (variable name) |
73 | | * @param dst Destination pointer (variable name) |
74 | | * @param n Total number of samples (variable name) |
75 | | * @param shift Bitshift (bits) |
76 | | * @param offset Sample value offset |
77 | | */ |
78 | | #define ENCODE(type, endian, src, dst, n, shift, offset) \ |
79 | 0 | samples_ ## type = (const type *) src; \ |
80 | 0 | for (; n > 0; n--) { \ |
81 | 0 | register type v = (*samples_ ## type++ >> shift) + offset; \ |
82 | 0 | bytestream_put_ ## endian(&dst, v); \ |
83 | 0 | } |
84 | | |
85 | | #define ENCODE_PLANAR(type, endian, dst, n, shift, offset) \ |
86 | 0 | n /= avctx->ch_layout.nb_channels; \ |
87 | 0 | for (c = 0; c < avctx->ch_layout.nb_channels; c++) { \ |
88 | 0 | int i; \ |
89 | 0 | samples_ ## type = (const type *) frame->extended_data[c]; \ |
90 | 0 | for (i = n; i > 0; i--) { \ |
91 | 0 | register type v = (*samples_ ## type++ >> shift) + offset; \ |
92 | 0 | bytestream_put_ ## endian(&dst, v); \ |
93 | 0 | } \ |
94 | 0 | } |
95 | | |
96 | | static int pcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, |
97 | | const AVFrame *frame, int *got_packet_ptr) |
98 | 0 | { |
99 | 0 | int n, c, sample_size, v, ret; |
100 | 0 | const short *samples; |
101 | 0 | unsigned char *dst; |
102 | 0 | const uint8_t *samples_uint8_t; |
103 | 0 | const int16_t *samples_int16_t; |
104 | 0 | const int32_t *samples_int32_t; |
105 | 0 | const int64_t *samples_int64_t; |
106 | 0 | const uint16_t *samples_uint16_t; |
107 | 0 | const uint32_t *samples_uint32_t; |
108 | |
|
109 | 0 | sample_size = av_get_bits_per_sample(avctx->codec->id) / 8; |
110 | 0 | n = frame->nb_samples * avctx->ch_layout.nb_channels; |
111 | 0 | samples = (const short *)frame->data[0]; |
112 | |
|
113 | 0 | if ((ret = ff_get_encode_buffer(avctx, avpkt, n * sample_size, 0)) < 0) |
114 | 0 | return ret; |
115 | 0 | dst = avpkt->data; |
116 | |
|
117 | 0 | switch (avctx->codec->id) { |
118 | 0 | case AV_CODEC_ID_PCM_U32LE: |
119 | 0 | ENCODE(uint32_t, le32, samples, dst, n, 0, 0x80000000) |
120 | 0 | break; |
121 | 0 | case AV_CODEC_ID_PCM_U32BE: |
122 | 0 | ENCODE(uint32_t, be32, samples, dst, n, 0, 0x80000000) |
123 | 0 | break; |
124 | 0 | case AV_CODEC_ID_PCM_S24LE: |
125 | 0 | ENCODE(int32_t, le24, samples, dst, n, 8, 0) |
126 | 0 | break; |
127 | 0 | case AV_CODEC_ID_PCM_S24LE_PLANAR: |
128 | 0 | ENCODE_PLANAR(int32_t, le24, dst, n, 8, 0) |
129 | 0 | break; |
130 | 0 | case AV_CODEC_ID_PCM_S24BE: |
131 | 0 | ENCODE(int32_t, be24, samples, dst, n, 8, 0) |
132 | 0 | break; |
133 | 0 | case AV_CODEC_ID_PCM_U24LE: |
134 | 0 | ENCODE(uint32_t, le24, samples, dst, n, 8, 0x800000) |
135 | 0 | break; |
136 | 0 | case AV_CODEC_ID_PCM_U24BE: |
137 | 0 | ENCODE(uint32_t, be24, samples, dst, n, 8, 0x800000) |
138 | 0 | break; |
139 | 0 | case AV_CODEC_ID_PCM_S24DAUD: |
140 | 0 | for (; n > 0; n--) { |
141 | 0 | uint32_t tmp = ff_reverse[(*samples >> 8) & 0xff] + |
142 | 0 | (ff_reverse[*samples & 0xff] << 8); |
143 | 0 | tmp <<= 4; // sync flags would go here |
144 | 0 | bytestream_put_be24(&dst, tmp); |
145 | 0 | samples++; |
146 | 0 | } |
147 | 0 | break; |
148 | 0 | case AV_CODEC_ID_PCM_U16LE: |
149 | 0 | ENCODE(uint16_t, le16, samples, dst, n, 0, 0x8000) |
150 | 0 | break; |
151 | 0 | case AV_CODEC_ID_PCM_U16BE: |
152 | 0 | ENCODE(uint16_t, be16, samples, dst, n, 0, 0x8000) |
153 | 0 | break; |
154 | 0 | case AV_CODEC_ID_PCM_S8: |
155 | 0 | ENCODE(uint8_t, byte, samples, dst, n, 0, -128) |
156 | 0 | break; |
157 | 0 | case AV_CODEC_ID_PCM_S8_PLANAR: |
158 | 0 | ENCODE_PLANAR(uint8_t, byte, dst, n, 0, -128) |
159 | 0 | 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 | 0 | case AV_CODEC_ID_PCM_S64BE: |
185 | 0 | case AV_CODEC_ID_PCM_F64BE: |
186 | 0 | ENCODE(int64_t, be64, samples, dst, n, 0, 0) |
187 | 0 | break; |
188 | 0 | case AV_CODEC_ID_PCM_F32BE: |
189 | 0 | case AV_CODEC_ID_PCM_S32BE: |
190 | 0 | ENCODE(int32_t, be32, samples, dst, n, 0, 0) |
191 | 0 | break; |
192 | 0 | case AV_CODEC_ID_PCM_S16BE: |
193 | 0 | ENCODE(int16_t, be16, samples, dst, n, 0, 0) |
194 | 0 | break; |
195 | 0 | case AV_CODEC_ID_PCM_S16BE_PLANAR: |
196 | 0 | ENCODE_PLANAR(int16_t, be16, dst, n, 0, 0) |
197 | 0 | break; |
198 | 0 | case AV_CODEC_ID_PCM_F64LE: |
199 | 0 | case AV_CODEC_ID_PCM_F32LE: |
200 | 0 | case AV_CODEC_ID_PCM_S64LE: |
201 | 0 | case AV_CODEC_ID_PCM_S32LE: |
202 | 0 | case AV_CODEC_ID_PCM_S16LE: |
203 | 0 | #endif /* HAVE_BIGENDIAN */ |
204 | 0 | case AV_CODEC_ID_PCM_U8: |
205 | 0 | memcpy(dst, samples, n * sample_size); |
206 | 0 | break; |
207 | | #if HAVE_BIGENDIAN |
208 | | case AV_CODEC_ID_PCM_S16BE_PLANAR: |
209 | | #else |
210 | 0 | case AV_CODEC_ID_PCM_S16LE_PLANAR: |
211 | 0 | case AV_CODEC_ID_PCM_S32LE_PLANAR: |
212 | 0 | #endif /* HAVE_BIGENDIAN */ |
213 | 0 | n /= avctx->ch_layout.nb_channels; |
214 | 0 | for (c = 0; c < avctx->ch_layout.nb_channels; c++) { |
215 | 0 | const uint8_t *src = frame->extended_data[c]; |
216 | 0 | bytestream_put_buffer(&dst, src, n * sample_size); |
217 | 0 | } |
218 | 0 | break; |
219 | 0 | case AV_CODEC_ID_PCM_ALAW: |
220 | 0 | for (; n > 0; n--) { |
221 | 0 | v = *samples++; |
222 | 0 | *dst++ = linear_to_alaw[(v + 32768) >> 2]; |
223 | 0 | } |
224 | 0 | break; |
225 | 0 | case AV_CODEC_ID_PCM_MULAW: |
226 | 0 | for (; n > 0; n--) { |
227 | 0 | v = *samples++; |
228 | 0 | *dst++ = linear_to_ulaw[(v + 32768) >> 2]; |
229 | 0 | } |
230 | 0 | break; |
231 | 0 | case AV_CODEC_ID_PCM_VIDC: |
232 | 0 | for (; n > 0; n--) { |
233 | 0 | v = *samples++; |
234 | 0 | *dst++ = linear_to_vidc[(v + 32768) >> 2]; |
235 | 0 | } |
236 | 0 | break; |
237 | 0 | default: |
238 | 0 | return -1; |
239 | 0 | } |
240 | | |
241 | 0 | *got_packet_ptr = 1; |
242 | 0 | return 0; |
243 | 0 | } |
244 | | |
245 | | typedef struct PCMDecode { |
246 | | short table[256]; |
247 | | void (*vector_fmul_scalar)(float *dst, const float *src, float mul, |
248 | | int len); |
249 | | float scale; |
250 | | } PCMDecode; |
251 | | |
252 | | static av_cold int pcm_decode_init(AVCodecContext *avctx) |
253 | 2.29k | { |
254 | 2.29k | PCMDecode *s = avctx->priv_data; |
255 | 2.29k | AVFloatDSPContext *fdsp; |
256 | 2.29k | int i; |
257 | | |
258 | 2.29k | switch (avctx->codec_id) { |
259 | 0 | case AV_CODEC_ID_PCM_ALAW: |
260 | 0 | for (i = 0; i < 256; i++) |
261 | 0 | s->table[i] = alaw2linear(i); |
262 | 0 | break; |
263 | 0 | case AV_CODEC_ID_PCM_MULAW: |
264 | 0 | for (i = 0; i < 256; i++) |
265 | 0 | s->table[i] = ulaw2linear(i); |
266 | 0 | break; |
267 | 0 | case AV_CODEC_ID_PCM_VIDC: |
268 | 0 | for (i = 0; i < 256; i++) |
269 | 0 | s->table[i] = vidc2linear(i); |
270 | 0 | break; |
271 | 568 | case AV_CODEC_ID_PCM_F16LE: |
272 | 1.15k | case AV_CODEC_ID_PCM_F24LE: |
273 | 1.15k | if (avctx->bits_per_coded_sample < 1 || avctx->bits_per_coded_sample > 24) |
274 | 184 | return AVERROR_INVALIDDATA; |
275 | | |
276 | 970 | s->scale = 1. / (1 << (avctx->bits_per_coded_sample - 1)); |
277 | 970 | fdsp = avpriv_float_dsp_alloc(0); |
278 | 970 | if (!fdsp) |
279 | 0 | return AVERROR(ENOMEM); |
280 | 970 | s->vector_fmul_scalar = fdsp->vector_fmul_scalar; |
281 | 970 | av_free(fdsp); |
282 | 970 | break; |
283 | 1.13k | default: |
284 | 1.13k | break; |
285 | 2.29k | } |
286 | | |
287 | 2.10k | avctx->sample_fmt = avctx->codec->sample_fmts[0]; |
288 | | |
289 | 2.10k | if (avctx->sample_fmt == AV_SAMPLE_FMT_S32) |
290 | 0 | avctx->bits_per_raw_sample = av_get_bits_per_sample(avctx->codec_id); |
291 | | |
292 | 2.10k | return 0; |
293 | 2.29k | } |
294 | | |
295 | | /** |
296 | | * Read PCM samples macro |
297 | | * @param size Data size of native machine format |
298 | | * @param endian bytestream_get_xxx() endian suffix |
299 | | * @param src Source pointer (variable name) |
300 | | * @param dst Destination pointer (variable name) |
301 | | * @param n Total number of samples (variable name) |
302 | | * @param shift Bitshift (bits) |
303 | | * @param offset Sample value offset |
304 | | */ |
305 | | #define DECODE(size, endian, src, dst, n, shift, offset) \ |
306 | 0 | for (; n > 0; n--) { \ |
307 | 0 | uint ## size ## _t v = bytestream_get_ ## endian(&src); \ |
308 | 0 | AV_WN ## size ## A(dst, (uint ## size ## _t)(v - offset) << shift); \ |
309 | 0 | dst += size / 8; \ |
310 | 0 | } |
311 | | |
312 | | #define DECODE_PLANAR(size, endian, src, dst, n, shift, offset) \ |
313 | 0 | n /= channels; \ |
314 | 0 | for (c = 0; c < avctx->ch_layout.nb_channels; c++) { \ |
315 | 0 | int i; \ |
316 | 0 | dst = frame->extended_data[c]; \ |
317 | 0 | for (i = n; i > 0; i--) { \ |
318 | 0 | uint ## size ## _t v = bytestream_get_ ## endian(&src); \ |
319 | 0 | AV_WN ## size ## A(dst, (uint ## size ##_t)(v - offset) << shift); \ |
320 | 0 | dst += size / 8; \ |
321 | 0 | } \ |
322 | 0 | } |
323 | | |
324 | | static int pcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, |
325 | | int *got_frame_ptr, AVPacket *avpkt) |
326 | 1.88M | { |
327 | 1.88M | const uint8_t *src = avpkt->data; |
328 | 1.88M | int buf_size = avpkt->size; |
329 | 1.88M | PCMDecode *s = avctx->priv_data; |
330 | 1.88M | int channels = avctx->ch_layout.nb_channels; |
331 | 1.88M | int sample_size, c, n, ret, samples_per_block; |
332 | 1.88M | uint8_t *samples; |
333 | 1.88M | int32_t *dst_int32_t; |
334 | | |
335 | 1.88M | sample_size = av_get_bits_per_sample(avctx->codec_id) / 8; |
336 | | |
337 | | /* av_get_bits_per_sample returns 0 for AV_CODEC_ID_PCM_DVD */ |
338 | 1.88M | samples_per_block = 1; |
339 | 1.88M | if (avctx->codec_id == AV_CODEC_ID_PCM_LXF) { |
340 | | /* we process 40-bit blocks per channel for LXF */ |
341 | 606k | samples_per_block = 2; |
342 | 606k | sample_size = 5; |
343 | 606k | } |
344 | | |
345 | 1.88M | if (sample_size == 0) { |
346 | 0 | av_log(avctx, AV_LOG_ERROR, "Invalid sample_size\n"); |
347 | 0 | return AVERROR(EINVAL); |
348 | 0 | } |
349 | | |
350 | 1.88M | if (channels == 0) { |
351 | 0 | av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n"); |
352 | 0 | return AVERROR(EINVAL); |
353 | 0 | } |
354 | | |
355 | 1.88M | if (avctx->codec_id != avctx->codec->id) { |
356 | 0 | av_log(avctx, AV_LOG_ERROR, "codec ids mismatch\n"); |
357 | 0 | return AVERROR(EINVAL); |
358 | 0 | } |
359 | | |
360 | 1.88M | n = channels * sample_size; |
361 | | |
362 | 1.88M | if (n && buf_size % n) { |
363 | 975k | if (buf_size < n) { |
364 | 945k | av_log(avctx, AV_LOG_ERROR, |
365 | 945k | "Invalid PCM packet, data has size %d but at least a size of %d was expected\n", |
366 | 945k | buf_size, n); |
367 | 945k | return AVERROR_INVALIDDATA; |
368 | 945k | } else |
369 | 30.0k | buf_size -= buf_size % n; |
370 | 975k | } |
371 | | |
372 | 936k | n = buf_size / sample_size; |
373 | | |
374 | | /* get output buffer */ |
375 | 936k | frame->nb_samples = n * samples_per_block / channels; |
376 | 936k | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
377 | 0 | return ret; |
378 | 936k | samples = frame->data[0]; |
379 | | |
380 | 936k | switch (avctx->codec_id) { |
381 | 0 | case AV_CODEC_ID_PCM_U32LE: |
382 | 0 | DECODE(32, le32, src, samples, n, 0, 0x80000000) |
383 | 0 | break; |
384 | 0 | case AV_CODEC_ID_PCM_U32BE: |
385 | 0 | DECODE(32, be32, src, samples, n, 0, 0x80000000) |
386 | 0 | break; |
387 | 0 | case AV_CODEC_ID_PCM_S24LE: |
388 | 0 | DECODE(32, le24, src, samples, n, 8, 0) |
389 | 0 | break; |
390 | 0 | case AV_CODEC_ID_PCM_S24LE_PLANAR: |
391 | 0 | DECODE_PLANAR(32, le24, src, samples, n, 8, 0); |
392 | 0 | break; |
393 | 0 | case AV_CODEC_ID_PCM_S24BE: |
394 | 0 | DECODE(32, be24, src, samples, n, 8, 0) |
395 | 0 | break; |
396 | 0 | case AV_CODEC_ID_PCM_U24LE: |
397 | 0 | DECODE(32, le24, src, samples, n, 8, 0x800000) |
398 | 0 | break; |
399 | 0 | case AV_CODEC_ID_PCM_U24BE: |
400 | 0 | DECODE(32, be24, src, samples, n, 8, 0x800000) |
401 | 0 | break; |
402 | 0 | case AV_CODEC_ID_PCM_S24DAUD: |
403 | 0 | for (; n > 0; n--) { |
404 | 0 | uint32_t v = bytestream_get_be24(&src); |
405 | 0 | v >>= 4; // sync flags are here |
406 | 0 | AV_WN16A(samples, ff_reverse[(v >> 8) & 0xff] + |
407 | 0 | (ff_reverse[v & 0xff] << 8)); |
408 | 0 | samples += 2; |
409 | 0 | } |
410 | 0 | break; |
411 | 0 | case AV_CODEC_ID_PCM_U16LE: |
412 | 0 | DECODE(16, le16, src, samples, n, 0, 0x8000) |
413 | 0 | break; |
414 | 0 | case AV_CODEC_ID_PCM_U16BE: |
415 | 0 | DECODE(16, be16, src, samples, n, 0, 0x8000) |
416 | 0 | break; |
417 | 0 | case AV_CODEC_ID_PCM_S8: |
418 | 0 | for (; n > 0; n--) |
419 | 0 | *samples++ = *src++ + 128; |
420 | 0 | break; |
421 | 245k | case AV_CODEC_ID_PCM_SGA: |
422 | 15.8M | for (; n > 0; n--) { |
423 | 15.6M | int sign = *src >> 7; |
424 | 15.6M | int magn = *src & 0x7f; |
425 | 15.6M | *samples++ = sign ? 128 - magn : 128 + magn; |
426 | 15.6M | src++; |
427 | 15.6M | } |
428 | 245k | break; |
429 | 0 | case AV_CODEC_ID_PCM_S8_PLANAR: |
430 | 0 | n /= avctx->ch_layout.nb_channels; |
431 | 0 | for (c = 0; c < avctx->ch_layout.nb_channels; c++) { |
432 | 0 | int i; |
433 | 0 | samples = frame->extended_data[c]; |
434 | 0 | for (i = n; i > 0; i--) |
435 | 0 | *samples++ = *src++ + 128; |
436 | 0 | } |
437 | 0 | break; |
438 | | #if HAVE_BIGENDIAN |
439 | | case AV_CODEC_ID_PCM_S64LE: |
440 | | case AV_CODEC_ID_PCM_F64LE: |
441 | | DECODE(64, le64, src, samples, n, 0, 0) |
442 | | break; |
443 | | case AV_CODEC_ID_PCM_S32LE: |
444 | | case AV_CODEC_ID_PCM_F32LE: |
445 | | case AV_CODEC_ID_PCM_F24LE: |
446 | | case AV_CODEC_ID_PCM_F16LE: |
447 | | DECODE(32, le32, src, samples, n, 0, 0) |
448 | | break; |
449 | | case AV_CODEC_ID_PCM_S32LE_PLANAR: |
450 | | DECODE_PLANAR(32, le32, src, samples, n, 0, 0); |
451 | | break; |
452 | | case AV_CODEC_ID_PCM_S16LE: |
453 | | DECODE(16, le16, src, samples, n, 0, 0) |
454 | | break; |
455 | | case AV_CODEC_ID_PCM_S16LE_PLANAR: |
456 | | DECODE_PLANAR(16, le16, src, samples, n, 0, 0); |
457 | | break; |
458 | | case AV_CODEC_ID_PCM_F64BE: |
459 | | case AV_CODEC_ID_PCM_F32BE: |
460 | | case AV_CODEC_ID_PCM_S64BE: |
461 | | case AV_CODEC_ID_PCM_S32BE: |
462 | | case AV_CODEC_ID_PCM_S16BE: |
463 | | #else |
464 | 0 | case AV_CODEC_ID_PCM_S64BE: |
465 | 0 | case AV_CODEC_ID_PCM_F64BE: |
466 | 0 | DECODE(64, be64, src, samples, n, 0, 0) |
467 | 0 | break; |
468 | 0 | case AV_CODEC_ID_PCM_F32BE: |
469 | 0 | case AV_CODEC_ID_PCM_S32BE: |
470 | 0 | DECODE(32, be32, src, samples, n, 0, 0) |
471 | 0 | break; |
472 | 0 | case AV_CODEC_ID_PCM_S16BE: |
473 | 0 | DECODE(16, be16, src, samples, n, 0, 0) |
474 | 0 | break; |
475 | 0 | case AV_CODEC_ID_PCM_S16BE_PLANAR: |
476 | 0 | DECODE_PLANAR(16, be16, src, samples, n, 0, 0); |
477 | 0 | break; |
478 | 0 | case AV_CODEC_ID_PCM_F64LE: |
479 | 0 | case AV_CODEC_ID_PCM_F32LE: |
480 | 230k | case AV_CODEC_ID_PCM_F24LE: |
481 | 452k | case AV_CODEC_ID_PCM_F16LE: |
482 | 452k | case AV_CODEC_ID_PCM_S64LE: |
483 | 452k | case AV_CODEC_ID_PCM_S32LE: |
484 | 452k | case AV_CODEC_ID_PCM_S16LE: |
485 | 452k | #endif /* HAVE_BIGENDIAN */ |
486 | 452k | case AV_CODEC_ID_PCM_U8: |
487 | 452k | memcpy(samples, src, n * sample_size); |
488 | 452k | break; |
489 | | #if HAVE_BIGENDIAN |
490 | | case AV_CODEC_ID_PCM_S16BE_PLANAR: |
491 | | #else |
492 | 0 | case AV_CODEC_ID_PCM_S16LE_PLANAR: |
493 | 0 | case AV_CODEC_ID_PCM_S32LE_PLANAR: |
494 | 0 | #endif /* HAVE_BIGENDIAN */ |
495 | 0 | n /= avctx->ch_layout.nb_channels; |
496 | 0 | for (c = 0; c < avctx->ch_layout.nb_channels; c++) { |
497 | 0 | samples = frame->extended_data[c]; |
498 | 0 | bytestream_get_buffer(&src, samples, n * sample_size); |
499 | 0 | } |
500 | 0 | break; |
501 | 0 | case AV_CODEC_ID_PCM_ALAW: |
502 | 0 | case AV_CODEC_ID_PCM_MULAW: |
503 | 0 | case AV_CODEC_ID_PCM_VIDC: |
504 | 0 | for (; n > 0; n--) { |
505 | 0 | AV_WN16A(samples, s->table[*src++]); |
506 | 0 | samples += 2; |
507 | 0 | } |
508 | 0 | break; |
509 | 239k | case AV_CODEC_ID_PCM_LXF: |
510 | 239k | { |
511 | 239k | int i; |
512 | 239k | n /= channels; |
513 | 515k | for (c = 0; c < channels; c++) { |
514 | 276k | dst_int32_t = (int32_t *)frame->extended_data[c]; |
515 | 4.19M | for (i = 0; i < n; i++) { |
516 | | // extract low 20 bits and expand to 32 bits |
517 | 3.92M | *dst_int32_t++ = ((uint32_t)src[2]<<28) | |
518 | 3.92M | (src[1] << 20) | |
519 | 3.92M | (src[0] << 12) | |
520 | 3.92M | ((src[2] & 0x0F) << 8) | |
521 | 3.92M | src[1]; |
522 | | // extract high 20 bits and expand to 32 bits |
523 | 3.92M | *dst_int32_t++ = ((uint32_t)src[4]<<24) | |
524 | 3.92M | (src[3] << 16) | |
525 | 3.92M | ((src[2] & 0xF0) << 8) | |
526 | 3.92M | (src[4] << 4) | |
527 | 3.92M | (src[3] >> 4); |
528 | 3.92M | src += 5; |
529 | 3.92M | } |
530 | 276k | } |
531 | 239k | break; |
532 | 0 | } |
533 | 0 | default: |
534 | 0 | return -1; |
535 | 936k | } |
536 | | |
537 | 936k | if (avctx->codec_id == AV_CODEC_ID_PCM_F16LE || |
538 | 936k | avctx->codec_id == AV_CODEC_ID_PCM_F24LE) { |
539 | 452k | s->vector_fmul_scalar((float *)frame->extended_data[0], |
540 | 452k | (const float *)frame->extended_data[0], |
541 | 452k | s->scale, FFALIGN(frame->nb_samples * avctx->ch_layout.nb_channels, 4)); |
542 | 452k | } |
543 | | |
544 | 936k | *got_frame_ptr = 1; |
545 | | |
546 | 936k | return buf_size; |
547 | 936k | } |
548 | | |
549 | | #define PCM_ENCODER_0(id_, sample_fmt_, name_, long_name_) |
550 | | #define PCM_ENCODER_1(id_, sample_fmt_, name_, long_name_) \ |
551 | | const FFCodec ff_ ## name_ ## _encoder = { \ |
552 | | .p.name = #name_, \ |
553 | | CODEC_LONG_NAME(long_name_), \ |
554 | | .p.type = AVMEDIA_TYPE_AUDIO, \ |
555 | | .p.id = AV_CODEC_ID_ ## id_, \ |
556 | | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_VARIABLE_FRAME_SIZE | \ |
557 | | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, \ |
558 | | .init = pcm_encode_init, \ |
559 | | FF_CODEC_ENCODE_CB(pcm_encode_frame), \ |
560 | | .p.sample_fmts = (const enum AVSampleFormat[]){ sample_fmt_, \ |
561 | | AV_SAMPLE_FMT_NONE }, \ |
562 | | } |
563 | | |
564 | | #define PCM_ENCODER_2(cf, id, sample_fmt, name, long_name) \ |
565 | | PCM_ENCODER_ ## cf(id, sample_fmt, name, long_name) |
566 | | #define PCM_ENCODER_3(cf, id, sample_fmt, name, long_name) \ |
567 | | PCM_ENCODER_2(cf, id, sample_fmt, name, long_name) |
568 | | #define PCM_ENCODER(id, sample_fmt, name, long_name) \ |
569 | | PCM_ENCODER_3(CONFIG_ ## id ## _ENCODER, id, sample_fmt, name, long_name) |
570 | | |
571 | | #define PCM_DECODER_0(id, sample_fmt, name, long_name) |
572 | | #define PCM_DECODER_1(id_, sample_fmt_, name_, long_name_) \ |
573 | | const FFCodec ff_ ## name_ ## _decoder = { \ |
574 | | .p.name = #name_, \ |
575 | | CODEC_LONG_NAME(long_name_), \ |
576 | | .p.type = AVMEDIA_TYPE_AUDIO, \ |
577 | | .p.id = AV_CODEC_ID_ ## id_, \ |
578 | | .priv_data_size = sizeof(PCMDecode), \ |
579 | | .init = pcm_decode_init, \ |
580 | | FF_CODEC_DECODE_CB(pcm_decode_frame), \ |
581 | | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_PARAM_CHANGE, \ |
582 | | .p.sample_fmts = (const enum AVSampleFormat[]){ sample_fmt_, \ |
583 | | AV_SAMPLE_FMT_NONE }, \ |
584 | | } |
585 | | |
586 | | #define PCM_DECODER_2(cf, id, sample_fmt, name, long_name) \ |
587 | | PCM_DECODER_ ## cf(id, sample_fmt, name, long_name) |
588 | | #define PCM_DECODER_3(cf, id, sample_fmt, name, long_name) \ |
589 | | PCM_DECODER_2(cf, id, sample_fmt, name, long_name) |
590 | | #define PCM_DECODER(id, sample_fmt, name, long_name) \ |
591 | | PCM_DECODER_3(CONFIG_ ## id ## _DECODER, id, sample_fmt, name, long_name) |
592 | | |
593 | | #define PCM_CODEC(id, sample_fmt_, name, long_name_) \ |
594 | | PCM_ENCODER(id, sample_fmt_, name, long_name_); \ |
595 | | PCM_DECODER(id, sample_fmt_, name, long_name_) |
596 | | |
597 | | /* Note: Do not forget to add new entries to the Makefile as well. */ |
598 | | PCM_CODEC (PCM_ALAW, AV_SAMPLE_FMT_S16, pcm_alaw, "PCM A-law / G.711 A-law"); |
599 | | PCM_DECODER(PCM_F16LE, AV_SAMPLE_FMT_FLT, pcm_f16le, "PCM 16.8 floating point little-endian"); |
600 | | PCM_DECODER(PCM_F24LE, AV_SAMPLE_FMT_FLT, pcm_f24le, "PCM 24.0 floating point little-endian"); |
601 | | PCM_CODEC (PCM_F32BE, AV_SAMPLE_FMT_FLT, pcm_f32be, "PCM 32-bit floating point big-endian"); |
602 | | PCM_CODEC (PCM_F32LE, AV_SAMPLE_FMT_FLT, pcm_f32le, "PCM 32-bit floating point little-endian"); |
603 | | PCM_CODEC (PCM_F64BE, AV_SAMPLE_FMT_DBL, pcm_f64be, "PCM 64-bit floating point big-endian"); |
604 | | PCM_CODEC (PCM_F64LE, AV_SAMPLE_FMT_DBL, pcm_f64le, "PCM 64-bit floating point little-endian"); |
605 | | PCM_DECODER(PCM_LXF, AV_SAMPLE_FMT_S32P,pcm_lxf, "PCM signed 20-bit little-endian planar"); |
606 | | PCM_CODEC (PCM_MULAW, AV_SAMPLE_FMT_S16, pcm_mulaw, "PCM mu-law / G.711 mu-law"); |
607 | | PCM_CODEC (PCM_S8, AV_SAMPLE_FMT_U8, pcm_s8, "PCM signed 8-bit"); |
608 | | PCM_CODEC (PCM_S8_PLANAR, AV_SAMPLE_FMT_U8P, pcm_s8_planar, "PCM signed 8-bit planar"); |
609 | | PCM_CODEC (PCM_S16BE, AV_SAMPLE_FMT_S16, pcm_s16be, "PCM signed 16-bit big-endian"); |
610 | | PCM_CODEC (PCM_S16BE_PLANAR, AV_SAMPLE_FMT_S16P,pcm_s16be_planar, "PCM signed 16-bit big-endian planar"); |
611 | | PCM_CODEC (PCM_S16LE, AV_SAMPLE_FMT_S16, pcm_s16le, "PCM signed 16-bit little-endian"); |
612 | | PCM_CODEC (PCM_S16LE_PLANAR, AV_SAMPLE_FMT_S16P,pcm_s16le_planar, "PCM signed 16-bit little-endian planar"); |
613 | | PCM_CODEC (PCM_S24BE, AV_SAMPLE_FMT_S32, pcm_s24be, "PCM signed 24-bit big-endian"); |
614 | | PCM_CODEC (PCM_S24DAUD, AV_SAMPLE_FMT_S16, pcm_s24daud, "PCM D-Cinema audio signed 24-bit"); |
615 | | PCM_CODEC (PCM_S24LE, AV_SAMPLE_FMT_S32, pcm_s24le, "PCM signed 24-bit little-endian"); |
616 | | PCM_CODEC (PCM_S24LE_PLANAR, AV_SAMPLE_FMT_S32P,pcm_s24le_planar, "PCM signed 24-bit little-endian planar"); |
617 | | PCM_CODEC (PCM_S32BE, AV_SAMPLE_FMT_S32, pcm_s32be, "PCM signed 32-bit big-endian"); |
618 | | PCM_CODEC (PCM_S32LE, AV_SAMPLE_FMT_S32, pcm_s32le, "PCM signed 32-bit little-endian"); |
619 | | PCM_CODEC (PCM_S32LE_PLANAR, AV_SAMPLE_FMT_S32P,pcm_s32le_planar, "PCM signed 32-bit little-endian planar"); |
620 | | PCM_CODEC (PCM_U8, AV_SAMPLE_FMT_U8, pcm_u8, "PCM unsigned 8-bit"); |
621 | | PCM_CODEC (PCM_U16BE, AV_SAMPLE_FMT_S16, pcm_u16be, "PCM unsigned 16-bit big-endian"); |
622 | | PCM_CODEC (PCM_U16LE, AV_SAMPLE_FMT_S16, pcm_u16le, "PCM unsigned 16-bit little-endian"); |
623 | | PCM_CODEC (PCM_U24BE, AV_SAMPLE_FMT_S32, pcm_u24be, "PCM unsigned 24-bit big-endian"); |
624 | | PCM_CODEC (PCM_U24LE, AV_SAMPLE_FMT_S32, pcm_u24le, "PCM unsigned 24-bit little-endian"); |
625 | | PCM_CODEC (PCM_U32BE, AV_SAMPLE_FMT_S32, pcm_u32be, "PCM unsigned 32-bit big-endian"); |
626 | | PCM_CODEC (PCM_U32LE, AV_SAMPLE_FMT_S32, pcm_u32le, "PCM unsigned 32-bit little-endian"); |
627 | | PCM_CODEC (PCM_S64BE, AV_SAMPLE_FMT_S64, pcm_s64be, "PCM signed 64-bit big-endian"); |
628 | | PCM_CODEC (PCM_S64LE, AV_SAMPLE_FMT_S64, pcm_s64le, "PCM signed 64-bit little-endian"); |
629 | | PCM_CODEC (PCM_VIDC, AV_SAMPLE_FMT_S16, pcm_vidc, "PCM Archimedes VIDC"); |
630 | | PCM_DECODER(PCM_SGA, AV_SAMPLE_FMT_U8, pcm_sga, "PCM SGA"); |