FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/adpcmenc.c
Date: 2026-09-01 23:28:12
Exec Total Coverage
Lines: 243 257 94.6%
Functions: 11 11 100.0%
Branches: 270 323 83.6%

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 21 static av_cold int adpcm_encode_init(AVCodecContext *avctx)
83 {
84 21 ADPCMEncodeContext *s = avctx->priv_data;
85 21 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 0 taken 21 times.
✗ Branch 1 not taken.
21 if (avctx->codec->id != AV_CODEC_ID_ADPCM_IMA_AMV &&
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 (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 0 taken 5 times.
✓ Branch 1 taken 16 times.
21 if (avctx->trellis) {
98 int frontier, max_paths;
99
100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 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 0 taken 5 times.
✗ Branch 1 not taken.
5 if (avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_SSI ||
106
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_APM ||
107
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 avctx->codec->id == AV_CODEC_ID_ADPCM_ARGO ||
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 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 5 frontier = 1 << avctx->trellis;
118 5 max_paths = frontier * FREEZE_INTERVAL;
119
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 if (!FF_ALLOC_TYPED_ARRAY(s->paths, max_paths) ||
120
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 !FF_ALLOC_TYPED_ARRAY(s->node_buf, 2 * frontier) ||
121
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 !FF_ALLOC_TYPED_ARRAY(s->nodep_buf, 2 * frontier) ||
122
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
5 !FF_ALLOC_TYPED_ARRAY(s->trellis_hash, 65536))
123 return AVERROR(ENOMEM);
124 }
125
126 21 avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id);
127
128
9/11
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 1 times.
✓ Branch 9 taken 1 times.
✗ Branch 10 not taken.
21 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 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 7 taken 28 times.
✓ Branch 8 taken 4 times.
32 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 1 not taken.
✓ Branch 2 taken 1 times.
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 21 return 0;
202 }
203
204 21 static av_cold int adpcm_encode_close(AVCodecContext *avctx)
205 {
206 21 ADPCMEncodeContext *s = avctx->priv_data;
207 21 av_freep(&s->paths);
208 21 av_freep(&s->node_buf);
209 21 av_freep(&s->nodep_buf);
210 21 av_freep(&s->trellis_hash);
211
212 21 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 0 taken 263832 times.
✓ Branch 1 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 0 taken 435880 times.
✓ Branch 1 taken 1151800 times.
1587680 if (delta >= step) {
259 435880 nibble |= 4;
260 435880 delta -= step;
261 }
262 1587680 step >>= 1;
263
2/2
✓ Branch 0 taken 760258 times.
✓ Branch 1 taken 827422 times.
1587680 if (delta >= step) {
264 760258 nibble |= 2;
265 760258 delta -= step;
266 }
267 1587680 step >>= 1;
268
2/2
✓ Branch 0 taken 770157 times.
✓ Branch 1 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 0 taken 789018 times.
✓ Branch 1 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 0 taken 530448 times.
✓ Branch 1 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 0 taken 480162 times.
✓ Branch 1 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 0 taken 56386 times.
✓ Branch 1 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 0 taken 3 times.
✓ Branch 1 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 9964 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 9964 ADPCMEncodeContext *s = avctx->priv_data;
342 9964 const int frontier = 1 << avctx->trellis;
343 9964 const int version = avctx->codec->id;
344 9964 TrellisPath *paths = s->paths, *p;
345 9964 TrellisNode *node_buf = s->node_buf;
346 9964 TrellisNode **nodep_buf = s->nodep_buf;
347 9964 TrellisNode **nodes = nodep_buf; // nodes[] is always sorted by .ssd
348 9964 TrellisNode **nodes_next = nodep_buf + frontier;
349 9964 int pathn = 0, froze = -1, i, j, k, generation = 0;
350 9964 uint8_t *hash = s->trellis_hash;
351 9964 memset(hash, 0xff, 65536 * sizeof(*hash));
352
353 9964 memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
354 9964 nodes[0] = node_buf + frontier;
355 9964 nodes[0]->ssd = 0;
356 9964 nodes[0]->path = 0;
357 9964 nodes[0]->step = c->step_index;
358 9964 nodes[0]->sample1 = c->sample1;
359 9964 nodes[0]->sample2 = c->sample2;
360
4/4
✓ Branch 0 taken 9442 times.
✓ Branch 1 taken 522 times.
✓ Branch 2 taken 1172 times.
✓ Branch 3 taken 8270 times.
9964 if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
361
1/2
✓ Branch 0 taken 1172 times.
✗ Branch 1 not taken.
1172 version == AV_CODEC_ID_ADPCM_IMA_QT ||
362
2/2
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 1042 times.
1172 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 0 taken 524 times.
✓ Branch 1 taken 9440 times.
9964 if (version == AV_CODEC_ID_ADPCM_MS)
366 524 nodes[0]->step = c->idelta;
367
2/2
✓ Branch 0 taken 518 times.
✓ Branch 1 taken 9446 times.
9964 if (version == AV_CODEC_ID_ADPCM_YAMAHA) {
368
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 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 0 taken 2651654 times.
✓ Branch 1 taken 9964 times.
2661618 for (i = 0; i < n; i++) {
378 2651654 TrellisNode *t = node_buf + frontier*(i&1);
379 TrellisNode **u;
380 2651654 int sample = samples[i * stride];
381 2651654 int heap_pos = 0;
382 2651654 memset(nodes_next, 0, frontier * sizeof(TrellisNode*));
383
4/4
✓ Branch 0 taken 81092362 times.
✓ Branch 1 taken 2465218 times.
✓ Branch 2 taken 80905926 times.
✓ Branch 3 taken 186436 times.
83557580 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 80905926 const int range = (j < frontier / 2) ? 1 : 0;
387 80905926 const int step = nodes[j]->step;
388 int nidx;
389
2/2
✓ Branch 0 taken 15997120 times.
✓ Branch 1 taken 64908806 times.
80905926 if (version == AV_CODEC_ID_ADPCM_MS) {
390 15997120 const int predictor = ((nodes[j]->sample1 * c->coeff1) +
391 15997120 (nodes[j]->sample2 * c->coeff2)) / 64;
392 15997120 const int div = (sample - predictor) / step;
393 15997120 const int nmin = av_clip(div-range, -8, 6);
394 15997120 const int nmax = av_clip(div+range, -7, 7);
395
2/2
✓ Branch 0 taken 32248194 times.
✓ Branch 1 taken 15997120 times.
48245314 for (nidx = nmin; nidx <= nmax; nidx++) {
396 32248194 const int nibble = nidx & 0xf;
397 32248194 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 0 not taken.
✓ Branch 1 taken 32248194 times.
✓ Branch 2 taken 1521733 times.
✓ Branch 3 taken 30726461 times.
✓ Branch 4 taken 16122660 times.
✓ Branch 5 taken 14603801 times.
✓ Branch 6 taken 9554500 times.
✓ Branch 7 taken 5049301 times.
✓ Branch 8 taken 16122660 times.
✓ Branch 9 taken 5049301 times.
✓ Branch 10 taken 19960642 times.
✓ Branch 11 taken 12223297 times.
✓ Branch 12 taken 32183939 times.
✓ Branch 13 taken 1211319 times.
44471491 STORE_NODE(ms, FFMAX(16,
463 (ff_adpcm_AdaptationTable[nibble] * step) >> 8));
464 }
465
4/4
✓ Branch 0 taken 48704033 times.
✓ Branch 1 taken 16204773 times.
✓ Branch 2 taken 32826778 times.
✓ Branch 3 taken 15877255 times.
64908806 } else if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
466
1/2
✓ Branch 0 taken 32826778 times.
✗ Branch 1 not taken.
32826778 version == AV_CODEC_ID_ADPCM_IMA_QT ||
467
2/2
✓ Branch 0 taken 16225575 times.
✓ Branch 1 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 0 taken 29107208 times.
✓ Branch 1 taken 19200395 times.
✓ Branch 2 taken 19427637 times.
✓ Branch 3 taken 28879966 times.
✓ Branch 4 taken 53824245 times.
✓ Branch 5 taken 53484536 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 107308781 times.
✓ Branch 8 taken 6917242 times.
✓ Branch 9 taken 100391539 times.
✓ Branch 10 taken 48809614 times.
✓ Branch 11 taken 51581925 times.
✓ Branch 12 taken 30220374 times.
✓ Branch 13 taken 21361551 times.
✓ Branch 14 taken 48809614 times.
✓ Branch 15 taken 21361551 times.
✓ Branch 16 taken 66310011 times.
✓ Branch 17 taken 40820211 times.
✓ Branch 18 taken 107130222 times.
✓ Branch 19 taken 3861154 times.
✓ Branch 20 taken 107308781 times.
✓ Branch 21 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 0 taken 10120353 times.
✓ Branch 1 taken 6480850 times.
✓ Branch 2 taken 6548369 times.
✓ Branch 3 taken 10052834 times.
✓ Branch 4 taken 18520397 times.
✓ Branch 5 taken 18416007 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 36936404 times.
✓ Branch 8 taken 3104173 times.
✓ Branch 9 taken 33832231 times.
✓ Branch 10 taken 16729641 times.
✓ Branch 11 taken 17102590 times.
✓ Branch 12 taken 10186988 times.
✓ Branch 13 taken 6915602 times.
✓ Branch 14 taken 16729641 times.
✓ Branch 15 taken 6915602 times.
✓ Branch 16 taken 22352974 times.
✓ Branch 17 taken 13542165 times.
✓ Branch 18 taken 35895139 times.
✓ Branch 19 taken 1292269 times.
✓ Branch 20 taken 36936404 times.
✓ Branch 21 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 2651654 u = nodes;
497 2651654 nodes = nodes_next;
498 2651654 nodes_next = u;
499
500 2651654 generation++;
501
2/2
✓ Branch 0 taken 7290 times.
✓ Branch 1 taken 2644364 times.
2651654 if (generation == 255) {
502 7290 memset(hash, 0xff, 65536 * sizeof(*hash));
503 7290 generation = 0;
504 }
505
506 // prevent overflow
507
2/2
✓ Branch 0 taken 4857 times.
✓ Branch 1 taken 2646797 times.
2651654 if (nodes[0]->ssd > (1 << 28)) {
508
4/4
✓ Branch 0 taken 147699 times.
✓ Branch 1 taken 4713 times.
✓ Branch 2 taken 147555 times.
✓ Branch 3 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 0 taken 15496 times.
✓ Branch 1 taken 2636158 times.
2651654 if (i == froze + FREEZE_INTERVAL) {
515 15496 p = &paths[nodes[0]->path];
516
2/2
✓ Branch 0 taken 1983488 times.
✓ Branch 1 taken 15496 times.
1998984 for (k = i; k > froze; k--) {
517 1983488 dst[k] = p->nibble;
518 1983488 p = &paths[p->prev];
519 }
520 15496 froze = i;
521 15496 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 15496 memset(nodes + 1, 0, (frontier - 1) * sizeof(TrellisNode*));
526 }
527 }
528
529 9964 p = &paths[nodes[0]->path];
530
2/2
✓ Branch 0 taken 668166 times.
✓ Branch 1 taken 9964 times.
678130 for (i = n - 1; i > froze; i--) {
531 668166 dst[i] = p->nibble;
532 668166 p = &paths[p->prev];
533 }
534
535 9964 c->predictor = nodes[0]->sample1;
536 9964 c->sample1 = nodes[0]->sample1;
537 9964 c->sample2 = nodes[0]->sample2;
538 9964 c->step_index = nodes[0]->step;
539 9964 c->step = nodes[0]->step;
540 9964 c->idelta = nodes[0]->step;
541 9964 }
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 0 taken 8538976 times.
✓ Branch 1 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 0 taken 16538 times.
✓ Branch 1 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 0 taken 16840928 times.
✓ Branch 1 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 0 taken 529216 times.
✓ Branch 1 taken 16311712 times.
16840928 if (pb)
578 529216 put_bits(pb, 4, nibble);
579 }
580
581 526279 return error;
582 }
583 #endif
584
585 19879 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 19879 ADPCMEncodeContext *c = avctx->priv_data;
593 19879 int channels = avctx->ch_layout.nb_channels;
594
595 19879 samples = (const int16_t *)frame->data[0];
596 19879 samples_p = (const int16_t *const *)frame->extended_data;
597 19879 st = channels == 2;
598
599
2/2
✓ Branch 0 taken 19620 times.
✓ Branch 1 taken 259 times.
19879 if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_SSI ||
600
2/2
✓ Branch 0 taken 19361 times.
✓ Branch 1 taken 259 times.
19620 avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_ALP ||
601
2/2
✓ Branch 0 taken 19102 times.
✓ Branch 1 taken 259 times.
19361 avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_APM ||
602
2/2
✓ Branch 0 taken 259 times.
✓ Branch 1 taken 18843 times.
19102 avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_WS)
603 1036 pkt_size = (frame->nb_samples * channels + 1) / 2;
604 else
605 18843 pkt_size = avctx->block_align;
606
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19879 times.
19879 if ((ret = ff_get_encode_buffer(avctx, avpkt, pkt_size, 0)) < 0)
607 return ret;
608 19879 dst = avpkt->data;
609
610
10/12
✓ Branch 0 taken 783 times.
✓ Branch 1 taken 8270 times.
✓ Branch 2 taken 259 times.
✓ Branch 3 taken 259 times.
✓ Branch 4 taken 195 times.
✓ Branch 5 taken 786 times.
✓ Branch 6 taken 540 times.
✓ Branch 7 taken 259 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 8269 times.
✓ Branch 10 taken 259 times.
✗ Branch 11 not taken.
19879 switch(avctx->codec->id) {
611
19/20
✓ Branch 1 taken 1566 times.
✓ Branch 2 taken 783 times.
✓ Branch 3 taken 261 times.
✓ Branch 4 taken 522 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 261 times.
✓ Branch 9 taken 522 times.
✓ Branch 10 taken 261 times.
✓ Branch 11 taken 265176 times.
✓ Branch 12 taken 66294 times.
✓ Branch 13 taken 66294 times.
✓ Branch 14 taken 33147 times.
✓ Branch 15 taken 33147 times.
✓ Branch 16 taken 261 times.
✓ Branch 20 taken 530352 times.
✓ Branch 21 taken 132588 times.
✓ Branch 22 taken 132588 times.
✓ Branch 23 taken 66294 times.
✓ Branch 24 taken 66294 times.
✓ Branch 25 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 3 taken 8270 times.
✓ Branch 4 taken 8270 times.
✓ Branch 7 taken 529280 times.
✓ Branch 8 taken 8270 times.
✓ Branch 13 taken 264640 times.
✓ Branch 14 taken 8270 times.
✓ Branch 15 taken 16540 times.
✓ Branch 16 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 1 not taken.
✓ Branch 2 taken 259 times.
✓ Branch 7 taken 529200 times.
✓ Branch 8 taken 264600 times.
✓ Branch 9 taken 264600 times.
✓ Branch 10 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 1 not taken.
✓ Branch 2 taken 259 times.
✓ Branch 9 taken 264600 times.
✓ Branch 10 taken 132300 times.
✓ Branch 11 taken 132300 times.
✓ Branch 12 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 1 not taken.
✓ Branch 2 taken 195 times.
✓ Branch 8 taken 390 times.
✓ Branch 9 taken 195 times.
✓ Branch 10 taken 65 times.
✓ Branch 11 taken 130 times.
✓ Branch 13 taken 65 times.
✗ Branch 14 not taken.
✓ Branch 17 taken 266175 times.
✗ Branch 18 not taken.
✓ Branch 20 taken 266175 times.
✓ Branch 21 taken 65 times.
✓ Branch 24 taken 532350 times.
✗ Branch 25 not taken.
✓ Branch 28 taken 532350 times.
✓ Branch 29 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
20/24
✓ Branch 0 taken 1572 times.
✓ Branch 1 taken 786 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 1566 times.
✓ Branch 5 taken 1572 times.
✓ Branch 6 taken 786 times.
✓ Branch 7 taken 1572 times.
✓ Branch 8 taken 786 times.
✓ Branch 10 taken 1572 times.
✓ Branch 11 taken 786 times.
✓ Branch 13 taken 1572 times.
✓ Branch 14 taken 786 times.
✓ Branch 15 taken 262 times.
✓ Branch 16 taken 524 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 262 times.
✗ Branch 20 not taken.
✓ Branch 21 taken 262 times.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✓ Branch 27 taken 264620 times.
✓ Branch 28 taken 262 times.
✓ Branch 32 taken 529240 times.
✓ Branch 33 taken 524 times.
802506 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], n,
786 channels);
787 for (int i = 0; i < 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 0 taken 259 times.
✓ Branch 1 taken 281 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 259 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 259 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 12 taken 265216 times.
✓ Branch 13 taken 259 times.
✓ Branch 17 taken 287744 times.
✓ Branch 18 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 1 not taken.
✓ Branch 2 taken 259 times.
✓ Branch 9 taken 264600 times.
✓ Branch 10 taken 132300 times.
✓ Branch 11 taken 132300 times.
✓ Branch 12 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 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 1 not taken.
✓ Branch 2 taken 8269 times.
✓ Branch 6 taken 75397 times.
✓ Branch 7 taken 434344 times.
✓ Branch 8 taken 509986 times.
✓ Branch 9 taken 254748 times.
✓ Branch 10 taken 509741 times.
✓ Branch 11 taken 245 times.
✓ Branch 12 taken 255634 times.
✓ Branch 13 taken 15897 times.
✓ Branch 14 taken 254993 times.
✓ Branch 15 taken 641 times.
✓ Branch 17 taken 16538 times.
✓ Branch 18 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 1 not taken.
✓ Branch 2 taken 259 times.
✓ Branch 9 taken 264600 times.
✓ Branch 10 taken 132300 times.
✓ Branch 11 taken 132300 times.
✓ Branch 12 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 19879 *got_packet_ptr = 1;
945 19879 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