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