FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/adpcmenc.c
Date: 2021-09-16 08:47:15
Exec Total Coverage
Lines: 242 258 93.8%
Branches: 274 335 81.8%

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