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