../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
243 / 257
94.6%
Branches:
273 / 323
84.5%
Functions:
11 / 11
100.0%

libavcodec/adpcmenc.c
Line Branch Exec Source
1 /*
2 * Copyright (c) 2001-2003 The FFmpeg project
3 *
4 * first version by Francois Revol (revol@free.fr)
5 * fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
6 * by Mike Melanson (melanson@pcisys.net)
7 *
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 #include "config_components.h"
26
27 #include "libavutil/mem.h"
28 #include "libavutil/opt.h"
29
30 #include "avcodec.h"
31 #include "put_bits.h"
32 #include "bytestream.h"
33 #include "adpcm.h"
34 #include "adpcm_data.h"
35 #include "codec_internal.h"
36 #include "encode.h"
37
38 /**
39 * @file
40 * ADPCM encoders
41 * See ADPCM decoder reference documents for codec information.
42 */
43
44 #define CASE_0(codec_id, ...)
45 #define CASE_1(codec_id, ...) \
46 case codec_id: \
47 { __VA_ARGS__ } \
48 break;
49 #define CASE_2(enabled, codec_id, ...) \
50 CASE_ ## enabled(codec_id, __VA_ARGS__)
51 #define CASE_3(config, codec_id, ...) \
52 CASE_2(config, codec_id, __VA_ARGS__)
53 #define CASE(codec, ...) \
54 CASE_3(CONFIG_ ## codec ## _ENCODER, AV_CODEC_ID_ ## codec, __VA_ARGS__)
55
56 typedef struct TrellisPath {
57 int nibble;
58 int prev;
59 } TrellisPath;
60
61 typedef struct TrellisNode {
62 uint32_t ssd;
63 int path;
64 int sample1;
65 int sample2;
66 int step;
67 } TrellisNode;
68
69 typedef struct ADPCMEncodeContext {
70 AVClass *class;
71 int block_size;
72
73 ADPCMChannelStatus status[6];
74 TrellisPath *paths;
75 TrellisNode *node_buf;
76 TrellisNode **nodep_buf;
77 uint8_t *trellis_hash;
78 } ADPCMEncodeContext;
79
80 #define FREEZE_INTERVAL 128
81
82 22 static av_cold int adpcm_encode_init(AVCodecContext *avctx)
83 {
84 22 ADPCMEncodeContext *s = avctx->priv_data;
85 22 int channels = avctx->ch_layout.nb_channels;
86
87 /*
88 * AMV's block size has to match that of the corresponding video
89 * stream. Relax the POT requirement.
90 */
91
1/2
✓ Branch 2 → 3 taken 22 times.
✗ Branch 2 → 6 not taken.
22 if (avctx->codec->id != AV_CODEC_ID_ADPCM_IMA_AMV &&
92
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 22 times.
22 (s->block_size & (s->block_size - 1))) {
93 ✗ av_log(avctx, AV_LOG_ERROR, "block size must be power of 2\n");
94 ✗ return AVERROR(EINVAL);
95 }
96
97
2/2
✓ Branch 6 → 7 taken 6 times.
✓ Branch 6 → 25 taken 16 times.
22 if (avctx->trellis) {
98 int frontier, max_paths;
99
100
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 10 taken 6 times.
6 if ((unsigned)avctx->trellis > 16U) {
101 ✗ av_log(avctx, AV_LOG_ERROR, "invalid trellis size\n");
102 ✗ return AVERROR(EINVAL);
103 }
104
105
1/2
✓ Branch 10 → 11 taken 6 times.
✗ Branch 10 → 14 not taken.
6 if (avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_SSI ||
106
1/2
✓ Branch 11 → 12 taken 6 times.
✗ Branch 11 → 14 not taken.
6 avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_APM ||
107
1/2
✓ Branch 12 → 13 taken 6 times.
✗ Branch 12 → 14 not taken.
6 avctx->codec->id == AV_CODEC_ID_ADPCM_ARGO ||
108
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 16 taken 6 times.
6 avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_WS) {
109 /*
110 * The current trellis implementation doesn't work for extended
111 * runs of samples without periodic resets. Disallow it.
112 */
113 ✗ av_log(avctx, AV_LOG_ERROR, "trellis not supported\n");
114 ✗ return AVERROR_PATCHWELCOME;
115 }
116
117 6 frontier = 1 << avctx->trellis;
118 6 max_paths = frontier * FREEZE_INTERVAL;
119
1/2
✓ Branch 17 → 18 taken 6 times.
✗ Branch 17 → 24 not taken.
6 if (!FF_ALLOC_TYPED_ARRAY(s->paths, max_paths) ||
120
1/2
✓ Branch 19 → 20 taken 6 times.
✗ Branch 19 → 24 not taken.
6 !FF_ALLOC_TYPED_ARRAY(s->node_buf, 2 * frontier) ||
121
1/2
✓ Branch 21 → 22 taken 6 times.
✗ Branch 21 → 24 not taken.
6 !FF_ALLOC_TYPED_ARRAY(s->nodep_buf, 2 * frontier) ||
122
1/2
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 6 times.
6 !FF_ALLOC_TYPED_ARRAY(s->trellis_hash, 65536))
123 ✗ return AVERROR(ENOMEM);
124 }
125
126 22 avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id);
127
128
9/11
✓ Branch 26 → 27 taken 4 times.
✓ Branch 26 → 28 taken 2 times.
✓ Branch 26 → 29 taken 5 times.
✓ Branch 26 → 40 taken 3 times.
✓ Branch 26 → 41 taken 3 times.
✓ Branch 26 → 42 taken 2 times.
✗ Branch 26 → 43 not taken.
✓ Branch 26 → 44 taken 1 time.
✓ Branch 26 → 48 taken 1 time.
✓ Branch 26 → 49 taken 1 time.
✗ Branch 26 → 50 not taken.
22 switch (avctx->codec->id) {
129 4 CASE(ADPCM_IMA_WAV,
130 /* each 16 bits sample gives one nibble
131 and we have 4 bytes per channel overhead */
132 avctx->frame_size = (s->block_size - 4 * channels) * 8 /
133 (4 * channels) + 1;
134 /* seems frame_size isn't taken into account...
135 have to buffer the samples :-( */
136 avctx->block_align = s->block_size;
137 avctx->bits_per_coded_sample = 4;
138 avctx->bit_rate = avctx->block_align * 8LL * avctx->sample_rate / avctx->frame_size;
139 ) /* End of CASE */
140 2 CASE(ADPCM_IMA_QT,
141 avctx->frame_size = 64;
142 avctx->block_align = 34 * channels;
143 ) /* End of CASE */
144
3/4
✗ Branch 30 → 31 not taken.
✓ Branch 30 → 32 taken 5 times.
✓ Branch 38 → 35 taken 35 times.
✓ Branch 38 → 39 taken 5 times.
40 CASE(ADPCM_MS,
145 uint8_t *extradata;
146 /* each 16 bits sample gives one nibble
147 and we have 7 bytes per channel overhead */
148 avctx->frame_size = (s->block_size - 7 * channels) * 2 / channels + 2;
149 avctx->bits_per_coded_sample = 4;
150 avctx->block_align = s->block_size;
151 avctx->bit_rate = avctx->block_align * 8LL * avctx->sample_rate / avctx->frame_size;
152 if (!(avctx->extradata = av_malloc(32 + AV_INPUT_BUFFER_PADDING_SIZE)))
153 return AVERROR(ENOMEM);
154 avctx->extradata_size = 32;
155 extradata = avctx->extradata;
156 bytestream_put_le16(&extradata, avctx->frame_size);
157 bytestream_put_le16(&extradata, 7); /* wNumCoef */
158 for (int i = 0; i < 7; i++) {
159 bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff1[i] * 4);
160 bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff2[i] * 4);
161 }
162 ) /* End of CASE */
163 3 CASE(ADPCM_YAMAHA,
164 avctx->frame_size = s->block_size * 2 / channels;
165 avctx->block_align = s->block_size;
166 ) /* End of CASE */
167 3 CASE(ADPCM_SWF,
168 avctx->frame_size = 4096; /* Hardcoded according to the SWF spec. */
169 avctx->block_align = (2 + channels * (22 + 4 * (avctx->frame_size - 1)) + 7) / 8;
170 ) /* End of CASE */
171 2 case AV_CODEC_ID_ADPCM_IMA_SSI:
172 case AV_CODEC_ID_ADPCM_IMA_ALP:
173 2 avctx->frame_size = s->block_size * 2 / channels;
174 2 avctx->block_align = s->block_size;
175 2 break;
176 ✗ CASE(ADPCM_IMA_AMV,
177 avctx->frame_size = s->block_size;
178 avctx->block_align = 8 + (FFALIGN(avctx->frame_size, 2) / 2);
179 ) /* End of CASE */
180
1/2
✗ Branch 45 → 46 not taken.
✓ Branch 45 → 47 taken 1 time.
1 CASE(ADPCM_IMA_APM,
181 avctx->frame_size = s->block_size * 2 / channels;
182 avctx->block_align = s->block_size;
183
184 if (!(avctx->extradata = av_mallocz(28 + AV_INPUT_BUFFER_PADDING_SIZE)))
185 return AVERROR(ENOMEM);
186 avctx->extradata_size = 28;
187 ) /* End of CASE */
188 1 CASE(ADPCM_ARGO,
189 avctx->frame_size = 32;
190 avctx->block_align = 17 * channels;
191 ) /* End of CASE */
192 1 CASE(ADPCM_IMA_WS,
193 /* each 16 bits sample gives one nibble */
194 avctx->frame_size = s->block_size * 2 / channels;
195 avctx->block_align = s->block_size;
196 ) /* End of CASE */
197 ✗ default:
198 ✗ av_unreachable("there is a case for every codec using adpcm_encode_init()");
199 }
200
201 22 return 0;
202 }
203
204 22 static av_cold int adpcm_encode_close(AVCodecContext *avctx)
205 {
206 22 ADPCMEncodeContext *s = avctx->priv_data;
207 22 av_freep(&s->paths);
208 22 av_freep(&s->node_buf);
209 22 av_freep(&s->nodep_buf);
210 22 av_freep(&s->trellis_hash);
211
212 22 return 0;
213 }
214
215
216 2654604 static inline uint8_t adpcm_ima_compress_sample(ADPCMChannelStatus *c,
217 int16_t sample)
218 {
219 2654604 int delta = sample - c->prev_sample;
220 2654604 int nibble = FFMIN(7, abs(delta) * 4 /
221 2654604 ff_adpcm_step_table[c->step_index]) + (delta < 0) * 8;
222 2654604 c->prev_sample += ((ff_adpcm_step_table[c->step_index] *
223 2654604 ff_adpcm_yamaha_difflookup[nibble]) / 8);
224 2654604 c->prev_sample = av_clip_int16(c->prev_sample);
225 2654604 c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
226 2654604 return nibble;
227 }
228
229 529200 static inline uint8_t adpcm_ima_alp_compress_sample(ADPCMChannelStatus *c, int16_t sample)
230 {
231 529200 const int delta = sample - c->prev_sample;
232 529200 const int step = ff_adpcm_step_table[c->step_index];
233 529200 const int sign = (delta < 0) * 8;
234
235 529200 int nibble = FFMIN(abs(delta) * 4 / step, 7);
236 529200 int diff = (step * nibble) >> 2;
237
2/2
✓ Branch 2 → 3 taken 263832 times.
✓ Branch 2 → 4 taken 265368 times.
529200 if (sign)
238 263832 diff = -diff;
239
240 529200 nibble = sign | nibble;
241
242 529200 c->prev_sample += diff;
243 529200 c->prev_sample = av_clip_int16(c->prev_sample);
244 529200 c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
245 529200 return nibble;
246 }
247
248 1587680 static inline uint8_t adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c,
249 int16_t sample)
250 {
251 1587680 int delta = sample - c->prev_sample;
252 1587680 int diff, step = ff_adpcm_step_table[c->step_index];
253 1587680 int nibble = 8*(delta < 0);
254
255 1587680 delta= abs(delta);
256 1587680 diff = delta + (step >> 3);
257
258
2/2
✓ Branch 2 → 3 taken 435880 times.
✓ Branch 2 → 4 taken 1151800 times.
1587680 if (delta >= step) {
259 435880 nibble |= 4;
260 435880 delta -= step;
261 }
262 1587680 step >>= 1;
263
2/2
✓ Branch 4 → 5 taken 760258 times.
✓ Branch 4 → 6 taken 827422 times.
1587680 if (delta >= step) {
264 760258 nibble |= 2;
265 760258 delta -= step;
266 }
267 1587680 step >>= 1;
268
2/2
✓ Branch 6 → 7 taken 770157 times.
✓ Branch 6 → 8 taken 817523 times.
1587680 if (delta >= step) {
269 770157 nibble |= 1;
270 770157 delta -= step;
271 }
272 1587680 diff -= delta;
273
274
2/2
✓ Branch 8 → 9 taken 789018 times.
✓ Branch 8 → 10 taken 798662 times.
1587680 if (nibble & 8)
275 789018 c->prev_sample -= diff;
276 else
277 798662 c->prev_sample += diff;
278
279 1587680 c->prev_sample = av_clip_int16(c->prev_sample);
280 1587680 c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
281
282 1587680 return nibble;
283 }
284
285 1058480 static inline uint8_t adpcm_ms_compress_sample(ADPCMChannelStatus *c,
286 int16_t sample)
287 {
288 int predictor, nibble, bias;
289
290 1058480 predictor = (((c->sample1) * (c->coeff1)) +
291 1058480 (( c->sample2) * (c->coeff2))) / 64;
292
293 1058480 nibble = sample - predictor;
294
2/2
✓ Branch 2 → 3 taken 530448 times.
✓ Branch 2 → 4 taken 528032 times.
1058480 if (nibble >= 0)
295 530448 bias = c->idelta / 2;
296 else
297 528032 bias = -c->idelta / 2;
298
299 1058480 nibble = (nibble + bias) / c->idelta;
300 1058480 nibble = av_clip_intp2(nibble, 3) & 0x0F;
301
302
2/2
✓ Branch 5 → 6 taken 480162 times.
✓ Branch 5 → 7 taken 578318 times.
1058480 predictor += ((nibble & 0x08) ? (nibble - 0x10) : nibble) * c->idelta;
303
304 1058480 c->sample2 = c->sample1;
305 1058480 c->sample1 = av_clip_int16(predictor);
306
307 1058480 c->idelta = (ff_adpcm_AdaptationTable[nibble] * c->idelta) >> 8;
308
2/2
✓ Branch 8 → 9 taken 56386 times.
✓ Branch 8 → 10 taken 1002094 times.
1058480 if (c->idelta < 16)
309 56386 c->idelta = 16;
310
311 1058480 return nibble;
312 }
313
314 575488 static inline uint8_t adpcm_yamaha_compress_sample(ADPCMChannelStatus *c,
315 int16_t sample)
316 {
317 int nibble, delta;
318
319
2/2
✓ Branch 2 → 3 taken 3 times.
✓ Branch 2 → 4 taken 575485 times.
575488 if (!c->step) {
320 3 c->predictor = 0;
321 3 c->step = 127;
322 }
323
324 575488 delta = sample - c->predictor;
325
326 575488 nibble = FFMIN(7, abs(delta) * 4 / c->step) + (delta < 0) * 8;
327
328 575488 c->predictor += ((c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8);
329 575488 c->predictor = av_clip_int16(c->predictor);
330 575488 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
331 575488 c->step = av_clip(c->step, 127, 24576);
332
333 575488 return nibble;
334 }
335
336 9965 static void adpcm_compress_trellis(AVCodecContext *avctx,
337 const int16_t *samples, uint8_t *dst,
338 ADPCMChannelStatus *c, int n, int stride)
339 {
340 //FIXME 6% faster if frontier is a compile-time constant
341 9965 ADPCMEncodeContext *s = avctx->priv_data;
342 9965 const int frontier = 1 << avctx->trellis;
343 9965 const int version = avctx->codec->id;
344 9965 TrellisPath *paths = s->paths, *p;
345 9965 TrellisNode *node_buf = s->node_buf;
346 9965 TrellisNode **nodep_buf = s->nodep_buf;
347 9965 TrellisNode **nodes = nodep_buf; // nodes[] is always sorted by .ssd
348 9965 TrellisNode **nodes_next = nodep_buf + frontier;
349 9965 int pathn = 0, froze = -1, i, j, k, generation = 0;
350 9965 uint8_t *hash = s->trellis_hash;
351 9965 memset(hash, 0xff, 65536 * sizeof(*hash));
352
353 9965 memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
354 9965 nodes[0] = node_buf + frontier;
355 9965 nodes[0]->ssd = 0;
356 9965 nodes[0]->path = 0;
357 9965 nodes[0]->step = c->step_index;
358 9965 nodes[0]->sample1 = c->sample1;
359 9965 nodes[0]->sample2 = c->sample2;
360
4/4
✓ Branch 2 → 3 taken 9443 times.
✓ Branch 2 → 6 taken 522 times.
✓ Branch 3 → 4 taken 1173 times.
✓ Branch 3 → 6 taken 8270 times.
9965 if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
361
1/2
✓ Branch 4 → 5 taken 1173 times.
✗ Branch 4 → 6 not taken.
1173 version == AV_CODEC_ID_ADPCM_IMA_QT ||
362
2/2
✓ Branch 5 → 6 taken 130 times.
✓ Branch 5 → 7 taken 1043 times.
1173 version == AV_CODEC_ID_ADPCM_IMA_AMV ||
363 version == AV_CODEC_ID_ADPCM_SWF)
364 8922 nodes[0]->sample1 = c->prev_sample;
365
2/2
✓ Branch 7 → 8 taken 525 times.
✓ Branch 7 → 9 taken 9440 times.
9965 if (version == AV_CODEC_ID_ADPCM_MS)
366 525 nodes[0]->step = c->idelta;
367
2/2
✓ Branch 9 → 10 taken 518 times.
✓ Branch 9 → 13 taken 9447 times.
9965 if (version == AV_CODEC_ID_ADPCM_YAMAHA) {
368
2/2
✓ Branch 10 → 11 taken 2 times.
✓ Branch 10 → 12 taken 516 times.
518 if (c->step == 0) {
369 2 nodes[0]->step = 127;
370 2 nodes[0]->sample1 = 0;
371 } else {
372 516 nodes[0]->step = c->step;
373 516 nodes[0]->sample1 = c->predictor;
374 }
375 }
376
377
2/2
✓ Branch 109 → 14 taken 2653688 times.
✓ Branch 109 → 110 taken 9965 times.
2663653 for (i = 0; i < n; i++) {
378 2653688 TrellisNode *t = node_buf + frontier*(i&1);
379 TrellisNode **u;
380 2653688 int sample = samples[i * stride];
381 2653688 int heap_pos = 0;
382 2653688 memset(nodes_next, 0, frontier * sizeof(TrellisNode*));
383
4/4
✓ Branch 93 → 94 taken 81155744 times.
✓ Branch 93 → 95 taken 2467092 times.
✓ Branch 94 → 15 taken 80969148 times.
✓ Branch 94 → 95 taken 186596 times.
83622836 for (j = 0; j < frontier && nodes[j]; j++) {
384 // higher j have higher ssd already, so they're likely
385 // to yield a suboptimal next sample too
386 80969148 const int range = (j < frontier / 2) ? 1 : 0;
387 80969148 const int step = nodes[j]->step;
388 int nidx;
389
2/2
✓ Branch 15 → 16 taken 16060342 times.
✓ Branch 15 → 35 taken 64908806 times.
80969148 if (version == AV_CODEC_ID_ADPCM_MS) {
390 16060342 const int predictor = ((nodes[j]->sample1 * c->coeff1) +
391 16060342 (nodes[j]->sample2 * c->coeff2)) / 64;
392 16060342 const int div = (sample - predictor) / step;
393 16060342 const int nmin = av_clip(div-range, -8, 6);
394 16060342 const int nmax = av_clip(div+range, -7, 7);
395
2/2
✓ Branch 34 → 17 taken 32375529 times.
✓ Branch 34 → 92 taken 16060342 times.
48435871 for (nidx = nmin; nidx <= nmax; nidx++) {
396 32375529 const int nibble = nidx & 0xf;
397 32375529 int dec_sample = predictor + nidx * step;
398 #define STORE_NODE(NAME, STEP_INDEX)\
399 int d;\
400 uint32_t ssd;\
401 int pos;\
402 TrellisNode *u;\
403 uint8_t *h;\
404 dec_sample = av_clip_int16(dec_sample);\
405 d = sample - dec_sample;\
406 ssd = nodes[j]->ssd + d*(unsigned)d;\
407 /* Check for wraparound, skip such samples completely. \
408 * Note, changing ssd to a 64 bit variable would be \
409 * simpler, avoiding this check, but it's slower on \
410 * x86 32 bit at the moment. */\
411 if (ssd < nodes[j]->ssd)\
412 goto next_##NAME;\
413 /* Collapse any two states with the same previous sample value. \
414 * One could also distinguish states by step and by 2nd to last
415 * sample, but the effects of that are negligible.
416 * Since nodes in the previous generation are iterated
417 * through a heap, they're roughly ordered from better to
418 * worse, but not strictly ordered. Therefore, an earlier
419 * node with the same sample value is better in most cases
420 * (and thus the current is skipped), but not strictly
421 * in all cases. Only skipping samples where ssd >=
422 * ssd of the earlier node with the same sample gives
423 * slightly worse quality, though, for some reason. */ \
424 h = &hash[(uint16_t) dec_sample];\
425 if (*h == generation)\
426 goto next_##NAME;\
427 if (heap_pos < frontier) {\
428 pos = heap_pos++;\
429 } else {\
430 /* Try to replace one of the leaf nodes with the new \
431 * one, but try a different slot each time. */\
432 pos = (frontier >> 1) +\
433 (heap_pos & ((frontier >> 1) - 1));\
434 if (ssd > nodes_next[pos]->ssd)\
435 goto next_##NAME;\
436 heap_pos++;\
437 }\
438 *h = generation;\
439 u = nodes_next[pos];\
440 if (!u) {\
441 av_assert1(pathn < FREEZE_INTERVAL << avctx->trellis);\
442 u = t++;\
443 nodes_next[pos] = u;\
444 u->path = pathn++;\
445 }\
446 u->ssd = ssd;\
447 u->step = STEP_INDEX;\
448 u->sample2 = nodes[j]->sample1;\
449 u->sample1 = dec_sample;\
450 paths[u->path].nibble = nibble;\
451 paths[u->path].prev = nodes[j]->path;\
452 /* Sift the newly inserted node up in the heap to \
453 * restore the heap property. */\
454 while (pos > 0) {\
455 int parent = (pos - 1) >> 1;\
456 if (nodes_next[parent]->ssd <= ssd)\
457 break;\
458 FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
459 pos = parent;\
460 }\
461 next_##NAME:;
462
13/14
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 19 taken 32375529 times.
✓ Branch 19 → 20 taken 1544478 times.
✓ Branch 19 → 21 taken 30831051 times.
✓ Branch 21 → 22 taken 16186370 times.
✓ Branch 21 → 23 taken 14644681 times.
✓ Branch 23 → 24 taken 9582628 times.
✓ Branch 23 → 25 taken 5062053 times.
✓ Branch 26 → 27 taken 16186370 times.
✓ Branch 26 → 28 taken 5062053 times.
✓ Branch 29 → 30 taken 20032513 times.
✓ Branch 29 → 31 taken 12264951 times.
✓ Branch 32 → 29 taken 32297464 times.
✓ Branch 32 → 33 taken 1215910 times.
44640480 STORE_NODE(ms, FFMAX(16,
463 (ff_adpcm_AdaptationTable[nibble] * step) >> 8));
464 }
465
4/4
✓ Branch 35 → 36 taken 48704033 times.
✓ Branch 35 → 39 taken 16204773 times.
✓ Branch 36 → 37 taken 32826778 times.
✓ Branch 36 → 39 taken 15877255 times.
64908806 } else if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
466
1/2
✓ Branch 37 → 38 taken 32826778 times.
✗ Branch 37 → 39 not taken.
32826778 version == AV_CODEC_ID_ADPCM_IMA_QT ||
467
2/2
✓ Branch 38 → 39 taken 16225575 times.
✓ Branch 38 → 66 taken 16601203 times.
32826778 version == AV_CODEC_ID_ADPCM_IMA_AMV ||
468 48307603 version == AV_CODEC_ID_ADPCM_SWF) {
469 #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
470 const int predictor = nodes[j]->sample1;\
471 const int div = (sample - predictor) * 4 / STEP_TABLE;\
472 int nmin = av_clip(div - range, -7, 6);\
473 int nmax = av_clip(div + range, -6, 7);\
474 if (nmin <= 0)\
475 nmin--; /* distinguish -0 from +0 */\
476 if (nmax < 0)\
477 nmax--;\
478 for (nidx = nmin; nidx <= nmax; nidx++) {\
479 const int nibble = nidx < 0 ? 7 - nidx : nidx;\
480 int dec_sample = predictor +\
481 (STEP_TABLE *\
482 ff_adpcm_yamaha_difflookup[nibble]) / 8;\
483 STORE_NODE(NAME, STEP_INDEX);\
484 }
485
21/22
✓ Branch 39 → 40 taken 29107208 times.
✓ Branch 39 → 41 taken 19200395 times.
✓ Branch 41 → 42 taken 19427637 times.
✓ Branch 41 → 43 taken 28879966 times.
✓ Branch 44 → 45 taken 53824245 times.
✓ Branch 44 → 46 taken 53484536 times.
✗ Branch 47 → 48 not taken.
✓ Branch 47 → 49 taken 107308781 times.
✓ Branch 49 → 50 taken 6917242 times.
✓ Branch 49 → 51 taken 100391539 times.
✓ Branch 51 → 52 taken 48809614 times.
✓ Branch 51 → 53 taken 51581925 times.
✓ Branch 53 → 54 taken 30220374 times.
✓ Branch 53 → 55 taken 21361551 times.
✓ Branch 56 → 57 taken 48809614 times.
✓ Branch 56 → 58 taken 21361551 times.
✓ Branch 59 → 60 taken 66310011 times.
✓ Branch 59 → 61 taken 40820211 times.
✓ Branch 62 → 59 taken 107130222 times.
✓ Branch 62 → 63 taken 3861154 times.
✓ Branch 64 → 44 taken 107308781 times.
✓ Branch 64 → 65 taken 48307603 times.
196436595 LOOP_NODES(ima, ff_adpcm_step_table[step],
486 av_clip(step + ff_adpcm_index_table[nibble], 0, 88));
487 } else { //AV_CODEC_ID_ADPCM_YAMAHA
488
21/22
✓ Branch 66 → 67 taken 10120353 times.
✓ Branch 66 → 68 taken 6480850 times.
✓ Branch 68 → 69 taken 6548369 times.
✓ Branch 68 → 70 taken 10052834 times.
✓ Branch 71 → 72 taken 18520397 times.
✓ Branch 71 → 73 taken 18416007 times.
✗ Branch 74 → 75 not taken.
✓ Branch 74 → 76 taken 36936404 times.
✓ Branch 76 → 77 taken 3104173 times.
✓ Branch 76 → 78 taken 33832231 times.
✓ Branch 78 → 79 taken 16729641 times.
✓ Branch 78 → 80 taken 17102590 times.
✓ Branch 80 → 81 taken 10186988 times.
✓ Branch 80 → 82 taken 6915602 times.
✓ Branch 83 → 84 taken 16729641 times.
✓ Branch 83 → 85 taken 6915602 times.
✓ Branch 86 → 87 taken 22352974 times.
✓ Branch 86 → 88 taken 13542165 times.
✓ Branch 89 → 86 taken 35895139 times.
✓ Branch 89 → 90 taken 1292269 times.
✓ Branch 91 → 71 taken 36936404 times.
✓ Branch 91 → 92 taken 16601203 times.
67079772 LOOP_NODES(yamaha, step,
489 av_clip((step * ff_adpcm_yamaha_indexscale[nibble]) >> 8,
490 127, 24576));
491 #undef LOOP_NODES
492 #undef STORE_NODE
493 }
494 }
495
496 2653688 u = nodes;
497 2653688 nodes = nodes_next;
498 2653688 nodes_next = u;
499
500 2653688 generation++;
501
2/2
✓ Branch 95 → 96 taken 7297 times.
✓ Branch 95 → 97 taken 2646391 times.
2653688 if (generation == 255) {
502 7297 memset(hash, 0xff, 65536 * sizeof(*hash));
503 7297 generation = 0;
504 }
505
506 // prevent overflow
507
2/2
✓ Branch 97 → 98 taken 4857 times.
✓ Branch 97 → 103 taken 2648831 times.
2653688 if (nodes[0]->ssd > (1 << 28)) {
508
4/4
✓ Branch 100 → 101 taken 147699 times.
✓ Branch 100 → 102 taken 4713 times.
✓ Branch 101 → 99 taken 147555 times.
✓ Branch 101 → 102 taken 144 times.
152412 for (j = 1; j < frontier && nodes[j]; j++)
509 147555 nodes[j]->ssd -= nodes[0]->ssd;
510 4857 nodes[0]->ssd = 0;
511 }
512
513 // merge old paths to save memory
514
2/2
✓ Branch 103 → 104 taken 15511 times.
✓ Branch 103 → 108 taken 2638177 times.
2653688 if (i == froze + FREEZE_INTERVAL) {
515 15511 p = &paths[nodes[0]->path];
516
2/2
✓ Branch 106 → 105 taken 1985408 times.
✓ Branch 106 → 107 taken 15511 times.
2000919 for (k = i; k > froze; k--) {
517 1985408 dst[k] = p->nibble;
518 1985408 p = &paths[p->prev];
519 }
520 15511 froze = i;
521 15511 pathn = 0;
522 // other nodes might use paths that don't coincide with the frozen one.
523 // checking which nodes do so is too slow, so just kill them all.
524 // this also slightly improves quality, but I don't know why.
525 15511 memset(nodes + 1, 0, (frontier - 1) * sizeof(TrellisNode*));
526 }
527 }
528
529 9965 p = &paths[nodes[0]->path];
530
2/2
✓ Branch 112 → 111 taken 668280 times.
✓ Branch 112 → 113 taken 9965 times.
678245 for (i = n - 1; i > froze; i--) {
531 668280 dst[i] = p->nibble;
532 668280 p = &paths[p->prev];
533 }
534
535 9965 c->predictor = nodes[0]->sample1;
536 9965 c->sample1 = nodes[0]->sample1;
537 9965 c->sample2 = nodes[0]->sample2;
538 9965 c->step_index = nodes[0]->step;
539 9965 c->step = nodes[0]->step;
540 9965 c->idelta = nodes[0]->step;
541 9965 }
542
543 #if CONFIG_ADPCM_ARGO_ENCODER
544 16840928 static inline int adpcm_argo_compress_nibble(const ADPCMChannelStatus *cs, int16_t s,
545 int shift, int flag)
546 {
547 int nibble;
548
549
2/2
✓ Branch 2 → 3 taken 8538976 times.
✓ Branch 2 → 4 taken 8301952 times.
16840928 if (flag)
550 8538976 nibble = 4 * s - 8 * cs->sample1 + 4 * cs->sample2;
551 else
552 8301952 nibble = 4 * s - 4 * cs->sample1;
553
554 16840928 return (nibble >> shift) & 0x0F;
555 }
556
557 526279 static int64_t adpcm_argo_compress_block(ADPCMChannelStatus *cs, PutBitContext *pb,
558 const int16_t *samples, int nsamples,
559 int shift, int flag)
560 {
561 526279 int64_t error = 0;
562
563
2/2
✓ Branch 2 → 3 taken 16538 times.
✓ Branch 2 → 7 taken 509741 times.
526279 if (pb) {
564 16538 put_bits(pb, 4, shift - 2);
565 16538 put_bits(pb, 1, 0);
566 16538 put_bits(pb, 1, !!flag);
567 16538 put_bits(pb, 2, 0);
568 }
569
570
2/2
✓ Branch 13 → 8 taken 16840928 times.
✓ Branch 13 → 14 taken 526279 times.
17367207 for (int n = 0; n < nsamples; n++) {
571 /* Compress the nibble, then expand it to see how much precision we've lost. */
572 16840928 int nibble = adpcm_argo_compress_nibble(cs, samples[n], shift, flag);
573 16840928 int16_t sample = ff_adpcm_argo_expand_nibble(cs, nibble, shift, flag);
574
575 16840928 error += abs(samples[n] - sample);
576
577
2/2
✓ Branch 10 → 11 taken 529216 times.
✓ Branch 10 → 12 taken 16311712 times.
16840928 if (pb)
578 529216 put_bits(pb, 4, nibble);
579 }
580
581 526279 return error;
582 }
583 #endif
584
585 19880 static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
586 const AVFrame *frame, int *got_packet_ptr)
587 {
588 int st, pkt_size, ret;
589 const int16_t *samples;
590 const int16_t *const *samples_p;
591 uint8_t *dst;
592 19880 ADPCMEncodeContext *c = avctx->priv_data;
593 19880 int channels = avctx->ch_layout.nb_channels;
594
595 19880 samples = (const int16_t *)frame->data[0];
596 19880 samples_p = (const int16_t *const *)frame->extended_data;
597 19880 st = channels == 2;
598
599
2/2
✓ Branch 2 → 3 taken 19621 times.
✓ Branch 2 → 6 taken 259 times.
19880 if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_SSI ||
600
2/2
✓ Branch 3 → 4 taken 19362 times.
✓ Branch 3 → 6 taken 259 times.
19621 avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_ALP ||
601
2/2
✓ Branch 4 → 5 taken 19103 times.
✓ Branch 4 → 6 taken 259 times.
19362 avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_APM ||
602
2/2
✓ Branch 5 → 6 taken 259 times.
✓ Branch 5 → 7 taken 18844 times.
19103 avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_WS)
603 1036 pkt_size = (frame->nb_samples * channels + 1) / 2;
604 else
605 18844 pkt_size = avctx->block_align;
606
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 19880 times.
19880 if ((ret = ff_get_encode_buffer(avctx, avpkt, pkt_size, 0)) < 0)
607 ✗ return ret;
608 19880 dst = avpkt->data;
609
610
10/12
✓ Branch 11 → 12 taken 783 times.
✓ Branch 11 → 46 taken 8270 times.
✓ Branch 11 → 68 taken 259 times.
✓ Branch 11 → 82 taken 259 times.
✓ Branch 11 → 98 taken 195 times.
✓ Branch 11 → 129 taken 787 times.
✓ Branch 11 → 170 taken 540 times.
✓ Branch 11 → 191 taken 259 times.
✗ Branch 11 → 207 not taken.
✓ Branch 11 → 234 taken 8269 times.
✓ Branch 11 → 255 taken 259 times.
✗ Branch 11 → 271 not taken.
19880 switch(avctx->codec->id) {
611
19/20
✓ Branch 15 → 13 taken 1566 times.
✓ Branch 15 → 16 taken 783 times.
✓ Branch 16 → 17 taken 261 times.
✓ Branch 16 → 34 taken 522 times.
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 261 times.
✓ Branch 23 → 21 taken 522 times.
✓ Branch 23 → 24 taken 261 times.
✓ Branch 28 → 27 taken 265176 times.
✓ Branch 28 → 29 taken 66294 times.
✓ Branch 30 → 26 taken 66294 times.
✓ Branch 30 → 31 taken 33147 times.
✓ Branch 32 → 25 taken 33147 times.
✓ Branch 32 → 33 taken 261 times.
✓ Branch 40 → 37 taken 530352 times.
✓ Branch 40 → 41 taken 132588 times.
✓ Branch 42 → 36 taken 132588 times.
✓ Branch 42 → 43 taken 66294 times.
✓ Branch 44 → 35 taken 66294 times.
✓ Branch 44 → 45 taken 522 times.
1096722 CASE(ADPCM_IMA_WAV,
612 int blocks = (frame->nb_samples - 1) / 8;
613
614 for (int ch = 0; ch < channels; ch++) {
615 ADPCMChannelStatus *status = &c->status[ch];
616 status->prev_sample = samples_p[ch][0];
617 /* status->step_index = 0;
618 XXX: not sure how to init the state machine */
619 bytestream_put_le16(&dst, status->prev_sample);
620 *dst++ = status->step_index;
621 *dst++ = 0; /* unknown */
622 }
623
624 /* stereo: 4 bytes (8 samples) for left, 4 bytes for right */
625 if (avctx->trellis > 0) {
626 uint8_t *buf;
627 if (!FF_ALLOC_TYPED_ARRAY(buf, channels * blocks * 8))
628 return AVERROR(ENOMEM);
629 for (int ch = 0; ch < channels; ch++) {
630 adpcm_compress_trellis(avctx, &samples_p[ch][1],
631 buf + ch * blocks * 8, &c->status[ch],
632 blocks * 8, 1);
633 }
634 for (int i = 0; i < blocks; i++) {
635 for (int ch = 0; ch < channels; ch++) {
636 uint8_t *buf1 = buf + ch * blocks * 8 + i * 8;
637 for (int j = 0; j < 8; j += 2)
638 *dst++ = buf1[j] | (buf1[j + 1] << 4);
639 }
640 }
641 av_free(buf);
642 } else {
643 for (int i = 0; i < blocks; i++) {
644 for (int ch = 0; ch < channels; ch++) {
645 ADPCMChannelStatus *status = &c->status[ch];
646 const int16_t *smp = &samples_p[ch][1 + i * 8];
647 for (int j = 0; j < 8; j += 2) {
648 uint8_t v = adpcm_ima_compress_sample(status, smp[j ]);
649 v |= adpcm_ima_compress_sample(status, smp[j + 1]) << 4;
650 *dst++ = v;
651 }
652 }
653 }
654 }
655 ) /* End of CASE */
656
8/8
✓ Branch 50 → 51 taken 8270 times.
✓ Branch 50 → 57 taken 8270 times.
✓ Branch 55 → 53 taken 529280 times.
✓ Branch 55 → 56 taken 8270 times.
✓ Branch 63 → 58 taken 264640 times.
✓ Branch 63 → 64 taken 8270 times.
✓ Branch 65 → 48 taken 16540 times.
✓ Branch 65 → 66 taken 8270 times.
818730 CASE(ADPCM_IMA_QT,
657 PutBitContext pb;
658 init_put_bits(&pb, dst, pkt_size);
659
660 for (int ch = 0; ch < channels; ch++) {
661 ADPCMChannelStatus *status = &c->status[ch];
662 put_bits(&pb, 9, (status->prev_sample & 0xFFFF) >> 7);
663 put_bits(&pb, 7, status->step_index);
664 if (avctx->trellis > 0) {
665 uint8_t buf[64];
666 adpcm_compress_trellis(avctx, &samples_p[ch][0], buf, status,
667 64, 1);
668 for (int i = 0; i < 64; i++)
669 put_bits(&pb, 4, buf[i ^ 1]);
670 status->prev_sample = status->predictor;
671 } else {
672 for (int i = 0; i < 64; i += 2) {
673 int t1, t2;
674 t1 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i ]);
675 t2 = adpcm_ima_qt_compress_sample(status, samples_p[ch][i + 1]);
676 put_bits(&pb, 4, t2);
677 put_bits(&pb, 4, t1);
678 }
679 }
680 }
681
682 flush_put_bits(&pb);
683 ) /* End of CASE */
684
5/6
✗ Branch 69 → 70 not taken.
✓ Branch 69 → 72 taken 259 times.
✓ Branch 77 → 74 taken 529200 times.
✓ Branch 77 → 78 taken 264600 times.
✓ Branch 79 → 73 taken 264600 times.
✓ Branch 79 → 80 taken 259 times.
794059 CASE(ADPCM_IMA_SSI,
685 PutBitContext pb;
686 init_put_bits(&pb, dst, pkt_size);
687
688 av_assert0(avctx->trellis == 0);
689
690 for (int i = 0; i < frame->nb_samples; i++) {
691 for (int ch = 0; ch < channels; ch++) {
692 put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
693 }
694 }
695
696 flush_put_bits(&pb);
697 ) /* End of CASE */
698
5/6
✗ Branch 83 → 84 not taken.
✓ Branch 83 → 86 taken 259 times.
✓ Branch 93 → 88 taken 264600 times.
✓ Branch 93 → 94 taken 132300 times.
✓ Branch 95 → 87 taken 132300 times.
✓ Branch 95 → 96 taken 259 times.
397159 CASE(ADPCM_IMA_ALP,
699 PutBitContext pb;
700 init_put_bits(&pb, dst, pkt_size);
701
702 av_assert0(avctx->trellis == 0);
703
704 for (int n = frame->nb_samples / 2; n > 0; n--) {
705 for (int ch = 0; ch < channels; ch++) {
706 put_bits(&pb, 4, adpcm_ima_alp_compress_sample(c->status + ch, *samples++));
707 put_bits(&pb, 4, adpcm_ima_alp_compress_sample(c->status + ch, samples[st]));
708 }
709 samples += channels;
710 }
711
712 flush_put_bits(&pb);
713 ) /* End of CASE */
714
12/16
✗ Branch 99 → 100 not taken.
✓ Branch 99 → 102 taken 195 times.
✓ Branch 107 → 104 taken 390 times.
✓ Branch 107 → 108 taken 195 times.
✓ Branch 108 → 109 taken 65 times.
✓ Branch 108 → 119 taken 130 times.
✓ Branch 110 → 111 taken 65 times.
✗ Branch 110 → 112 not taken.
✓ Branch 114 → 115 taken 266175 times.
✗ Branch 114 → 116 not taken.
✓ Branch 117 → 113 taken 266175 times.
✓ Branch 117 → 118 taken 65 times.
✓ Branch 122 → 123 taken 532350 times.
✗ Branch 122 → 125 not taken.
✓ Branch 126 → 120 taken 532350 times.
✓ Branch 126 → 127 taken 130 times.
799175 CASE(ADPCM_SWF,
715 const int n = frame->nb_samples - 1;
716 PutBitContext pb;
717 init_put_bits(&pb, dst, pkt_size);
718
719 /* NB: This is safe as we don't have AV_CODEC_CAP_SMALL_LAST_FRAME. */
720 av_assert0(n == 4095);
721
722 // store AdpcmCodeSize
723 put_bits(&pb, 2, 2); // set 4-bit flash adpcm format
724
725 // init the encoder state
726 for (int i = 0; i < channels; i++) {
727 // clip step so it fits 6 bits
728 c->status[i].step_index = av_clip_uintp2(c->status[i].step_index, 6);
729 put_sbits(&pb, 16, samples[i]);
730 put_bits(&pb, 6, c->status[i].step_index);
731 c->status[i].prev_sample = samples[i];
732 }
733
734 if (avctx->trellis > 0) {
735 uint8_t buf[8190 /* = 2 * n */];
736 adpcm_compress_trellis(avctx, samples + channels, buf,
737 &c->status[0], n, channels);
738 if (channels == 2)
739 adpcm_compress_trellis(avctx, samples + channels + 1,
740 buf + n, &c->status[1], n,
741 channels);
742 for (int i = 0; i < n; i++) {
743 put_bits(&pb, 4, buf[i]);
744 if (channels == 2)
745 put_bits(&pb, 4, buf[n + i]);
746 }
747 } else {
748 for (int i = 1; i < frame->nb_samples; i++) {
749 put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0],
750 samples[channels * i]));
751 if (channels == 2)
752 put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1],
753 samples[2 * i + 1]));
754 }
755 }
756 flush_put_bits(&pb);
757 ) /* End of CASE */
758
23/24
✓ Branch 131 → 130 taken 1573 times.
✓ Branch 131 → 132 taken 787 times.
✓ Branch 133 → 134 taken 7 times.
✓ Branch 133 → 135 taken 1566 times.
✓ Branch 137 → 133 taken 1573 times.
✓ Branch 137 → 138 taken 787 times.
✓ Branch 140 → 139 taken 1573 times.
✓ Branch 140 → 141 taken 787 times.
✓ Branch 144 → 142 taken 1573 times.
✓ Branch 144 → 145 taken 787 times.
✓ Branch 148 → 146 taken 1573 times.
✓ Branch 148 → 149 taken 787 times.
✓ Branch 149 → 150 taken 263 times.
✓ Branch 149 → 164 taken 524 times.
✗ Branch 151 → 152 not taken.
✓ Branch 151 → 153 taken 263 times.
✓ Branch 153 → 154 taken 1 time.
✓ Branch 153 → 158 taken 262 times.
✓ Branch 157 → 156 taken 1017 times.
✓ Branch 157 → 163 taken 1 time.
✓ Branch 162 → 161 taken 264620 times.
✓ Branch 162 → 163 taken 262 times.
✓ Branch 168 → 165 taken 529240 times.
✓ Branch 168 → 169 taken 524 times.
803529 CASE(ADPCM_MS,
759 for (int i = 0; i < channels; i++) {
760 int predictor = 0;
761 *dst++ = predictor;
762 c->status[i].coeff1 = ff_adpcm_AdaptCoeff1[predictor];
763 c->status[i].coeff2 = ff_adpcm_AdaptCoeff2[predictor];
764 }
765 for (int i = 0; i < channels; i++) {
766 if (c->status[i].idelta < 16)
767 c->status[i].idelta = 16;
768 bytestream_put_le16(&dst, c->status[i].idelta);
769 }
770 for (int i = 0; i < channels; i++)
771 c->status[i].sample2= *samples++;
772 for (int i = 0; i < channels; i++) {
773 c->status[i].sample1 = *samples++;
774 bytestream_put_le16(&dst, c->status[i].sample1);
775 }
776 for (int i = 0; i < channels; i++)
777 bytestream_put_le16(&dst, c->status[i].sample2);
778
779 if (avctx->trellis > 0) {
780 const int n = avctx->block_align - 7 * channels;
781 uint8_t *buf = av_malloc(2 * n);
782 if (!buf)
783 return AVERROR(ENOMEM);
784 if (channels == 1) {
785 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], 2 * n,
786 channels);
787 for (int i = 0; i < 2 * n; i += 2)
788 *dst++ = (buf[i] << 4) | buf[i + 1];
789 } else {
790 adpcm_compress_trellis(avctx, samples, buf,
791 &c->status[0], n, channels);
792 adpcm_compress_trellis(avctx, samples + 1, buf + n,
793 &c->status[1], n, channels);
794 for (int i = 0; i < n; i++)
795 *dst++ = (buf[i] << 4) | buf[n + i];
796 }
797 av_free(buf);
798 } else {
799 for (int i = 7 * channels; i < avctx->block_align; i++) {
800 int nibble;
801 nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++) << 4;
802 nibble |= adpcm_ms_compress_sample(&c->status[st], *samples++);
803 *dst++ = nibble;
804 }
805 }
806 ) /* End of CASE */
807
8/12
✓ Branch 170 → 171 taken 259 times.
✓ Branch 170 → 185 taken 281 times.
✗ Branch 172 → 173 not taken.
✓ Branch 172 → 174 taken 259 times.
✗ Branch 174 → 175 not taken.
✓ Branch 174 → 179 taken 259 times.
✗ Branch 178 → 177 not taken.
✗ Branch 178 → 184 not taken.
✓ Branch 183 → 182 taken 265216 times.
✓ Branch 183 → 184 taken 259 times.
✓ Branch 189 → 186 taken 287744 times.
✓ Branch 189 → 190 taken 281 times.
553500 CASE(ADPCM_YAMAHA,
808 int n = frame->nb_samples / 2;
809 if (avctx->trellis > 0) {
810 uint8_t *buf = av_malloc(2 * n * 2);
811 if (!buf)
812 return AVERROR(ENOMEM);
813 n *= 2;
814 if (channels == 1) {
815 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
816 channels);
817 for (int i = 0; i < n; i += 2)
818 *dst++ = buf[i] | (buf[i + 1] << 4);
819 } else {
820 adpcm_compress_trellis(avctx, samples, buf,
821 &c->status[0], n, channels);
822 adpcm_compress_trellis(avctx, samples + 1, buf + n,
823 &c->status[1], n, channels);
824 for (int i = 0; i < n; i++)
825 *dst++ = buf[i] | (buf[n + i] << 4);
826 }
827 av_free(buf);
828 } else
829 for (n *= channels; n > 0; n--) {
830 int nibble;
831 nibble = adpcm_yamaha_compress_sample(&c->status[ 0], *samples++);
832 nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
833 *dst++ = nibble;
834 }
835 ) /* End of CASE */
836
5/6
✗ Branch 192 → 193 not taken.
✓ Branch 192 → 195 taken 259 times.
✓ Branch 202 → 197 taken 264600 times.
✓ Branch 202 → 203 taken 132300 times.
✓ Branch 204 → 196 taken 132300 times.
✓ Branch 204 → 205 taken 259 times.
397159 CASE(ADPCM_IMA_APM,
837 PutBitContext pb;
838 init_put_bits(&pb, dst, pkt_size);
839
840 av_assert0(avctx->trellis == 0);
841
842 for (int n = frame->nb_samples / 2; n > 0; n--) {
843 for (int ch = 0; ch < channels; ch++) {
844 put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
845 put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, samples[st]));
846 }
847 samples += channels;
848 }
849
850 flush_put_bits(&pb);
851 ) /* End of CASE */
852
0/12
✗ Branch 207 → 208 not taken.
✗ Branch 207 → 210 not taken.
✗ Branch 214 → 215 not taken.
✗ Branch 214 → 224 not taken.
✗ Branch 216 → 217 not taken.
✗ Branch 216 → 218 not taken.
✗ Branch 222 → 220 not taken.
✗ Branch 222 → 223 not taken.
✗ Branch 229 → 225 not taken.
✗ Branch 229 → 230 not taken.
✗ Branch 230 → 231 not taken.
✗ Branch 230 → 233 not taken.
✗ CASE(ADPCM_IMA_AMV,
853 av_assert0(channels == 1);
854
855 c->status[0].prev_sample = *samples;
856 bytestream_put_le16(&dst, c->status[0].prev_sample);
857 bytestream_put_byte(&dst, c->status[0].step_index);
858 bytestream_put_byte(&dst, 0);
859 bytestream_put_le32(&dst, avctx->frame_size);
860
861 if (avctx->trellis > 0) {
862 const int n = frame->nb_samples >> 1;
863 uint8_t *buf = av_malloc(2 * n);
864
865 if (!buf)
866 return AVERROR(ENOMEM);
867
868 adpcm_compress_trellis(avctx, samples, buf, &c->status[0], 2 * n, channels);
869 for (int i = 0; i < n; i++)
870 bytestream_put_byte(&dst, (buf[2 * i] << 4) | buf[2 * i + 1]);
871
872 samples += 2 * n;
873 av_free(buf);
874 } else for (int n = frame->nb_samples >> 1; n > 0; n--) {
875 int nibble;
876 nibble = adpcm_ima_compress_sample(&c->status[0], *samples++) << 4;
877 nibble |= adpcm_ima_compress_sample(&c->status[0], *samples++) & 0x0F;
878 bytestream_put_byte(&dst, nibble);
879 }
880
881 if (avctx->frame_size & 1) {
882 int nibble = adpcm_ima_compress_sample(&c->status[0], *samples++) << 4;
883 bytestream_put_byte(&dst, nibble);
884 }
885 ) /* End of CASE */
886
13/14
✗ Branch 235 → 236 not taken.
✓ Branch 235 → 238 taken 8269 times.
✓ Branch 242 → 243 taken 75397 times.
✓ Branch 242 → 244 taken 434344 times.
✓ Branch 245 → 246 taken 509986 times.
✓ Branch 245 → 247 taken 254748 times.
✓ Branch 246 → 241 taken 509741 times.
✓ Branch 246 → 247 taken 245 times.
✓ Branch 248 → 249 taken 255634 times.
✓ Branch 248 → 250 taken 15897 times.
✓ Branch 249 → 240 taken 254993 times.
✓ Branch 249 → 250 taken 641 times.
✓ Branch 252 → 239 taken 16538 times.
✓ Branch 252 → 253 taken 8269 times.
789541 CASE(ADPCM_ARGO,
887 PutBitContext pb;
888 init_put_bits(&pb, dst, pkt_size);
889
890 av_assert0(frame->nb_samples == 32);
891
892 for (int ch = 0; ch < channels; ch++) {
893 int64_t error = INT64_MAX, tmperr = INT64_MAX;
894 int shift = 2, flag = 0;
895 int saved1 = c->status[ch].sample1;
896 int saved2 = c->status[ch].sample2;
897
898 /* Find the optimal coefficients, bail early if we find a perfect result. */
899 for (int s = 2; s < 18 && tmperr != 0; s++) {
900 for (int f = 0; f < 2 && tmperr != 0; f++) {
901 c->status[ch].sample1 = saved1;
902 c->status[ch].sample2 = saved2;
903 tmperr = adpcm_argo_compress_block(c->status + ch, NULL, samples_p[ch],
904 frame->nb_samples, s, f);
905 if (tmperr < error) {
906 shift = s;
907 flag = f;
908 error = tmperr;
909 }
910 }
911 }
912
913 /* Now actually do the encode. */
914 c->status[ch].sample1 = saved1;
915 c->status[ch].sample2 = saved2;
916 adpcm_argo_compress_block(c->status + ch, &pb, samples_p[ch],
917 frame->nb_samples, shift, flag);
918 }
919
920 flush_put_bits(&pb);
921 ) /* End of CASE */
922
5/6
✗ Branch 256 → 257 not taken.
✓ Branch 256 → 259 taken 259 times.
✓ Branch 266 → 261 taken 264600 times.
✓ Branch 266 → 267 taken 132300 times.
✓ Branch 268 → 260 taken 132300 times.
✓ Branch 268 → 269 taken 259 times.
397159 CASE(ADPCM_IMA_WS,
923 PutBitContext pb;
924 init_put_bits(&pb, dst, pkt_size);
925
926 av_assert0(avctx->trellis == 0);
927 for (int n = frame->nb_samples / 2; n > 0; n--) {
928 /* stereo: 1 byte (2 samples) for left, 1 byte for right */
929 for (int ch = 0; ch < channels; ch++) {
930 int t1, t2;
931 t1 = adpcm_ima_compress_sample(&c->status[ch], *samples++);
932 t2 = adpcm_ima_compress_sample(&c->status[ch], samples[st]);
933 put_bits(&pb, 4, t2);
934 put_bits(&pb, 4, t1);
935 }
936 samples += channels;
937 }
938 flush_put_bits(&pb);
939 ) /* End of CASE */
940 ✗ default:
941 ✗ return AVERROR(EINVAL);
942 }
943
944 19880 *got_packet_ptr = 1;
945 19880 return 0;
946 }
947
948 static const enum AVSampleFormat sample_fmts[] = {
949 AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
950 };
951
952 static const enum AVSampleFormat sample_fmts_p[] = {
953 AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_NONE
954 };
955
956 static const AVChannelLayout ch_layouts_mono_stereo[] = {
957 AV_CHANNEL_LAYOUT_MONO,
958 AV_CHANNEL_LAYOUT_STEREO,
959 { 0 },
960 };
961
962 static const AVOption options[] = {
963 {
964 .name = "block_size",
965 .help = "set the block size",
966 .offset = offsetof(ADPCMEncodeContext, block_size),
967 .type = AV_OPT_TYPE_INT,
968 .default_val = {.i64 = 1024},
969 .min = 32,
970 .max = 8192, /* Is this a reasonable upper limit? */
971 .flags = AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
972 },
973 { NULL }
974 };
975
976 static const AVClass adpcm_encoder_class = {
977 .class_name = "ADPCM encoder",
978 .item_name = av_default_item_name,
979 .option = options,
980 .version = LIBAVUTIL_VERSION_INT,
981 };
982
983 #define ADPCM_ENCODER_0(id_, name_, sample_fmts_, capabilities_, long_name_, ...)
984 #define ADPCM_ENCODER_1(id_, name_, sample_fmts_, capabilities_, long_name_, ...) \
985 const FFCodec ff_ ## name_ ## _encoder = { \
986 .p.name = #name_, \
987 CODEC_LONG_NAME(long_name_), \
988 .p.type = AVMEDIA_TYPE_AUDIO, \
989 .p.id = id_, \
990 .p.capabilities = capabilities_ | AV_CODEC_CAP_DR1 | \
991 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, \
992 CODEC_SAMPLEFMTS_ARRAY(sample_fmts_), \
993 .priv_data_size = sizeof(ADPCMEncodeContext), \
994 .init = adpcm_encode_init, \
995 FF_CODEC_ENCODE_CB(adpcm_encode_frame), \
996 .close = adpcm_encode_close, \
997 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, \
998 __VA_ARGS__, \
999 };
1000 #define ADPCM_ENCODER_2(enabled, codec_id, name, sample_fmts, capabilities, long_name, ...) \
1001 ADPCM_ENCODER_ ## enabled(codec_id, name, sample_fmts, capabilities, long_name, __VA_ARGS__)
1002 #define ADPCM_ENCODER_3(config, codec_id, name, sample_fmts, capabilities, long_name, ...) \
1003 ADPCM_ENCODER_2(config, codec_id, name, sample_fmts, capabilities, long_name, __VA_ARGS__)
1004 #define ADPCM_ENCODER(codec, name, sample_fmts, capabilities, long_name, ...) \
1005 ADPCM_ENCODER_3(CONFIG_ ## codec ## _ENCODER, AV_CODEC_ID_ ## codec, \
1006 name, sample_fmts, capabilities, long_name, __VA_ARGS__)
1007
1008 #define MONO_STEREO CODEC_CH_LAYOUTS_ARRAY(ch_layouts_mono_stereo)
1009 #define AVCLASS .p.priv_class = &adpcm_encoder_class
1010
1011 ADPCM_ENCODER(ADPCM_ARGO, adpcm_argo, sample_fmts_p, 0, "ADPCM Argonaut Games", MONO_STEREO)
1012 ADPCM_ENCODER(ADPCM_IMA_AMV, adpcm_ima_amv, sample_fmts, 0, "ADPCM IMA AMV", CODEC_CH_LAYOUTS(AV_CHANNEL_LAYOUT_MONO), CODEC_SAMPLERATES(22050), AVCLASS)
1013 ADPCM_ENCODER(ADPCM_IMA_APM, adpcm_ima_apm, sample_fmts, AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Ubisoft APM", MONO_STEREO, AVCLASS)
1014 ADPCM_ENCODER(ADPCM_IMA_ALP, adpcm_ima_alp, sample_fmts, AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA High Voltage Software ALP", MONO_STEREO, AVCLASS)
1015 ADPCM_ENCODER(ADPCM_IMA_QT, adpcm_ima_qt, sample_fmts_p, 0, "ADPCM IMA QuickTime", MONO_STEREO)
1016 ADPCM_ENCODER(ADPCM_IMA_SSI, adpcm_ima_ssi, sample_fmts, AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Simon & Schuster Interactive", MONO_STEREO, AVCLASS)
1017 ADPCM_ENCODER(ADPCM_IMA_WAV, adpcm_ima_wav, sample_fmts_p, 0, "ADPCM IMA WAV", MONO_STEREO, AVCLASS)
1018 ADPCM_ENCODER(ADPCM_IMA_WS, adpcm_ima_ws, sample_fmts, AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Westwood", MONO_STEREO, AVCLASS)
1019 ADPCM_ENCODER(ADPCM_MS, adpcm_ms, sample_fmts, 0, "ADPCM Microsoft", MONO_STEREO, AVCLASS)
1020 ADPCM_ENCODER(ADPCM_SWF, adpcm_swf, sample_fmts, 0, "ADPCM Shockwave Flash", MONO_STEREO, CODEC_SAMPLERATES(11025, 22050, 44100))
1021 ADPCM_ENCODER(ADPCM_YAMAHA, adpcm_yamaha, sample_fmts, 0, "ADPCM Yamaha", MONO_STEREO, AVCLASS)
1022