../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
216 / 282
76.6%
Branches:
151 / 186
81.2%
Functions:
5 / 6
83.3%

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