FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pcm.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 213 275 77.5%
Functions: 5 6 83.3%
Branches: 151 186 81.2%

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