FFmpeg coverage


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