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