ADPCM Codecs
Loading...
Searching...
No Matches
ADPCMDecoder.h
1#pragma once
2#include "ADPCM.h"
3#include "ADPCMCodec.h"
4#include "ADPCMEncoder.h"
5#include "adpcm/adpcm.h"
6#include "adpcm/ByteReader.h"
7#include "adpcm/BitReader.h"
8
9namespace adpcm_ffmpeg {
10
17class ADPCMDecoder : public ADPCMCodec {
18 public:
20 setBlockSize(ADAPCM_DEFAULT_BLOCK_SIZE);
21 avctx.bits_per_coded_sample = av_get_bits_per_sample();
22 // the decoder state is larger than ADPCMEncodeContext: it needs its own
23 // storage, otherwise adpcm_flush() overwrites the members that follow enc_ctx
24 avctx.priv_data = (uint8_t *)&dec_ctx;
25 }
26
33 bool begin(int sampleRate, int channels, int frameSize, int blockAlign = 0) {
34 if (frameSize <= 0) return false;
35 setFrameSize(frameSize);
36 if (blockAlign > 0) avctx.block_align = blockAlign;
37 return begin(sampleRate, channels);
38 }
39
40 bool begin(int sampleRate, int channels) {
41 avctx.sample_rate = sampleRate;
42 avctx.nb_channels = channels;
43 avctx.bits_per_coded_sample = av_get_bits_per_sample();
44
45 data_source = Undefined;
46 // determine frame size
47 int rc = adpcm_decode_init();
48 if (rc != 0) return false;
49 // set the decoder state to the codec's initial values (e.g. the CT step
50 // size or the extradata predictors); libavcodec does this during init
52
53 // if frame size has not been defined, get it from encoder
54 int frame_size = frameSize();
55 if (frame_size == 0) {
56 ADPCMEncoder *enc = ADPCMEncoderFactory::create(codecID());
57 // codecs without an encoder cannot derive a frame size
58 if (enc == nullptr) return false;
59 // preserve the block size that was already configured on this
60 // decoder (e.g. from a WAV 'fmt ' chunk's block_align) instead of
61 // letting the freshly created encoder's default block size
62 // (ADAPCM_DEFAULT_BLOCK_SIZE) silently overwrite it below
63 enc->setBlockSize(blockSize());
64 enc->begin(sampleRate, channels);
65 setFrameSize(enc->frameSize());
66 setBlockSize(enc->blockSize());
67 avctx.block_align = enc->blockAlign();
68 enc->end(); // release the buffers allocated by the encoder's init
69 delete enc;
70 frame_size = frameSize();
71 }
72
73 if (frame_size <= 0) return false;
74 // avctx.frame_size = frameSize;
75 avctx.sample_fmt = sample_formats[0];
76 // setup result frame data
77 frame_data_vector.resize(frame_size * channels);
78 frame.data[0] = (uint8_t *)&frame_data_vector[0];
79 // setup extra_data
80 frame_extended_data_vectors.resize(channels);
81 for (int ch = 0;ch < channels; ch++){
82 frame_extended_data_vectors[ch].resize(frame_size);
83 extended_data[ch] = &frame_extended_data_vectors[ch][0];
84 }
85 frame.extended_data = extended_data;
86 // set result samples
87 return true;
88 }
89
90 void end() {
91 flush();
92 // release memory
93 for (int ch = 0; ch < frame_extended_data_vectors.size(); ch++)
94 frame_extended_data_vectors[ch].resize(0);
95 frame_data_vector.resize(0);
96 }
97
98 AVFrame &decode(uint8_t *data, size_t size) {
99 packet.size = size;
100 packet.data = (uint8_t *)data;
101 return decode(packet);
102 }
103
105 int got_packet_ptr = 0;
106
107 // clear frame data result
108 // just reset the data, for the subsequent source determination
109 //std::fill(frame_data_vector.begin(), frame_data_vector.end(), 0);
110 frame_data_vector.clearContent();
111 for (int ch = 0; ch < channels(); ch++) {
112 //std::fill(frame_extended_data_vectors[ch].begin(), frame_extended_data_vectors[ch].end(), 0);
113 frame_extended_data_vectors[ch].clearContent();
114 }
115
116 // the bit readers fetch whole words, so decode from a copy of the packet
117 // that is followed by AV_INPUT_BUFFER_PADDING_SIZE zero bytes
118 const int size = packet.size;
119 packet_buffer.resize(size + AV_INPUT_BUFFER_PADDING_SIZE);
120 if (size > 0) memcpy(packet_buffer.data(), packet.data, size);
121 memset(packet_buffer.data() + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
122 AVPacket padded = packet;
123 padded.data = packet_buffer.data();
124 int rc = adpcm_decode_frame(&frame, &got_packet_ptr, &padded);
125 if (rc == 0 || !got_packet_ptr) {
126 frame.nb_samples = 0;
127 }
128
130 if (data_source == Undefined) {
131 data_source = getDataSource((int16_t *)(frame.data[0]),
132 frame.extended_data[0], frame.nb_samples);
133 }
134
135 // if data is in exended data, we copy it to the frame_data
136 if (data_source == FromExtended) {
137 int16_t *result16 = (int16_t *)frame.data[0];
138 int pos = 0;
139 for (int j = 0; j < frame.nb_samples; j++) {
140 for (int ch = 0; ch < channels(); ch++) {
141 result16[pos++] = frame.extended_data[ch][j];
142 }
143 }
144 }
145
146 return frame;
147 }
148
149 virtual ADPCMVector<AVSampleFormat> getSampleFormat() {
150 return sample_formats;
151 }
152
153 void flush() {
154 adpcm_flush();
155 }
156
157 protected:
158 enum DataSource { Undefined, FromFrame, FromExtended };
159 ADPCMDecodeContext dec_ctx{};
160 AVPacket packet;
161 AVFrame frame;
162 DataSource data_source = Undefined;
163 bool is_frame_data = true;
164 ADPCMVector<uint8_t> packet_buffer; // padded copy of the input packet
165 ADPCMVector<int16_t> frame_data_vector;
166 ADPCMVector<ADPCMVector<int16_t>> frame_extended_data_vectors;
167 int16_t *extended_data[ADPCM_MAX_CHANNELS]{};
168 uint8_t *data[AV_NUM_DATA_POINTERS]{};
169 // decoding
170 const uint8_t *buf;
171 int buf_size;
172 ADPCMDecodeContext *c;
173 int16_t *samples;
174 int16_t **samples_p;
175 int st; /* stereo */
176 int nb_samples, coded_samples, approx_nb_samples, ret;
177 ByteReader gb;
178
183 // for (int j = 0; j < len; j++) {
184 // if (data[j] != 0) return FromFrame;
185 // if (ext_data[j] != 0) return FromExtended;
186 // }
187 // return FromFrame;
188 return isPlanar() ? FromExtended : FromFrame;
189 }
190
191 // decoder
193 virtual int adpcm_decode_init() {
194 ADPCMDecodeContext *c = (ADPCMDecodeContext *)avctx.priv_data;
195 unsigned int min_channels = 1;
196 unsigned int max_channels = 2;
197
198 if (c == NULL) {
199 av_log(avctx, AV_LOG_ERROR, "priv_data is null");
200 return -1;
201 }
202
203 // adpcm_flush(avctx);
204
205 switch (avctx.codec_id) {
206 case AV_CODEC_ID_ADPCM_IMA_AMV:
207 max_channels = 1;
208 break;
209 case AV_CODEC_ID_ADPCM_DTK:
210 case AV_CODEC_ID_ADPCM_EA:
211 min_channels = 2;
212 break;
213 case AV_CODEC_ID_ADPCM_AFC:
214 case AV_CODEC_ID_ADPCM_EA_R1:
215 case AV_CODEC_ID_ADPCM_EA_R2:
216 case AV_CODEC_ID_ADPCM_EA_R3:
217 case AV_CODEC_ID_ADPCM_EA_XAS:
218 case AV_CODEC_ID_ADPCM_MS:
219 max_channels = 6;
220 break;
221 case AV_CODEC_ID_ADPCM_MTAF:
222 min_channels = 2;
223 max_channels = 8;
224 if (avctx.nb_channels & 1) {
225 avpriv_request_sample(&avctx, "channel count %d", avctx.nb_channels);
226 return AVERROR_PATCHWELCOME;
227 }
228 break;
229 case AV_CODEC_ID_ADPCM_PSX:
230 max_channels = 8;
231 if (avctx.nb_channels <= 0 ||
232 avctx.block_align % (16 * avctx.nb_channels))
233 return AVERROR_INVALIDDATA;
234 break;
235 case AV_CODEC_ID_ADPCM_IMA_DAT4:
236 case AV_CODEC_ID_ADPCM_THP:
237 case AV_CODEC_ID_ADPCM_THP_LE:
238 max_channels = 14;
239 break;
240 }
241 if (avctx.nb_channels < min_channels || avctx.nb_channels > max_channels) {
242 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
243 return AVERROR(AVERROR_INVALID);
244 }
245
246 switch (avctx.codec_id) {
247 case AV_CODEC_ID_ADPCM_IMA_WAV:
248 // ps: define default value
249 if (avctx.bits_per_coded_sample == 0) avctx.bits_per_coded_sample = 4;
250 if (avctx.bits_per_coded_sample < 2 || avctx.bits_per_coded_sample > 5)
251 return AVERROR_INVALIDDATA;
252 break;
253 case AV_CODEC_ID_ADPCM_ARGO:
254 // ps: define default value
255 if (avctx.bits_per_coded_sample == 0) avctx.bits_per_coded_sample = 4;
256 if (avctx.block_align == 0) avctx.block_align = 17 * avctx.nb_channels;
257
258 if (avctx.bits_per_coded_sample != 4 ||
259 avctx.block_align != 17 * avctx.nb_channels)
260 return AVERROR_INVALIDDATA;
261 break;
262 case AV_CODEC_ID_ADPCM_ZORK:
263 if (avctx.bits_per_coded_sample == 0) avctx.bits_per_coded_sample = 8;
264 if (avctx.bits_per_coded_sample != 8) return AVERROR_INVALIDDATA;
265 break;
266 default:
267 break;
268 }
269
270 switch (avctx.codec_id) {
271 case AV_CODEC_ID_ADPCM_AICA:
272 case AV_CODEC_ID_ADPCM_IMA_CUNNING:
273 case AV_CODEC_ID_ADPCM_IMA_DAT4:
274 case AV_CODEC_ID_ADPCM_IMA_QT:
275 case AV_CODEC_ID_ADPCM_IMA_WAV:
276 case AV_CODEC_ID_ADPCM_4XM:
277 case AV_CODEC_ID_ADPCM_XA:
278 case AV_CODEC_ID_ADPCM_XMD:
279 case AV_CODEC_ID_ADPCM_EA_R1:
280 case AV_CODEC_ID_ADPCM_EA_R2:
281 case AV_CODEC_ID_ADPCM_EA_R3:
282 case AV_CODEC_ID_ADPCM_EA_XAS:
283 case AV_CODEC_ID_ADPCM_THP:
284 case AV_CODEC_ID_ADPCM_THP_LE:
285 case AV_CODEC_ID_ADPCM_AFC:
286 case AV_CODEC_ID_ADPCM_DTK:
287 case AV_CODEC_ID_ADPCM_PSX:
288 case AV_CODEC_ID_ADPCM_MTAF:
289 case AV_CODEC_ID_ADPCM_ARGO:
290 case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
291 avctx.sample_fmt = AV_SAMPLE_FMT_S16P;
292 break;
293 case AV_CODEC_ID_ADPCM_IMA_WS:
294 avctx.sample_fmt =
295 c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P : AV_SAMPLE_FMT_S16;
296 break;
297 case AV_CODEC_ID_ADPCM_MS:
298 avctx.sample_fmt =
299 avctx.nb_channels > 2 ? AV_SAMPLE_FMT_S16P : AV_SAMPLE_FMT_S16;
300 break;
301 default:
302 avctx.sample_fmt = AV_SAMPLE_FMT_S16;
303 }
304 return 0;
305 }
306
307 int16_t adpcm_ima_expand_nibble(ADPCMChannelStatus *c, int8_t nibble,
308 int shift) {
309 int step_index;
310 int predictor;
311 int sign, delta, diff, step;
312
313 step = ff_adpcm_step_table[c->step_index];
314 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
315 step_index = av_clip(step_index, 0, 88);
316
317 sign = nibble & 8;
318 delta = nibble & 7;
319 /* perform direct multiplication instead of series of jumps proposed by
320 * the reference ADPCM implementation since modern CPUs can do the mults
321 * quickly enough */
322 diff = ((2 * delta + 1) * step) >> shift;
323 predictor = c->predictor;
324 if (sign)
325 predictor -= diff;
326 else
327 predictor += diff;
328
329 c->predictor = av_clip_int16(predictor);
330 c->step_index = step_index;
331
332 return (int16_t)c->predictor;
333 }
334
335 int16_t adpcm_ima_mtf_expand_nibble(ADPCMChannelStatus *c, int nibble) {
336 int step_index, step, delta, predictor;
337
338 step = ff_adpcm_step_table[c->step_index];
339
340 delta = step * (2 * nibble - 15);
341 predictor = c->predictor + delta;
342
343 step_index = c->step_index + mtf_index_table[(unsigned)nibble];
344 c->predictor = av_clip_int16(predictor >> 4);
345 c->step_index = av_clip(step_index, 0, 88);
346
347 return (int16_t)c->predictor;
348 }
349
350 int16_t adpcm_ima_cunning_expand_nibble(ADPCMChannelStatus *c,
351 int8_t nibble) {
352 int step_index;
353 int predictor;
354 int step;
355
356 nibble = sign_extend(nibble & 0xF, 4);
357
358 step = ima_cunning_step_table[c->step_index];
359 step_index = c->step_index + ima_cunning_index_table[abs(nibble)];
360 step_index = av_clip(step_index, 0, 60);
361
362 predictor = c->predictor + step * nibble;
363
364 c->predictor = av_clip_int16(predictor);
365 c->step_index = step_index;
366
367 return c->predictor;
368 }
369
370 int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble) {
371 int step_index;
372 int predictor;
373 int diff, step;
374
375 step = ff_adpcm_step_table[c->step_index];
376 step_index = c->step_index + ff_adpcm_index_table[nibble];
377 step_index = av_clip(step_index, 0, 88);
378
379 diff = step >> 3;
380 if (nibble & 4) diff += step;
381 if (nibble & 2) diff += step >> 1;
382 if (nibble & 1) diff += step >> 2;
383
384 if (nibble & 8)
385 predictor = c->predictor - diff;
386 else
387 predictor = c->predictor + diff;
388
389 c->predictor = av_clip_int16(predictor);
390 c->step_index = step_index;
391
392 return c->predictor;
393 }
394
395 int16_t adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble) {
396 if (!c->step) {
397 c->predictor = 0;
398 c->step = 127;
399 }
400
401 c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
402 c->predictor = av_clip_int16(c->predictor);
403 c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
404 c->step = av_clip(c->step, 127, 24576);
405 return c->predictor;
406 }
407
419 int get_nb_samples(ByteReader &gb, int buf_size, int *coded_samples,
420 int *approx_nb_samples) {
421 ADPCMDecodeContext *s = (ADPCMDecodeContext *)avctx.priv_data;
422 int nb_samples = 0;
423 int ch = avctx.nb_channels;
424 int has_coded_samples = 0;
425 int header_size;
426
427 *coded_samples = 0;
428 *approx_nb_samples = 0;
429
430 if (ch <= 0) return 0;
431
432 switch (avctx.codec_id) {
433 /* constant, only check buf_size */
434 case AV_CODEC_ID_ADPCM_EA_XAS:
435 if (buf_size < 76 * ch) return 0;
436 nb_samples = 128;
437 break;
438 case AV_CODEC_ID_ADPCM_IMA_QT:
439 if (buf_size < 34 * ch) return 0;
440 nb_samples = 64;
441 break;
442 /* simple 4-bit adpcm */
443 case AV_CODEC_ID_ADPCM_CT:
444 case AV_CODEC_ID_ADPCM_IMA_APC:
445 case AV_CODEC_ID_ADPCM_IMA_CUNNING:
446 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
447 case AV_CODEC_ID_ADPCM_IMA_OKI:
448 case AV_CODEC_ID_ADPCM_IMA_WS:
449 case AV_CODEC_ID_ADPCM_YAMAHA:
450 case AV_CODEC_ID_ADPCM_AICA:
451 case AV_CODEC_ID_ADPCM_IMA_SSI:
452 case AV_CODEC_ID_ADPCM_IMA_APM:
453 case AV_CODEC_ID_ADPCM_IMA_ALP:
454 case AV_CODEC_ID_ADPCM_IMA_MTF:
455 nb_samples = buf_size * 2 / ch;
456 break;
457 }
458 if (nb_samples) return nb_samples;
459
460 /* simple 4-bit adpcm, with header */
461 header_size = 0;
462 switch (avctx.codec_id) {
463 case AV_CODEC_ID_ADPCM_4XM:
464 case AV_CODEC_ID_ADPCM_AGM:
465 case AV_CODEC_ID_ADPCM_IMA_ACORN:
466 case AV_CODEC_ID_ADPCM_IMA_DAT4:
467 case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
468 case AV_CODEC_ID_ADPCM_IMA_ISS:
469 header_size = 4 * ch;
470 break;
471 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
472 header_size = 4 * ch;
473 break;
474 }
475 if (header_size > 0) return (buf_size - header_size) * 2 / ch;
476
477 /* more complex formats */
478 switch (avctx.codec_id) {
479 case AV_CODEC_ID_ADPCM_IMA_AMV:
480 gb.skip(4);
482 *coded_samples = gb.getLe32Unchecked();
483 nb_samples = FFMIN((buf_size - 8) * 2, *coded_samples);
484 gb.seek(-8, SEEK_CUR);
485 break;
486 case AV_CODEC_ID_ADPCM_EA:
488 *coded_samples = gb.getLe32();
489 *coded_samples -= *coded_samples % 28;
490 nb_samples = (buf_size - 12) / 30 * 28;
491 break;
492 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
494 *coded_samples = gb.getLe32();
495 nb_samples = (buf_size - (4 + 8 * ch)) * 2 / ch;
496 break;
497 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
498 nb_samples = (buf_size - ch) / ch * 2;
499 break;
500 case AV_CODEC_ID_ADPCM_EA_R1:
501 case AV_CODEC_ID_ADPCM_EA_R2:
502 case AV_CODEC_ID_ADPCM_EA_R3:
503 /* maximum number of samples */
504 /* has internal offsets and a per-frame switch to signal raw 16-bit */
506 switch (avctx.codec_id) {
507 case AV_CODEC_ID_ADPCM_EA_R1:
508 header_size = 4 + 9 * ch;
509 *coded_samples = gb.getLe32();
510 break;
511 case AV_CODEC_ID_ADPCM_EA_R2:
512 header_size = 4 + 5 * ch;
513 *coded_samples = gb.getLe32();
514 break;
515 case AV_CODEC_ID_ADPCM_EA_R3:
516 header_size = 4 + 5 * ch;
517 *coded_samples = gb.getBe32();
518 break;
519 }
520 *coded_samples -= *coded_samples % 28;
521 nb_samples = (buf_size - header_size) * 2 / ch;
522 nb_samples -= nb_samples % 28;
523 *approx_nb_samples = 1;
524 break;
525 case AV_CODEC_ID_ADPCM_IMA_DK3:
526 if (avctx.block_align > 0)
527 buf_size = FFMIN(buf_size, avctx.block_align);
528 nb_samples = ((buf_size - 16) * 2 / 3 * 4) / ch;
529 break;
530 case AV_CODEC_ID_ADPCM_IMA_DK4:
531 if (avctx.block_align > 0)
532 buf_size = FFMIN(buf_size, avctx.block_align);
533 if (buf_size < 4 * ch) return AVERROR_INVALIDDATA;
534 nb_samples = 1 + (buf_size - 4 * ch) * 2 / ch;
535 break;
536 case AV_CODEC_ID_ADPCM_IMA_RAD:
537 if (avctx.block_align > 0)
538 buf_size = FFMIN(buf_size, avctx.block_align);
539 nb_samples = (buf_size - 4 * ch) * 2 / ch;
540 break;
541 case AV_CODEC_ID_ADPCM_IMA_WAV: {
542 int bsize = ff_adpcm_ima_block_sizes[avctx.bits_per_coded_sample - 2];
543 int bsamples =
544 ff_adpcm_ima_block_samples[avctx.bits_per_coded_sample - 2];
545 if (avctx.block_align > 0)
546 buf_size = FFMIN(buf_size, avctx.block_align);
547 if (buf_size < 4 * ch) return AVERROR_INVALIDDATA;
548 nb_samples = 1 + (buf_size - 4 * ch) / (bsize * ch) * bsamples;
549 } break; /* End of CASE */
550 case AV_CODEC_ID_ADPCM_MS:
551 if (avctx.block_align > 0)
552 buf_size = FFMIN(buf_size, avctx.block_align);
553 nb_samples = (buf_size - 6 * ch) * 2 / ch;
554 break;
555 case AV_CODEC_ID_ADPCM_MTAF:
556 if (avctx.block_align > 0)
557 buf_size = FFMIN(buf_size, avctx.block_align);
558 nb_samples = (buf_size - 16 * (ch / 2)) * 2 / ch;
559 break;
560 case AV_CODEC_ID_ADPCM_SBPRO_2:
561 case AV_CODEC_ID_ADPCM_SBPRO_3:
562 case AV_CODEC_ID_ADPCM_SBPRO_4: {
564 switch (avctx.codec_id) {
565 case AV_CODEC_ID_ADPCM_SBPRO_2:
567 break;
568 case AV_CODEC_ID_ADPCM_SBPRO_3:
570 break;
571 case AV_CODEC_ID_ADPCM_SBPRO_4:
573 break;
574 }
575 if (!s->status[0].step_index) {
576 if (buf_size < ch) return AVERROR_INVALIDDATA;
577 nb_samples++;
578 buf_size -= ch;
579 }
580 nb_samples += buf_size * samples_per_byte / ch;
581 break;
582 }
583 case AV_CODEC_ID_ADPCM_SWF: {
584 int buf_bits = buf_size * 8 - 2;
585 // SWF bit fields are most significant bit first, so the 2-bit format
586 // field is the high bits of the first byte
587 int nbits = ((buf[0] >> 6) & 3) + 2;
588 int block_hdr_size = 22 * ch;
589 int block_size = block_hdr_size + nbits * ch * 4095;
590 int nblocks = buf_bits / block_size;
591 int bits_left = buf_bits - nblocks * block_size;
592 nb_samples = nblocks * 4096;
594 nb_samples += 1 + (bits_left - block_hdr_size) / (nbits * ch);
595 break;
596 }
597 case AV_CODEC_ID_ADPCM_THP:
598 case AV_CODEC_ID_ADPCM_THP_LE:
599 if (avctx.extradata) {
600 nb_samples = buf_size * 14 / (8 * ch);
601 break;
602 }
604 gb.skip(4); // channel size
605 *coded_samples = (avctx.codec_id == AV_CODEC_ID_ADPCM_THP_LE)
606 ? gb.getLe32()
607 : gb.getBe32();
608 buf_size -= 8 + 36 * ch;
609 buf_size /= ch;
610 nb_samples = buf_size / 8 * 14;
611 if (buf_size % 8 > 1) nb_samples += (buf_size % 8 - 1) * 2;
612 *approx_nb_samples = 1;
613 break;
614 case AV_CODEC_ID_ADPCM_AFC:
615 nb_samples = buf_size / (9 * ch) * 16;
616 break;
617 case AV_CODEC_ID_ADPCM_XA:
618 nb_samples = (buf_size / 128) * 224 / ch;
619 break;
620 case AV_CODEC_ID_ADPCM_XMD:
621 nb_samples = buf_size / (21 * ch) * 32;
622 break;
623 case AV_CODEC_ID_ADPCM_DTK:
624 case AV_CODEC_ID_ADPCM_PSX:
625 nb_samples = buf_size / (16 * ch) * 28;
626 break;
627 case AV_CODEC_ID_ADPCM_ARGO:
628 nb_samples = buf_size / avctx.block_align * 32;
629 break;
630 case AV_CODEC_ID_ADPCM_ZORK:
631 nb_samples = buf_size / ch;
632 break;
633 }
634
635 /* validate coded sample count */
636 if (has_coded_samples &&
638 return AVERROR_INVALIDDATA;
639
640 return nb_samples;
641 }
642
644 virtual void adpcm_flush() {
645 ADPCMDecodeContext *c = (ADPCMDecodeContext *)avctx.priv_data;
646
647 /* Just nuke the entire state and re-init. */
648 memset(c, 0, sizeof(ADPCMDecodeContext));
649
650 switch (avctx.codec_id) {
651 case AV_CODEC_ID_ADPCM_CT:
652 c->status[0].step = c->status[1].step = 511;
653 break;
654
655 case AV_CODEC_ID_ADPCM_IMA_APC:
656 if (avctx.extradata && avctx.extradata_size >= 8) {
657 c->status[0].predictor = av_clip_intp2(loadLittleEndian<4>(avctx.extradata), 18);
658 c->status[1].predictor =
659 av_clip_intp2(loadLittleEndian<4>(avctx.extradata + 4), 18);
660 }
661 break;
662
663 case AV_CODEC_ID_ADPCM_IMA_APM:
664 if (avctx.extradata && avctx.extradata_size >= 28) {
665 c->status[0].predictor =
666 av_clip_intp2(loadLittleEndian<4>(avctx.extradata + 16), 18);
667 c->status[0].step_index =
668 av_clip(loadLittleEndian<4>(avctx.extradata + 20), 0, 88);
669 c->status[1].predictor =
670 av_clip_intp2(loadLittleEndian<4>(avctx.extradata + 4), 18);
671 c->status[1].step_index =
672 av_clip(loadLittleEndian<4>(avctx.extradata + 8), 0, 88);
673 }
674 break;
675
676 case AV_CODEC_ID_ADPCM_IMA_WS:
677 if (avctx.extradata && avctx.extradata_size >= 2)
678 c->vqa_version = loadLittleEndian<2>(avctx.extradata);
679 break;
680 default:
681 /* Other codecs may want to handle this during decoding. */
682 c->has_status = 0;
683 return;
684 }
685
686 c->has_status = 1;
687 }
688
689 virtual int decode_frame_impl(AVFrame *frame, int *got_frame_ptr,
690 AVPacket *avpkt) = 0;
691
693 virtual int adpcm_decode_frame(AVFrame *frame, int *got_frame_ptr,
694 AVPacket *avpkt) {
695 // the init result must be honored: it rejects packets whose sample count
696 // cannot be stored in the output buffer
697 int init_rc = decode_frame_init(frame, got_frame_ptr, avpkt);
698 if (init_rc != AV_OK) return init_rc;
699
700 int rc = decode_frame_impl(frame, got_frame_ptr, avpkt);
701 if (rc != AV_OK) return rc;
702
703 if (avpkt->size && gb.tell() == 0) {
704 av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n");
705 return AVERROR_INVALIDDATA;
706 }
707
708 *got_frame_ptr = 1;
709
710 if (avpkt->size < gb.tell()) {
711 av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size,
712 gb.tell());
713 return avpkt->size;
714 }
715
716 return gb.tell();
717 }
718
719 int decode_frame_init(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
720 buf = avpkt->data;
721 buf_size = avpkt->size;
722 c = (ADPCMDecodeContext *)avctx.priv_data;
723
724 gb.init(buf, buf_size);
725 nb_samples =
726 get_nb_samples(gb, buf_size, &coded_samples, &approx_nb_samples);
727 if (nb_samples <= 0) {
728 av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n");
729 return AVERROR_INVALIDDATA;
730 }
731
732 /* get output buffer */
733 frame->nb_samples = nb_samples;
734 if ((ret = ff_get_buffer(&avctx, frame, 0)) < 0) return ret;
735 samples = (int16_t *)frame->data[0];
736 samples_p = (int16_t **)frame->extended_data;
737
738 /* use coded_samples when applicable */
739 /* it must not exceed what the packet can hold: the sample loops read
740 nb_samples worth of data and would run past the end of the packet */
741 if (coded_samples) {
742 if (coded_samples > nb_samples) {
743 av_log(avctx, AV_LOG_ERROR, "coded sample count %d exceeds packet\n",
744 coded_samples);
745 return AVERROR_INVALIDDATA;
746 }
747 if (!approx_nb_samples && coded_samples != nb_samples)
748 av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n");
749 frame->nb_samples = nb_samples = coded_samples;
750 }
751
752 // the output buffer holds exactly frameSize() samples per channel; a
753 // packet that claims more would make the decoder write past its end
754 if (nb_samples > frameSize()) {
755 av_log(avctx, AV_LOG_ERROR,
756 "packet sample count %d exceeds frame size %d\n", nb_samples,
757 frameSize());
758 return AVERROR_INVALIDDATA;
759 }
760
761 st = channels() == 2 ? 1 : 0;
762 return AV_OK;
763 }
764};
765
767 public:
769 setCodecID(AV_CODEC_ID_ADPCM_IMA_QT);
770 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
771 }
772 protected:
773 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
774 /* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
775 Channel data is interleaved per-chunk. */
776 for (int channel = 0; channel < channels(); channel++) {
777 ADPCMChannelStatus *cs = &c->status[channel];
778 int predictor;
779 int step_index;
780 /* (pppppp) (piiiiiii) */
781
782 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
783 predictor = sign_extend(gb.getBe16Unchecked(), 16);
784 step_index = predictor & 0x7F;
785 predictor &= ~0x7F;
786
787 if (cs->step_index == step_index) {
788 int diff = predictor - cs->predictor;
789 if (diff < 0) diff = -diff;
790 if (diff > 0x7f) goto update;
791 } else {
792 update:
793 cs->step_index = step_index;
794 cs->predictor = predictor;
795 }
796
797 if (cs->step_index > 88u) {
798 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n", channel,
799 cs->step_index);
800 return AVERROR_INVALIDDATA;
801 }
802
803 samples = samples_p[channel];
804
805 for (int m = 0; m < 64; m += 2) {
806 int byte = gb.getByteUnchecked();
807 samples[m] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F);
808 samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4);
809 }
810 }
811 return AV_OK;
812 }
813};
814
816 public:
818 setCodecID(AV_CODEC_ID_ADPCM_IMA_WAV);
819 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
820 }
821 int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, BitReader &gb,
822 int bps) {
823 int nibble, step_index, predictor, sign, delta, diff, step, shift;
824
825 shift = bps - 1;
826 nibble = gb.read(bps), step = ff_adpcm_step_table[c->step_index];
827 step_index = c->step_index + adpcm_index_tables[bps - 2][nibble];
828 step_index = av_clip(step_index, 0, 88);
829
830 sign = nibble & (1 << shift);
831 delta = av_mod_uintp2(nibble, shift);
832 diff = ((2 * delta + 1) * step) >> shift;
833 predictor = c->predictor;
834 if (sign)
835 predictor -= diff;
836 else
837 predictor += diff;
838
839 c->predictor = av_clip_int16(predictor);
840 c->step_index = step_index;
841
842 return (int16_t)c->predictor;
843 }
844
845 protected:
846 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
847 for (int i = 0; i < channels(); i++) {
848 ADPCMChannelStatus *cs = &c->status[i];
849 cs->predictor = samples_p[i][0] =
850 sign_extend(gb.getLe16Unchecked(), 16);
851
852 cs->step_index = sign_extend(gb.getLe16Unchecked(), 16);
853 if (cs->step_index > 88u) {
854 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n", i,
855 cs->step_index);
856 return AVERROR_INVALIDDATA;
857 }
858 }
859
860 if (avctx.bits_per_coded_sample != 4) {
862 ff_adpcm_ima_block_samples[avctx.bits_per_coded_sample - 2];
863 int block_size =
864 ff_adpcm_ima_block_sizes[avctx.bits_per_coded_sample - 2];
865 uint8_t temp[20 + AV_INPUT_BUFFER_PADDING_SIZE] = {0};
866 BitReader g;
867
868 for (int n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
869 for (int i = 0; i < channels(); i++) {
870 ADPCMChannelStatus *cs = &c->status[i];
871 samples = &samples_p[i][1 + n * samples_per_block];
872 for (int j = 0; j < block_size; j++) {
873 temp[j] = buf[4 * channels() + block_size * n * channels() + (j % 4) +
874 (j / 4) * (channels() * 4) + i * 4];
875 }
876 ret = g.initBytes((const uint8_t *)&temp, block_size);
877 if (ret < 0) return ret;
878 for (int m = 0; m < samples_per_block; m++) {
879 samples[m] = adpcm_ima_wav_expand_nibble(
880 cs, g, avctx.bits_per_coded_sample);
881 }
882 }
883 }
884 gb.skip(avctx.block_align - channels() * 4);
885 } else {
886 for (int n = 0; n < (nb_samples - 1) / 8; n++) {
887 for (int i = 0; i < channels(); i++) {
888 ADPCMChannelStatus *cs = &c->status[i];
889 samples = &samples_p[i][1 + n * 8];
890 for (int m = 0; m < 8; m += 2) {
891 int v = gb.getByteUnchecked();
892 samples[m] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
893 samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4, 3);
894 }
895 }
896 }
897 }
898 return AV_OK;
899 }
900};
901
903 public:
905 setCodecID(AV_CODEC_ID_ADPCM_4XM);
906 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
907 }
908 protected:
909 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
910 for (int i = 0; i < channels(); i++)
911 c->status[i].predictor = sign_extend(gb.getLe16Unchecked(), 16);
912
913 for (int i = 0; i < channels(); i++) {
914 c->status[i].step_index = sign_extend(gb.getLe16Unchecked(), 16);
915 if (c->status[i].step_index > 88u) {
916 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n", i,
917 c->status[i].step_index);
918 return AVERROR_INVALIDDATA;
919 }
920 }
921
922 for (int i = 0; i < channels(); i++) {
923 ADPCMChannelStatus *cs = &c->status[i];
924 samples = samples_p[i];
925 for (int n = nb_samples >> 1; n > 0; n--) {
926 int v = gb.getByteUnchecked();
927 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4);
928 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4, 4);
929 }
930 }
931 return AV_OK;
932 }
933};
934
936 public:
938 setCodecID(AV_CODEC_ID_ADPCM_AGM);
939 sample_formats.push_back(AV_SAMPLE_FMT_S16);
940 }
941 int16_t adpcm_agm_expand_nibble(ADPCMChannelStatus *c, int8_t nibble) {
942 int delta, pred, step, add;
943
944 pred = c->predictor;
945 delta = nibble & 7;
946 step = c->step;
947 add = (delta * 2 + 1) * step;
948 if (add < 0) add = add + 7;
949
950 if ((nibble & 8) == 0)
951 pred = av_clip(pred + (add >> 3), -32767, 32767);
952 else
953 pred = av_clip(pred - (add >> 3), -32767, 32767);
954
955 switch (delta) {
956 case 7:
957 step *= 0x99;
958 break;
959 case 6:
960 c->step = av_clip(c->step * 2, 127, 24576);
961 c->predictor = pred;
962 return pred;
963 case 5:
964 step *= 0x66;
965 break;
966 case 4:
967 step *= 0x4d;
968 break;
969 default:
970 step *= 0x39;
971 break;
972 }
973
974 if (step < 0) step += 0x3f;
975
976 c->step = step >> 6;
977 c->step = av_clip(c->step, 127, 24576);
978 c->predictor = pred;
979 return pred;
980 }
981
982 protected:
983 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
984 for (int i = 0; i < channels(); i++)
985 c->status[i].predictor = sign_extend(gb.getLe16Unchecked(), 16);
986 for (int i = 0; i < channels(); i++)
987 c->status[i].step = sign_extend(gb.getLe16Unchecked(), 16);
988
989 for (int n = 0; n < nb_samples >> (1 - st); n++) {
990 int v = gb.getByteUnchecked();
991 *samples++ = adpcm_agm_expand_nibble(&c->status[0], v & 0xF);
992 *samples++ = adpcm_agm_expand_nibble(&c->status[st], v >> 4);
993 }
994 return AV_OK;
995 }
996};
997
999 public:
1000 DecoderADPCM_MS() {
1001 setCodecID(AV_CODEC_ID_ADPCM_MS);
1002 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1003 //sample_formats.push_back(AV_SAMPLE_FMT_S16P);
1004 }
1005 int16_t adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble) {
1006 int predictor;
1007
1008 predictor =
1009 (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
1010 predictor += ((nibble & 0x08) ? (nibble - 0x10) : (nibble)) * c->idelta;
1011
1012 c->sample2 = c->sample1;
1013 c->sample1 = av_clip_int16(predictor);
1014 c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
1015 if (c->idelta < 16) c->idelta = 16;
1016 if (c->idelta > INT_MAX / 768) {
1017 av_log(NULL, AV_LOG_WARNING, "idelta overflow\n");
1018 c->idelta = INT_MAX / 768;
1019 }
1020
1021 return c->sample1;
1022 }
1023
1024 protected:
1025 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
1026 int block_predictor;
1027
1028 if (avctx.nb_channels > 2) {
1029 for (int channel = 0; channel < avctx.nb_channels; channel++) {
1030 samples = samples_p[channel];
1031 block_predictor = gb.getByteUnchecked();
1032 if (block_predictor > 6) {
1033 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[%d] = %d\n",
1035 return AVERROR_INVALIDDATA;
1036 }
1037 c->status[channel].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1038 c->status[channel].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1039 c->status[channel].idelta = sign_extend(gb.getLe16Unchecked(), 16);
1040 c->status[channel].sample1 =
1041 sign_extend(gb.getLe16Unchecked(), 16);
1042 c->status[channel].sample2 =
1043 sign_extend(gb.getLe16Unchecked(), 16);
1044 *samples++ = c->status[channel].sample2;
1045 *samples++ = c->status[channel].sample1;
1046 for (int n = (nb_samples - 2) >> 1; n > 0; n--) {
1047 int byte = gb.getByteUnchecked();
1048 *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte >> 4);
1049 *samples++ = adpcm_ms_expand_nibble(&c->status[channel], byte & 0x0F);
1050 }
1051 }
1052 } else {
1053 block_predictor = gb.getByteUnchecked();
1054 if (block_predictor > 6) {
1055 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n",
1057 return AVERROR_INVALIDDATA;
1058 }
1059 c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1060 c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1061 if (st) {
1062 block_predictor = gb.getByteUnchecked();
1063 if (block_predictor > 6) {
1064 av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n",
1066 return AVERROR_INVALIDDATA;
1067 }
1068 c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor];
1069 c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor];
1070 }
1071 c->status[0].idelta = sign_extend(gb.getLe16Unchecked(), 16);
1072 if (st) {
1073 c->status[1].idelta = sign_extend(gb.getLe16Unchecked(), 16);
1074 }
1075
1076 c->status[0].sample1 = sign_extend(gb.getLe16Unchecked(), 16);
1077 if (st)
1078 c->status[1].sample1 = sign_extend(gb.getLe16Unchecked(), 16);
1079 c->status[0].sample2 = sign_extend(gb.getLe16Unchecked(), 16);
1080 if (st)
1081 c->status[1].sample2 = sign_extend(gb.getLe16Unchecked(), 16);
1082
1083 *samples++ = c->status[0].sample2;
1084 if (st) *samples++ = c->status[1].sample2;
1085 *samples++ = c->status[0].sample1;
1086 if (st) *samples++ = c->status[1].sample1;
1087 for (int n = (nb_samples - 2) >> (1 - st); n > 0; n--) {
1088 int byte = gb.getByteUnchecked();
1089 *samples++ = adpcm_ms_expand_nibble(&c->status[0], byte >> 4);
1090 *samples++ = adpcm_ms_expand_nibble(&c->status[st], byte & 0x0F);
1091 }
1092 }
1093 return AV_OK;
1094 }
1095};
1096
1098 public:
1100 setCodecID(AV_CODEC_ID_ADPCM_MTAF);
1101 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
1102 }
1103 int16_t adpcm_mtaf_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble) {
1104 c->predictor += mtaf_stepsize[c->step][nibble];
1105 c->predictor = av_clip_int16(c->predictor);
1106 c->step += ff_adpcm_index_table[nibble];
1107 c->step = av_clip_uintp2(c->step, 5);
1108 return c->predictor;
1109 }
1110
1111 protected:
1112 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
1113 for (int channel = 0; channel < channels(); channel += 2) {
1114 gb.skipUnchecked(4);
1115 c->status[channel].step = gb.getLe16Unchecked() & 0x1f;
1116 c->status[channel + 1].step = gb.getLe16Unchecked() & 0x1f;
1117 c->status[channel].predictor =
1118 sign_extend(gb.getLe16Unchecked(), 16);
1119 gb.skipUnchecked(2);
1120 c->status[channel + 1].predictor =
1121 sign_extend(gb.getLe16Unchecked(), 16);
1122 gb.skipUnchecked(2);
1123 for (int n = 0; n < nb_samples; n += 2) {
1124 int v = gb.getByteUnchecked();
1125 samples_p[channel][n] =
1126 adpcm_mtaf_expand_nibble(&c->status[channel], v & 0x0F);
1127 samples_p[channel][n + 1] =
1128 adpcm_mtaf_expand_nibble(&c->status[channel], v >> 4);
1129 }
1130 for (int n = 0; n < nb_samples; n += 2) {
1131 int v = gb.getByteUnchecked();
1132 samples_p[channel + 1][n] =
1133 adpcm_mtaf_expand_nibble(&c->status[channel + 1], v & 0x0F);
1134 samples_p[channel + 1][n + 1] =
1135 adpcm_mtaf_expand_nibble(&c->status[channel + 1], v >> 4);
1136 }
1137 }
1138 return AV_OK;
1139 }
1140};
1141
1143 public:
1145 setCodecID(AV_CODEC_ID_ADPCM_IMA_DK4);
1146 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1147 }
1148 protected:
1149 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
1150 for (int channel = 0; channel < channels(); channel++) {
1151 ADPCMChannelStatus *cs = &c->status[channel];
1152 cs->predictor = *samples++ = sign_extend(gb.getLe16Unchecked(), 16);
1153 cs->step_index = sign_extend(gb.getLe16Unchecked(), 16);
1154 if (cs->step_index > 88u) {
1155 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n", channel,
1156 cs->step_index);
1157 return AVERROR_INVALIDDATA;
1158 }
1159 }
1160 for (int n = (nb_samples - 1) >> (1 - st); n > 0; n--) {
1161 int v = gb.getByteUnchecked();
1162 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1163 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1164 }
1165 /* DK3 ADPCM support macro */
1166 return AV_OK;
1167 }
1168};
1169
1171 public:
1173 setCodecID(AV_CODEC_ID_ADPCM_IMA_DK3);
1174 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1175 }
1176
1177 inline void dk3_get_next_nibble(int &decode_top_nibble_next, int &nibble,
1178 int &last_byte) {
1180 nibble = last_byte >> 4;
1182 } else {
1183 last_byte = gb.getByteUnchecked();
1184 nibble = last_byte & 0x0F;
1186 }
1187 }
1188
1189 protected:
1190 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
1191 int last_byte = 0;
1192 int nibble;
1193 int decode_top_nibble_next = 0;
1194 int diff_channel;
1195 const int16_t *samples_end = samples + channels() * nb_samples;
1196
1197 gb.skipUnchecked(10);
1198 c->status[0].predictor = sign_extend(gb.getLe16Unchecked(), 16);
1199 c->status[1].predictor = sign_extend(gb.getLe16Unchecked(), 16);
1200 c->status[0].step_index = gb.getByteUnchecked();
1201 c->status[1].step_index = gb.getByteUnchecked();
1202 if (c->status[0].step_index > 88u || c->status[1].step_index > 88u) {
1203 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n",
1204 c->status[0].step_index, c->status[1].step_index);
1205 return AVERROR_INVALIDDATA;
1206 }
1207 /* sign extend the predictors */
1208 diff_channel = c->status[1].predictor;
1209
1210 while (samples < samples_end) {
1211 /* for this algorithm, c->status[0] is the sum channel and
1212 * c->status[1] is the diff channel */
1213
1214 /* process the first predictor of the sum channel */
1215 dk3_get_next_nibble(decode_top_nibble_next, nibble, last_byte);
1216 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1217
1218 /* process the diff channel predictor */
1219 dk3_get_next_nibble(decode_top_nibble_next, nibble, last_byte);
1220 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
1221
1222 /* process the first pair of stereo PCM samples */
1223 diff_channel = (diff_channel + c->status[1].predictor) / 2;
1224 *samples++ = c->status[0].predictor + c->status[1].predictor;
1225 *samples++ = c->status[0].predictor - c->status[1].predictor;
1226
1227 /* process the second predictor of the sum channel */
1228 dk3_get_next_nibble(decode_top_nibble_next, nibble, last_byte);
1229 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
1230
1231 /* process the second pair of stereo PCM samples */
1232 diff_channel = (diff_channel + c->status[1].predictor) / 2;
1233 *samples++ = c->status[0].predictor + c->status[1].predictor;
1234 *samples++ = c->status[0].predictor - c->status[1].predictor;
1235 }
1236
1237 if ((gb.tell() & 1)) gb.skip(1);
1238 return AV_OK;
1239 }
1240};
1241
1243 public:
1245 setCodecID(AV_CODEC_ID_ADPCM_IMA_ISS);
1246 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1247 }
1248 protected:
1249 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
1250 for (int channel = 0; channel < channels(); channel++) {
1251 ADPCMChannelStatus *cs = &c->status[channel];
1252 cs->predictor = sign_extend(gb.getLe16Unchecked(), 16);
1253 cs->step_index = sign_extend(gb.getLe16Unchecked(), 16);
1254 if (cs->step_index > 88u) {
1255 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n", channel,
1256 cs->step_index);
1257 return AVERROR_INVALIDDATA;
1258 }
1259 }
1260
1261 for (int n = nb_samples >> (1 - st); n > 0; n--) {
1262 int v1, v2;
1263 int v = gb.getByteUnchecked();
1264 /* nibbles are swapped for mono */
1265 if (st) {
1266 v1 = v >> 4;
1267 v2 = v & 0x0F;
1268 } else {
1269 v2 = v >> 4;
1270 v1 = v & 0x0F;
1271 }
1272 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v1, 3);
1273 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v2, 3);
1274 }
1275 return AV_OK;
1276 }
1277};
1278
1280 public:
1282 setCodecID(AV_CODEC_ID_ADPCM_IMA_MOFLEX);
1283 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
1284 }
1285 protected:
1286 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
1287 for (int channel = 0; channel < channels(); channel++) {
1288 ADPCMChannelStatus *cs = &c->status[channel];
1289 cs->step_index = sign_extend(gb.getLe16Unchecked(), 16);
1290 cs->predictor = sign_extend(gb.getLe16Unchecked(), 16);
1291 if (cs->step_index > 88u) {
1292 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n", channel,
1293 cs->step_index);
1294 return AVERROR_INVALIDDATA;
1295 }
1296 }
1297
1298 for (int subframe = 0; subframe < nb_samples / 256; subframe++) {
1299 for (int channel = 0; channel < channels(); channel++) {
1300 samples = samples_p[channel] + 256 * subframe;
1301 for (int n = 0; n < 256; n += 2) {
1302 int v = gb.getByteUnchecked();
1303 *samples++ =
1304 adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1305 *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4, 3);
1306 }
1307 }
1308 }
1309 return AV_OK;
1310 }
1311};
1312
1314 public:
1316 setCodecID(AV_CODEC_ID_ADPCM_IMA_DAT4);
1317 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
1318 }
1319 protected:
1320 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
1321 for (int channel = 0; channel < channels(); channel++) {
1322 ADPCMChannelStatus *cs = &c->status[channel];
1323 samples = samples_p[channel];
1324 gb.skip(4);
1325 for (int n = 0; n < nb_samples; n += 2) {
1326 int v = gb.getByteUnchecked();
1327 *samples++ = adpcm_ima_expand_nibble(cs, v >> 4, 3);
1328 *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 3);
1329 }
1330 }
1331 return AV_OK;
1332 }
1333};
1334
1336 public:
1338 setCodecID(AV_CODEC_ID_ADPCM_IMA_APC);
1339 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1340 }
1341 protected:
1342 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
1343 for (int n = nb_samples >> (1 - st); n > 0; n--) {
1344 int v = gb.getByteUnchecked();
1345 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1346 *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3);
1347 }
1348 return AV_OK;
1349 }
1350};
1351
1353 public:
1355 setCodecID(AV_CODEC_ID_ADPCM_IMA_SSI);
1356 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1357 }
1358 protected:
1359 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
1360 for (int n = nb_samples >> (1 - st); n > 0; n--) {
1361 int v = gb.getByteUnchecked();
1362 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0], v >> 4);
1363 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0x0F);
1364 }
1365 return AV_OK;
1366 }
1367};
1368
1370 public:
1372 setCodecID(AV_CODEC_ID_ADPCM_IMA_APM);
1373 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1374 }
1375 protected:
1376 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
1377 for (int n = nb_samples / 2; n > 0; n--) {
1378 for (int channel = 0; channel < channels(); channel++) {
1379 int v = gb.getByteUnchecked();
1380 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[channel], v >> 4);
1381 samples[st] = adpcm_ima_qt_expand_nibble(&c->status[channel], v & 0x0F);
1382 }
1383 samples += channels();
1384 }
1385 return AV_OK;
1386 }
1387};
1388
1390 public:
1392 setCodecID(AV_CODEC_ID_ADPCM_IMA_ALP);
1393 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1394 }
1395 int16_t adpcm_ima_alp_expand_nibble(ADPCMChannelStatus *c, int8_t nibble,
1396 int shift) {
1397 int step_index;
1398 int predictor;
1399 int sign, delta, diff, step;
1400
1401 step = ff_adpcm_step_table[c->step_index];
1402 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
1403 step_index = av_clip(step_index, 0, 88);
1404
1405 sign = nibble & 8;
1406 delta = nibble & 7;
1407 diff = (delta * step) >> shift;
1408 predictor = c->predictor;
1409 if (sign)
1410 predictor -= diff;
1411 else
1412 predictor += diff;
1413
1414 c->predictor = av_clip_int16(predictor);
1415 c->step_index = step_index;
1416
1417 return (int16_t)c->predictor;
1418 }
1419
1420 protected:
1421 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
1422 for (int n = nb_samples / 2; n > 0; n--) {
1423 for (int channel = 0; channel < channels(); channel++) {
1424 int v = gb.getByteUnchecked();
1425 *samples++ =
1426 adpcm_ima_alp_expand_nibble(&c->status[channel], v >> 4, 2);
1427 samples[st] =
1428 adpcm_ima_alp_expand_nibble(&c->status[channel], v & 0x0F, 2);
1429 }
1430 samples += channels();
1431 }
1432 return AV_OK;
1433 }
1434};
1435
1437 public:
1439 setCodecID(AV_CODEC_ID_ADPCM_IMA_CUNNING);
1440 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
1441 }
1442 protected:
1443 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
1444 for (int channel = 0; channel < channels(); channel++) {
1445 int16_t *smp = samples_p[channel];
1446 for (int n = 0; n < nb_samples / 2; n++) {
1447 int v = gb.getByteUnchecked();
1448 *smp++ = adpcm_ima_cunning_expand_nibble(&c->status[channel], v & 0x0F);
1449 *smp++ = adpcm_ima_cunning_expand_nibble(&c->status[channel], v >> 4);
1450 }
1451 }
1452 return AV_OK;
1453 }
1454};
1455
1457 public:
1459 setCodecID(AV_CODEC_ID_ADPCM_IMA_OKI);
1460 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1461 }
1462 int16_t adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble) {
1463 int step_index, predictor, sign, delta, diff, step;
1464
1465 step = oki_step_table[c->step_index];
1466 step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
1467 step_index = av_clip(step_index, 0, 48);
1468
1469 sign = nibble & 8;
1470 delta = nibble & 7;
1471 diff = ((2 * delta + 1) * step) >> 3;
1472 predictor = c->predictor;
1473 if (sign)
1474 predictor -= diff;
1475 else
1476 predictor += diff;
1477
1478 c->predictor = av_clip_intp2(predictor, 11);
1479 c->step_index = step_index;
1480
1481 return c->predictor * 16;
1482 }
1483
1484 protected:
1485 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
1486 for (int n = nb_samples >> (1 - st); n > 0; n--) {
1487 int v = gb.getByteUnchecked();
1488 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4);
1489 *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F);
1490 }
1491 return AV_OK;
1492 }
1493};
1494
1496 public:
1498 setCodecID(AV_CODEC_ID_ADPCM_IMA_RAD);
1499 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1500 }
1501 protected:
1502 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
1503 for (int channel = 0; channel < channels(); channel++) {
1504 ADPCMChannelStatus *cs = &c->status[channel];
1505 cs->step_index = sign_extend(gb.getLe16Unchecked(), 16);
1506 cs->predictor = sign_extend(gb.getLe16Unchecked(), 16);
1507 if (cs->step_index > 88u) {
1508 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n", channel,
1509 cs->step_index);
1510 return AVERROR_INVALIDDATA;
1511 }
1512 }
1513 for (int n = 0; n < nb_samples / 2; n++) {
1514 int byte[2];
1515
1516 byte[0] = gb.getByteUnchecked();
1517 if (st) byte[1] = gb.getByteUnchecked();
1518 for (int channel = 0; channel < channels(); channel++) {
1519 *samples++ = adpcm_ima_expand_nibble(&c->status[channel],
1520 byte[channel] & 0x0F, 3);
1521 }
1522 for (int channel = 0; channel < channels(); channel++) {
1523 *samples++ =
1524 adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4, 3);
1525 }
1526 }
1527 return AV_OK;
1528 }
1529};
1530
1532 public:
1534 setCodecID(AV_CODEC_ID_ADPCM_IMA_WS);
1535 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1536 //sample_formats.push_back(AV_SAMPLE_FMT_S16P);
1537 }
1538 protected:
1539 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
1540 if (c->vqa_version == 3) {
1541 for (int channel = 0; channel < channels(); channel++) {
1542 int16_t *smp = samples_p[channel];
1543
1544 for (int n = nb_samples / 2; n > 0; n--) {
1545 int v = gb.getByteUnchecked();
1546 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1547 *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4, 3);
1548 }
1549 }
1550 } else {
1551 for (int n = nb_samples / 2; n > 0; n--) {
1552 for (int channel = 0; channel < channels(); channel++) {
1553 int v = gb.getByteUnchecked();
1554 *samples++ =
1555 adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
1556 samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v >> 4, 3);
1557 }
1558 samples += channels();
1559 }
1560 }
1561 gb.seek(0, SEEK_END);
1562 return AV_OK;
1563 }
1564};
1565
1567 public:
1569 setCodecID(AV_CODEC_ID_ADPCM_XMD);
1570 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
1571 }
1572 protected:
1573 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
1574 int bytes_remaining, block = 0;
1575 while (gb.bytesLeft() >= 21 * channels()) {
1576 for (int channel = 0; channel < channels(); channel++) {
1577 int16_t *out = samples_p[channel] + block * 32;
1578 int16_t history[2];
1580
1581 history[1] = sign_extend(gb.getLe16(), 16);
1582 history[0] = sign_extend(gb.getLe16(), 16);
1583 scale = gb.getLe16();
1584
1585 out[0] = history[1];
1586 out[1] = history[0];
1587
1588 for (int n = 0; n < 15; n++) {
1589 unsigned byte = gb.getByte();
1590 int32_t nibble[2];
1591
1592 nibble[0] = sign_extend(byte & 15, 4);
1593 nibble[1] = sign_extend(byte >> 4, 4);
1594
1595 out[2 + n * 2] = nibble[0] * scale +
1596 ((history[0] * 3667 - history[1] * 1642) >> 11);
1597 history[1] = history[0];
1598 history[0] = out[2 + n * 2];
1599
1600 out[2 + n * 2 + 1] = nibble[1] * scale +
1601 ((history[0] * 3667 - history[1] * 1642) >> 11);
1602 history[1] = history[0];
1603 history[0] = out[2 + n * 2 + 1];
1604 }
1605 }
1606
1607 block++;
1608 }
1609 bytes_remaining = gb.bytesLeft();
1610 if (bytes_remaining > 0) {
1611 gb.skip(bytes_remaining);
1612 }
1613 return AV_OK;
1614 }
1615};
1616
1618 public:
1619 DecoderADPCM_XA() {
1620 setCodecID(AV_CODEC_ID_ADPCM_XA);
1621 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
1622 }
1623 int xa_decode(int16_t *out0, int16_t *out1, const uint8_t *in,
1625 int channels, int sample_offset) {
1626 int i, j;
1627 int shift, filter, f0, f1;
1628 int s_1, s_2;
1629 int d, s, t;
1630
1632 if (channels == 1)
1633 out1 = out0 + 28;
1634 else
1636
1637 for (i = 0; i < 4; i++) {
1638 shift = 12 - (in[4 + i * 2] & 15);
1639 filter = in[4 + i * 2] >> 4;
1640 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
1641 avpriv_request_sample(&avctx, "unknown XA-ADPCM filter %d", filter);
1642 filter = 0;
1643 }
1644 if (shift < 0) {
1645 avpriv_request_sample(&avctx, "unknown XA-ADPCM shift %d", shift);
1646 shift = 0;
1647 }
1648 f0 = xa_adpcm_table[filter][0];
1649 f1 = xa_adpcm_table[filter][1];
1650
1651 s_1 = left->sample1;
1652 s_2 = left->sample2;
1653
1654 for (j = 0; j < 28; j++) {
1655 d = in[16 + i + j * 4];
1656
1657 t = sign_extend(d, 4);
1658 s = t * (1 << shift) + ((s_1 * f0 + s_2 * f1 + 32) >> 6);
1659 s_2 = s_1;
1660 s_1 = av_clip_int16(s);
1661 out0[j] = s_1;
1662 }
1663
1664 if (channels == 2) {
1665 left->sample1 = s_1;
1666 left->sample2 = s_2;
1667 s_1 = right->sample1;
1668 s_2 = right->sample2;
1669 }
1670
1671 shift = 12 - (in[5 + i * 2] & 15);
1672 filter = in[5 + i * 2] >> 4;
1673 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table) || shift < 0) {
1674 avpriv_request_sample(&avctx, "unknown XA-ADPCM filter %d", filter);
1675 filter = 0;
1676 }
1677 if (shift < 0) {
1678 avpriv_request_sample(&avctx, "unknown XA-ADPCM shift %d", shift);
1679 shift = 0;
1680 }
1681
1682 f0 = xa_adpcm_table[filter][0];
1683 f1 = xa_adpcm_table[filter][1];
1684
1685 for (j = 0; j < 28; j++) {
1686 d = in[16 + i + j * 4];
1687
1688 t = sign_extend(d >> 4, 4);
1689 s = t * (1 << shift) + ((s_1 * f0 + s_2 * f1 + 32) >> 6);
1690 s_2 = s_1;
1691 s_1 = av_clip_int16(s);
1692 out1[j] = s_1;
1693 }
1694
1695 if (channels == 2) {
1696 right->sample1 = s_1;
1697 right->sample2 = s_2;
1698 } else {
1699 left->sample1 = s_1;
1700 left->sample2 = s_2;
1701 }
1702
1703 out0 += 28 * (3 - channels);
1704 out1 += 28 * (3 - channels);
1705 }
1706
1707 return 0;
1708 }
1709
1710 protected:
1711 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
1712 int16_t *out0 = samples_p[0];
1713 int16_t *out1 = samples_p[1];
1714 int samples_per_block = 28 * (3 - channels()) * 4;
1715 int sample_offset = 0;
1716 int bytes_remaining;
1717 while (gb.bytesLeft() >= 128) {
1718 if ((ret =
1719 xa_decode(out0, out1, buf + gb.tell(), &c->status[0],
1720 &c->status[1], channels(), sample_offset)) < 0)
1721 return ret;
1722 gb.skipUnchecked(128);
1724 }
1725 /* Less than a full block of data left, e.g. when reading from
1726 * 2324 byte per sector XA; the remainder is padding */
1727 bytes_remaining = gb.bytesLeft();
1728 if (bytes_remaining > 0) {
1729 gb.skip(bytes_remaining);
1730 }
1731 return AV_OK;
1732 }
1733};
1734
1736 public:
1738 setCodecID(AV_CODEC_ID_ADPCM_IMA_EA_EACS);
1739 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1740 }
1741 protected:
1742 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
1743 for (int i = 0; i <= st; i++) {
1744 c->status[i].step_index = gb.getLe32Unchecked();
1745 if (c->status[i].step_index > 88u) {
1746 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n", i,
1747 c->status[i].step_index);
1748 return AVERROR_INVALIDDATA;
1749 }
1750 }
1751 for (int i = 0; i <= st; i++) {
1752 c->status[i].predictor = gb.getLe32Unchecked();
1753 if (FFABS((int64_t)c->status[i].predictor) > (1 << 16))
1754 return AVERROR_INVALIDDATA;
1755 }
1756
1757 for (int n = nb_samples >> (1 - st); n > 0; n--) {
1758 int byte = gb.getByteUnchecked();
1759 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3);
1760 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3);
1761 }
1762 return AV_OK;
1763 }
1764};
1765
1767 public:
1769 setCodecID(AV_CODEC_ID_ADPCM_IMA_EA_SEAD);
1770 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1771 }
1772 protected:
1773 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
1774 for (int n = nb_samples >> (1 - st); n > 0; n--) {
1775 int byte = gb.getByteUnchecked();
1776 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6);
1777 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6);
1778 }
1779 return AV_OK;
1780 }
1781};
1782
1784 public:
1785 DecoderADPCM_EA() {
1786 setCodecID(AV_CODEC_ID_ADPCM_EA);
1787 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1788 }
1789 protected:
1790 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
1796
1797 /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
1798 each coding 28 stereo samples. */
1799
1800 if (channels() != 2) return AVERROR_INVALIDDATA;
1801
1802 current_left_sample = sign_extend(gb.getLe16Unchecked(), 16);
1803 previous_left_sample = sign_extend(gb.getLe16Unchecked(), 16);
1804 current_right_sample = sign_extend(gb.getLe16Unchecked(), 16);
1805 previous_right_sample = sign_extend(gb.getLe16Unchecked(), 16);
1806
1807 for (int count1 = 0; count1 < nb_samples / 28; count1++) {
1808 int byte = gb.getByteUnchecked();
1809 coeff1l = ea_adpcm_table[byte >> 4];
1810 coeff2l = ea_adpcm_table[(byte >> 4) + 4];
1811 coeff1r = ea_adpcm_table[byte & 0x0F];
1812 coeff2r = ea_adpcm_table[(byte & 0x0F) + 4];
1813
1814 byte = gb.getByteUnchecked();
1815 shift_left = 20 - (byte >> 4);
1816 shift_right = 20 - (byte & 0x0F);
1817
1818 for (int count2 = 0; count2 < 28; count2++) {
1819 byte = gb.getByteUnchecked();
1820 next_left_sample = sign_extend(byte >> 4, 4) * (1 << shift_left);
1821 next_right_sample = sign_extend(byte, 4) * (1 << shift_right);
1822
1824 (previous_left_sample * coeff2l) + 0x80) >>
1825 8;
1828 (previous_right_sample * coeff2r) + 0x80) >>
1829 8;
1830
1832 current_left_sample = av_clip_int16(next_left_sample);
1834 current_right_sample = av_clip_int16(next_right_sample);
1835 *samples++ = current_left_sample;
1836 *samples++ = current_right_sample;
1837 }
1838 }
1839
1840 gb.skip(2); // Skip terminating 0x0000
1841 return AV_OK;
1842 }
1843};
1844
1846 public:
1848 setCodecID(AV_CODEC_ID_ADPCM_EA_MAXIS_XA);
1849 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1850 }
1851 protected:
1852 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
1853 int coeff[2][2], shift[2];
1854
1855 for (int channel = 0; channel < channels(); channel++) {
1856 int byte = gb.getByteUnchecked();
1857 for (int i = 0; i < 2; i++)
1858 coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4 * i];
1859 shift[channel] = 20 - (byte & 0x0F);
1860 }
1861 for (int count1 = 0; count1 < nb_samples / 2; count1++) {
1862 int byte[2];
1863
1864 byte[0] = gb.getByteUnchecked();
1865 if (st) byte[1] = gb.getByteUnchecked();
1866 for (int i = 4; i >= 0;
1867 i -= 4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
1868 for (int channel = 0; channel < channels(); channel++) {
1869 int sample =
1870 sign_extend(byte[channel] >> i, 4) * (1 << shift[channel]);
1871 sample = (sample + c->status[channel].sample1 * coeff[channel][0] +
1872 c->status[channel].sample2 * coeff[channel][1] + 0x80) >>
1873 8;
1874 c->status[channel].sample2 = c->status[channel].sample1;
1875 c->status[channel].sample1 = av_clip_int16(sample);
1876 *samples++ = c->status[channel].sample1;
1877 }
1878 }
1879 }
1880 gb.seek(0, SEEK_END);
1881 return AV_OK;
1882 }
1883};
1884
1886 public:
1888 setCodecID(AV_CODEC_ID_ADPCM_EA_XAS);
1889 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
1890 }
1891 protected:
1892 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
1893 for (int channel = 0; channel < channels(); channel++) {
1894 int coeff[2][4], shift[4];
1895 int16_t *s = samples_p[channel];
1896 for (int n = 0; n < 4; n++, s += 32) {
1897 int val = sign_extend(gb.getLe16Unchecked(), 16);
1898 for (int i = 0; i < 2; i++)
1899 coeff[i][n] = ea_adpcm_table[(val & 0x0F) + 4 * i];
1900 s[0] = val & ~0x0F;
1901
1902 val = sign_extend(gb.getLe16Unchecked(), 16);
1903 shift[n] = 20 - (val & 0x0F);
1904 s[1] = val & ~0x0F;
1905 }
1906
1907 for (int m = 2; m < 32; m += 2) {
1908 s = &samples_p[channel][m];
1909 for (int n = 0; n < 4; n++, s += 32) {
1910 int level, pred;
1911 int byte = gb.getByteUnchecked();
1912
1913 level = sign_extend(byte >> 4, 4) * (1 << shift[n]);
1914 pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n];
1915 s[0] = av_clip_int16((level + pred + 0x80) >> 8);
1916
1917 level = sign_extend(byte, 4) * (1 << shift[n]);
1918 pred = s[0] * coeff[0][n] + s[-1] * coeff[1][n];
1919 s[1] = av_clip_int16((level + pred + 0x80) >> 8);
1920 }
1921 }
1922 }
1923 return AV_OK;
1924 }
1925};
1926
1928 public:
1930 setCodecID(AV_CODEC_ID_ADPCM_IMA_ACORN);
1931 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1932 }
1933 protected:
1934 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
1935 for (int channel = 0; channel < channels(); channel++) {
1936 ADPCMChannelStatus *cs = &c->status[channel];
1937 cs->predictor = sign_extend(gb.getLe16Unchecked(), 16);
1938 cs->step_index = gb.getLe16Unchecked() & 0xFF;
1939 if (cs->step_index > 88u) {
1940 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n", channel,
1941 cs->step_index);
1942 return AVERROR_INVALIDDATA;
1943 }
1944 }
1945 for (int n = nb_samples >> (1 - st); n > 0; n--) {
1946 int byte = gb.getByteUnchecked();
1947 *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte & 0x0F, 3);
1948 *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte >> 4, 3);
1949 }
1950 return AV_OK;
1951 }
1952};
1953
1955 public:
1957 setCodecID(AV_CODEC_ID_ADPCM_IMA_AMV);
1958 sample_formats.push_back(AV_SAMPLE_FMT_S16);
1959 }
1960 protected:
1961 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
1962 av_assert(channels() == 1);
1963
1964 /*
1965 * Header format:
1966 * int16_t predictor;
1967 * uint8_t step_index;
1968 * uint8_t reserved;
1969 * uint32_t frame_size;
1970 *
1971 * Some implementations have step_index as 16-bits, but others
1972 * only use the lower 8 and store garbage in the upper 8.
1973 */
1974 c->status[0].predictor = sign_extend(gb.getLe16Unchecked(), 16);
1975 c->status[0].step_index = gb.getByteUnchecked();
1976 gb.skipUnchecked(5);
1977 if (c->status[0].step_index > 88u) {
1978 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
1979 c->status[0].step_index);
1980 return AVERROR_INVALIDDATA;
1981 }
1982
1983 for (int n = nb_samples >> 1; n > 0; n--) {
1984 int v = gb.getByteUnchecked();
1985
1986 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1987 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
1988 }
1989
1990 if (nb_samples & 1) {
1991 int v = gb.getByteUnchecked();
1992 *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
1993
1994 if (v & 0x0F) {
1995 /* Holds true on all the http://samples.mplayerhq.hu/amv samples. */
1996 av_log(avctx, AV_LOG_WARNING,
1997 "Last nibble set on packet with odd sample count.\n");
1998 av_log(avctx, AV_LOG_WARNING, "Sample will be skipped.\n");
1999 }
2000 }
2001 return AV_OK;
2002 }
2003};
2004
2006 public:
2008 setCodecID(AV_CODEC_ID_ADPCM_IMA_SMJPEG);
2009 sample_formats.push_back(AV_SAMPLE_FMT_S16);
2010 }
2011 protected:
2012 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
2013 for (int i = 0; i < channels(); i++) {
2014 c->status[i].predictor = sign_extend(gb.getBe16Unchecked(), 16);
2015 c->status[i].step_index = gb.getByteUnchecked();
2016 gb.skipUnchecked(1);
2017 if (c->status[i].step_index > 88u) {
2018 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n",
2019 c->status[i].step_index);
2020 return AVERROR_INVALIDDATA;
2021 }
2022 }
2023
2024 for (int n = nb_samples >> (1 - st); n > 0; n--) {
2025 int v = gb.getByteUnchecked();
2026
2027 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0], v >> 4);
2028 *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf);
2029 }
2030 return AV_OK;
2031 }
2032};
2033
2035 public:
2036 DecoderADPCM_CT() {
2037 setCodecID(AV_CODEC_ID_ADPCM_CT);
2038 sample_formats.push_back(AV_SAMPLE_FMT_S16);
2039 }
2040 int16_t adpcm_ct_expand_nibble(ADPCMChannelStatus *c, int8_t nibble) {
2041 int sign, delta, diff;
2042 int new_step;
2043
2044 sign = nibble & 8;
2045 delta = nibble & 7;
2046 /* perform direct multiplication instead of series of jumps proposed by
2047 * the reference ADPCM implementation since modern CPUs can do the mults
2048 * quickly enough */
2049 diff = ((2 * delta + 1) * c->step) >> 3;
2050 /* predictor update is not so trivial: predictor is multiplied on 254/256
2051 * before updating */
2052 c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
2053 c->predictor = av_clip_int16(c->predictor);
2054 /* calculate new step and clamp it to range 511..32767 */
2055 new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
2056 c->step = av_clip(new_step, 511, 32767);
2057
2058 return (int16_t)c->predictor;
2059 }
2060
2061 protected:
2062 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
2063 for (int n = nb_samples >> (1 - st); n > 0; n--) {
2064 int v = gb.getByteUnchecked();
2065 *samples++ = adpcm_ct_expand_nibble(&c->status[0], v >> 4);
2066 *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F);
2067 }
2068 return AV_OK;
2069 }
2070};
2071
2073 public:
2075 setCodecID(AV_CODEC_ID_ADPCM_SWF);
2076 sample_formats.push_back(AV_SAMPLE_FMT_S16);
2077 }
2078 void adpcm_swf_decode(const uint8_t *buf, int buf_size, int16_t *samples) {
2079 ADPCMDecodeContext *c = (ADPCMDecodeContext *)avctx.priv_data;
2080 BitReader gb;
2081 const int8_t *table;
2082 int channels = avctx.nb_channels;
2083 int k0, signmask, nb_bits, count;
2084 int size = buf_size * 8;
2085 int i;
2086
2087 gb.init(buf, size);
2088 gb.setMsbFirst(true); // SWF bit fields are most significant bit first
2089
2090 // read bits & initial values
2091 nb_bits = gb.read(2) + 2;
2092 table = swf_index_tables[nb_bits - 2];
2093 k0 = 1 << (nb_bits - 2);
2094 signmask = 1 << (nb_bits - 1);
2095
2096 while (gb.bitsCount() <= size - 22 * channels) {
2097 for (i = 0; i < channels; i++) {
2098 *samples++ = c->status[i].predictor = gb.readSigned(16);
2099 c->status[i].step_index = gb.read(6);
2100 }
2101
2102 for (count = 0;
2103 gb.bitsCount() <= size - nb_bits * channels && count < 4095;
2104 count++) {
2105 int i;
2106
2107 for (i = 0; i < channels; i++) {
2108 // similar to IMA adpcm
2109 int delta = gb.read(nb_bits);
2110 int step = ff_adpcm_step_table[c->status[i].step_index];
2111 int vpdiff = 0; // vpdiff = (delta+0.5)*step/4
2112 int k = k0;
2113
2114 do {
2115 if (delta & k) vpdiff += step;
2116 step >>= 1;
2117 k >>= 1;
2118 } while (k);
2119 vpdiff += step;
2120
2121 if (delta & signmask)
2122 c->status[i].predictor -= vpdiff;
2123 else
2124 c->status[i].predictor += vpdiff;
2125
2126 c->status[i].step_index += table[delta & (~signmask)];
2127
2128 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
2129 c->status[i].predictor = av_clip_int16(c->status[i].predictor);
2130
2131 *samples++ = c->status[i].predictor;
2132 }
2133 }
2134 }
2135 }
2136 protected:
2137 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
2138 adpcm_swf_decode(buf, buf_size, samples);
2139 gb.seek(0, SEEK_END);
2140 return AV_OK;
2141 }
2142};
2143
2145 public:
2147 setCodecID(AV_CODEC_ID_ADPCM_YAMAHA);
2148 sample_formats.push_back(AV_SAMPLE_FMT_S16);
2149 }
2150 protected:
2151 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
2152 for (int n = nb_samples >> (1 - st); n > 0; n--) {
2153 int v = gb.getByteUnchecked();
2154 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0], v & 0x0F);
2155 *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4);
2156 }
2157 return AV_OK;
2158 }
2159};
2160
2162 public:
2164 setCodecID(AV_CODEC_ID_ADPCM_AICA);
2165 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
2166 }
2167 protected:
2168 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
2169 for (int channel = 0; channel < channels(); channel++) {
2170 samples = samples_p[channel];
2171 for (int n = nb_samples >> 1; n > 0; n--) {
2172 int v = gb.getByteUnchecked();
2173 *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v & 0x0F);
2174 *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v >> 4);
2175 }
2176 }
2177 return AV_OK;
2178 }
2179};
2180
2182 public:
2184 setCodecID(AV_CODEC_ID_ADPCM_AFC);
2185 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
2186 }
2187 protected:
2188 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
2190 int blocks;
2191
2192 if (avctx.extradata && avctx.extradata_size == 1 && avctx.extradata[0]) {
2193 samples_per_block = avctx.extradata[0] / 16;
2194 blocks = nb_samples / avctx.extradata[0];
2195 } else {
2196 samples_per_block = nb_samples / 16;
2197 blocks = 1;
2198 }
2199
2200 for (int m = 0; m < blocks; m++) {
2201 for (int channel = 0; channel < channels(); channel++) {
2202 int prev1 = c->status[channel].sample1;
2203 int prev2 = c->status[channel].sample2;
2204
2205 samples = samples_p[channel] + m * 16;
2206 /* Read in every sample for this channel. */
2207 for (int i = 0; i < samples_per_block; i++) {
2208 int byte = gb.getByteUnchecked();
2209 int scale = 1 << (byte >> 4);
2210 int index = byte & 0xf;
2211 int factor1 = afc_coeffs[0][index];
2212 int factor2 = afc_coeffs[1][index];
2213
2214 /* Decode 16 samples. */
2215 for (int n = 0; n < 16; n++) {
2217
2218 if (n & 1) {
2219 sampledat = sign_extend(byte, 4);
2220 } else {
2221 byte = gb.getByteUnchecked();
2222 sampledat = sign_extend(byte >> 4, 4);
2223 }
2224
2225 sampledat =
2226 ((prev1 * factor1 + prev2 * factor2) >> 11) + sampledat * scale;
2227 *samples = av_clip_int16(sampledat);
2228 prev2 = prev1;
2229 prev1 = *samples++;
2230 }
2231 }
2232
2233 c->status[channel].sample1 = prev1;
2234 c->status[channel].sample2 = prev2;
2235 }
2236 }
2237 gb.seek(0, SEEK_END);
2238 return AV_OK;
2239 }
2240};
2241
2243 public:
2245 setCodecID(AV_CODEC_ID_ADPCM_DTK);
2246 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
2247 }
2248 protected:
2249 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
2250 for (int channel = 0; channel < channels(); channel++) {
2251 samples = samples_p[channel];
2252
2253 /* Read in every sample for this channel. */
2254 for (int i = 0; i < nb_samples / 28; i++) {
2255 int byte, header;
2256 if (channel) gb.skipUnchecked(1);
2257 header = gb.getByteUnchecked();
2258 gb.skipUnchecked(3 - channel);
2259
2260 /* Decode 28 samples. */
2261 for (int n = 0; n < 28; n++) {
2262 int32_t sampledat, prev;
2263
2264 switch (header >> 4) {
2265 case 1:
2266 prev = (c->status[channel].sample1 * 0x3c);
2267 break;
2268 case 2:
2269 prev = (c->status[channel].sample1 * 0x73) -
2270 (c->status[channel].sample2 * 0x34);
2271 break;
2272 case 3:
2273 prev = (c->status[channel].sample1 * 0x62) -
2274 (c->status[channel].sample2 * 0x37);
2275 break;
2276 default:
2277 prev = 0;
2278 }
2279
2280 prev = av_clip_intp2((prev + 0x20) >> 6, 21);
2281
2282 byte = gb.getByteUnchecked();
2283 if (!channel)
2284 sampledat = sign_extend(byte, 4);
2285 else
2286 sampledat = sign_extend(byte >> 4, 4);
2287
2288 sampledat =
2289 ((sampledat * (1 << 12)) >> (header & 0xf)) * (1 << 6) + prev;
2290 *samples++ = av_clip_int16(sampledat >> 6);
2291 c->status[channel].sample2 = c->status[channel].sample1;
2292 c->status[channel].sample1 = sampledat;
2293 }
2294 }
2295 if (!channel) gb.seek(0, SEEK_SET);
2296 }
2297 return AV_OK;
2298 }
2299};
2300
2302 public:
2304 setCodecID(AV_CODEC_ID_ADPCM_PSX);
2305 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
2306 }
2307 protected:
2308 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
2309 for (int block = 0;
2310 block < avpkt->size / FFMAX(avctx.block_align, 16 * channels());
2311 block++) {
2313 28 * FFMAX(avctx.block_align, 16 * channels()) / (16 * channels());
2314 for (int channel = 0; channel < channels(); channel++) {
2315 samples = samples_p[channel] + block * nb_samples_per_block;
2316 av_assert((block + 1) * nb_samples_per_block <= nb_samples);
2317
2318 /* Read in every sample for this channel. */
2319 for (int i = 0; i < nb_samples_per_block / 28; i++) {
2320 int filter, shift, flag, byte;
2321
2322 filter = gb.getByteUnchecked();
2323 shift = filter & 0xf;
2324 filter = filter >> 4;
2325 if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table))
2326 return AVERROR_INVALIDDATA;
2327 flag = gb.getByteUnchecked() & 0x7;
2328
2329 /* Decode 28 samples. */
2330 for (int n = 0; n < 28; n++) {
2331 int sample = 0, scale;
2332
2333 if (n & 1) {
2334 scale = sign_extend(byte >> 4, 4);
2335 } else {
2336 byte = gb.getByteUnchecked();
2337 scale = sign_extend(byte, 4);
2338 }
2339
2340 if (flag < 0x07) {
2341 scale = scale * (1 << 12);
2342 sample =
2343 (int)((scale >> shift) + (c->status[channel].sample1 *
2344 xa_adpcm_table[filter][0] +
2345 c->status[channel].sample2 *
2346 xa_adpcm_table[filter][1]) /
2347 64);
2348 }
2349 *samples++ = av_clip_int16(sample);
2350 c->status[channel].sample2 = c->status[channel].sample1;
2351 c->status[channel].sample1 = sample;
2352 }
2353 }
2354 }
2355 }
2356 return AV_OK;
2357 }
2358};
2359
2361 public:
2363 setCodecID(AV_CODEC_ID_ADPCM_ARGO);
2364 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
2365 }
2366 int16_t ff_adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble,
2367 int shift, int flag) {
2368 int sample = sign_extend(nibble, 4) * (1 << shift);
2369
2370 if (flag)
2371 sample += (8 * cs->sample1) - (4 * cs->sample2);
2372 else
2373 sample += 4 * cs->sample1;
2374
2375 sample = av_clip_int16(sample >> 2);
2376
2377 cs->sample2 = cs->sample1;
2378 cs->sample1 = sample;
2379
2380 return sample;
2381 }
2382
2383 protected:
2384 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
2385 /*
2386 * The format of each block:
2387 * uint8_t left_control;
2388 * uint4_t left_samples[nb_samples];
2389 * ---- and if stereo ----
2390 * uint8_t right_control;
2391 * uint4_t right_samples[nb_samples];
2392 *
2393 * Format of the control byte:
2394 * MSB [SSSSRDRR] LSB
2395 * S = (Shift Amount - 2)
2396 * D = Decoder flag.
2397 * R = Reserved
2398 *
2399 * Each block relies on the previous two samples of each channel.
2400 * They should be 0 initially.
2401 */
2402 for (int block = 0; block < avpkt->size / avctx.block_align; block++) {
2403 for (int channel = 0; channel < avctx.nb_channels; channel++) {
2404 ADPCMChannelStatus *cs = c->status + channel;
2405 int control, shift;
2406
2407 samples = samples_p[channel] + block * 32;
2408
2409 /* Get the control byte and decode the samples, 2 at a time. */
2410 control = gb.getByteUnchecked();
2411 shift = (control >> 4) + 2;
2412
2413 for (int n = 0; n < 16; n++) {
2414 int sample = gb.getByteUnchecked();
2415 *samples++ = ff_adpcm_argo_expand_nibble(cs, sample >> 4, shift,
2416 control & 0x04);
2417 *samples++ = ff_adpcm_argo_expand_nibble(cs, sample >> 0, shift,
2418 control & 0x04);
2419 }
2420 }
2421 }
2422 return AV_OK;
2423 }
2424};
2425
2427 public:
2429 setCodecID(AV_CODEC_ID_ADPCM_ZORK);
2430 sample_formats.push_back(AV_SAMPLE_FMT_S16);
2431 }
2432 int16_t adpcm_zork_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble) {
2433 int16_t index = c->step_index;
2434 uint32_t lookup_sample = ff_adpcm_step_table[index];
2435 int32_t sample = 0;
2436
2437 if (nibble & 0x40) sample += lookup_sample;
2438 if (nibble & 0x20) sample += lookup_sample >> 1;
2439 if (nibble & 0x10) sample += lookup_sample >> 2;
2440 if (nibble & 0x08) sample += lookup_sample >> 3;
2441 if (nibble & 0x04) sample += lookup_sample >> 4;
2442 if (nibble & 0x02) sample += lookup_sample >> 5;
2443 if (nibble & 0x01) sample += lookup_sample >> 6;
2444 if (nibble & 0x80) sample = -sample;
2445
2446 sample += c->predictor;
2447 sample = av_clip_int16(sample);
2448
2449 index += zork_index_table[(nibble >> 4) & 7];
2450 index = av_clip(index, 0, 88);
2451
2452 c->predictor = sample;
2453 c->step_index = index;
2454
2455 return sample;
2456 }
2457 protected:
2458 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
2459 for (int n = 0; n < nb_samples * channels(); n++) {
2460 int v = gb.getByteUnchecked();
2461 *samples++ = adpcm_zork_expand_nibble(&c->status[n % channels()], v);
2462 }
2463 return AV_OK;
2464 }
2465};
2466
2468 public:
2470 setCodecID(AV_CODEC_ID_ADPCM_IMA_MTF);
2471 sample_formats.push_back(AV_SAMPLE_FMT_S16);
2472 }
2473 protected:
2474 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
2475 for (int n = nb_samples / 2; n > 0; n--) {
2476 for (int channel = 0; channel < channels(); channel++) {
2477 int v = gb.getByteUnchecked();
2478 *samples++ = adpcm_ima_mtf_expand_nibble(&c->status[channel], v >> 4);
2479 samples[st] =
2480 adpcm_ima_mtf_expand_nibble(&c->status[channel], v & 0x0F);
2481 }
2482 samples += channels();
2483 }
2484 return AV_OK;
2485 }
2486};
2487
2489 public:
2490 DecoderADPCM_SBPRO_X(AVCodecID id) {
2491 setCodecID(id);
2492 sample_formats.push_back(AV_SAMPLE_FMT_S16);
2493 // the enum orders these IDs as SBPRO_4, SBPRO_3, SBPRO_2, so test membership
2494 assert(id == AV_CODEC_ID_ADPCM_SBPRO_2 || id == AV_CODEC_ID_ADPCM_SBPRO_3 ||
2495 id == AV_CODEC_ID_ADPCM_SBPRO_4);
2496 }
2497 int16_t adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, int8_t nibble,
2498 int size, int shift) {
2499 int sign, delta, diff;
2500
2501 sign = nibble & (1 << (size - 1));
2502 delta = nibble & ((1 << (size - 1)) - 1);
2503 diff = delta << (7 + c->step + shift);
2504
2505 /* clamp result */
2506 c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384, 16256);
2507
2508 /* calculate new step */
2509 if (delta >= (2 * size - 3) && c->step < 3)
2510 c->step++;
2511 else if (delta == 0 && c->step > 0)
2512 c->step--;
2513
2514 return (int16_t)c->predictor;
2515 }
2516
2517 protected:
2518 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
2519 if (!c->status[0].step_index) {
2520 /* the first byte is a raw sample */
2521 *samples++ = 128 * (gb.getByteUnchecked() - 0x80);
2522 if (st) *samples++ = 128 * (gb.getByteUnchecked() - 0x80);
2523 c->status[0].step_index = 1;
2524 nb_samples--;
2525 }
2526 /* every packet byte is unpacked into samples_per_byte values; the last
2527 byte may only be partly used when the sample count is not a multiple of
2528 the packing, so output stops at exactly nb_samples per channel */
2529 const int need = nb_samples * channels();
2530 int produced = 0;
2531 while (produced < need && gb.bytesLeft() > 0) {
2532 int byte = gb.getByteUnchecked();
2533 int idx[4], nib[4], size[4], shift[4], count;
2534 if (avctx.codec_id == AV_CODEC_ID_ADPCM_SBPRO_4) {
2535 count = 2;
2536 idx[0] = 0; nib[0] = byte >> 4; size[0] = 4; shift[0] = 0;
2537 idx[1] = st; nib[1] = byte & 0x0F; size[1] = 4; shift[1] = 0;
2538 } else if (avctx.codec_id == AV_CODEC_ID_ADPCM_SBPRO_3) {
2539 count = 3;
2540 idx[0] = 0; nib[0] = byte >> 5; size[0] = 3; shift[0] = 0;
2541 idx[1] = 0; nib[1] = (byte >> 2) & 7; size[1] = 3; shift[1] = 0;
2542 idx[2] = 0; nib[2] = byte & 3; size[2] = 2; shift[2] = 0;
2543 } else {
2544 count = 4;
2545 idx[0] = 0; nib[0] = byte >> 6; size[0] = 2; shift[0] = 2;
2546 idx[1] = st; nib[1] = (byte >> 4) & 3; size[1] = 2; shift[1] = 2;
2547 idx[2] = 0; nib[2] = (byte >> 2) & 3; size[2] = 2; shift[2] = 2;
2548 idx[3] = st; nib[3] = byte & 3; size[3] = 2; shift[3] = 2;
2549 }
2550 for (int k = 0; k < count && produced < need; k++, produced++) {
2551 *samples++ = adpcm_sbpro_expand_nibble(&c->status[idx[k]], nib[k],
2552 size[k], shift[k]);
2553 }
2554 }
2555 return AV_OK;
2556 }
2557};
2558
2560 public:
2561 DecoderADPCM_SBPRO_2() : DecoderADPCM_SBPRO_X(AV_CODEC_ID_ADPCM_SBPRO_2) {}
2562};
2563
2565 public:
2566 DecoderADPCM_SBPRO_3() : DecoderADPCM_SBPRO_X(AV_CODEC_ID_ADPCM_SBPRO_3) {}
2567};
2568
2570 public:
2571 DecoderADPCM_SBPRO_4() : DecoderADPCM_SBPRO_X(AV_CODEC_ID_ADPCM_SBPRO_4) {}
2572};
2573
2575 public:
2576 DecoderADPCM_THP() : DecoderADPCM_THP(AV_CODEC_ID_ADPCM_THP) {}
2577 DecoderADPCM_THP(AVCodecID id) {
2578 setCodecID(id);
2579 assert(id == AV_CODEC_ID_ADPCM_THP || id == AV_CODEC_ID_ADPCM_THP_LE);
2580 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
2581 }
2582 protected:
2583 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
2584 int table[14][16];
2585
2586#define THP_GET16(g) \
2587 sign_extend(avctx.codec_id == AV_CODEC_ID_ADPCM_THP_LE \
2588 ? g.getLe16Unchecked() \
2589 : g.getBe16Unchecked(), \
2590 16)
2591
2592 if (avctx.extradata) {
2593 ByteReader tb;
2594 if (avctx.extradata_size < 32 * channels()) {
2595 av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
2596 return AVERROR_INVALIDDATA;
2597 }
2598
2599 tb.init(avctx.extradata, avctx.extradata_size);
2600 for (int i = 0; i < channels(); i++)
2601 for (int n = 0; n < 16; n++) table[i][n] = THP_GET16(tb);
2602 } else {
2603 // table (32 bytes/ch) + initial state (4 bytes/ch) must be present
2604 if (gb.bytesLeft() < 36 * channels()) {
2605 av_log(avctx, AV_LOG_ERROR, "THP packet too short for header\n");
2606 return AVERROR_INVALIDDATA;
2607 }
2608 for (int i = 0; i < channels(); i++)
2609 for (int n = 0; n < 16; n++) table[i][n] = THP_GET16(gb);
2610
2611 if (!c->has_status) {
2612 /* Initialize the previous sample. */
2613 for (int i = 0; i < channels(); i++) {
2614 c->status[i].sample1 = THP_GET16(gb);
2615 c->status[i].sample2 = THP_GET16(gb);
2616 }
2617 c->has_status = 1;
2618 } else {
2619 gb.skip(channels() * 4);
2620 }
2621 }
2622
2623 for (int ch = 0; ch < channels(); ch++) {
2624 samples = samples_p[ch];
2625
2626 /* Read in every sample for this channel. */
2627 for (int i = 0; i < (nb_samples + 13) / 14; i++) {
2628 int byte = gb.getByteUnchecked();
2629 int index = (byte >> 4) & 7;
2630 unsigned int exp = byte & 0x0F;
2631 int64_t factor1 = table[ch][index * 2];
2632 int64_t factor2 = table[ch][index * 2 + 1];
2633
2634 /* Decode 14 samples. */
2635 for (int n = 0; n < 14 && (i * 14 + n < nb_samples); n++) {
2637
2638 if (n & 1) {
2639 sampledat = sign_extend(byte, 4);
2640 } else {
2641 byte = gb.getByteUnchecked();
2642 sampledat = sign_extend(byte >> 4, 4);
2643 }
2644
2645 sampledat = ((c->status[ch].sample1 * factor1 +
2646 c->status[ch].sample2 * factor2) >>
2647 11) +
2648 sampledat * (1 << exp);
2649 *samples = av_clip_int16(sampledat);
2650 c->status[ch].sample2 = c->status[ch].sample1;
2651 c->status[ch].sample1 = *samples++;
2652 }
2653 }
2654 }
2655 return AV_OK;
2656 }
2657};
2658
2660 public:
2661 DecoderADPCM_THP_LE() : DecoderADPCM_THP(AV_CODEC_ID_ADPCM_THP_LE) {}
2662};
2663
2664// r1 - r3
2666 public:
2667 DecoderADPCM_EA_RX(AVCodecID id) {
2668 setCodecID(id);
2669 assert(id == AV_CODEC_ID_ADPCM_EA_R1 || id == AV_CODEC_ID_ADPCM_EA_R2 ||
2670 id == AV_CODEC_ID_ADPCM_EA_R3);
2671 sample_formats.push_back(AV_SAMPLE_FMT_S16P);
2672 }
2673 protected:
2674 int decode_frame_impl(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) {
2675 /* channel numbering
2676 2chan: 0=fl, 1=fr
2677 4chan: 0=fl, 1=rl, 2=fr, 3=rr
2678 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
2679 const int big_endian = avctx.codec_id == AV_CODEC_ID_ADPCM_EA_R3;
2681 int coeff1, coeff2;
2682 int shift;
2684 int count = 0;
2685 int offsets[6];
2686
2687 for (unsigned channel = 0; channel < channels(); channel++)
2688 offsets[channel] =
2689 (big_endian ? gb.getBe32() : gb.getLe32()) +
2690 (channels() + 1) * 4;
2691
2692 for (unsigned channel = 0; channel < channels(); channel++) {
2693 int count1;
2694
2695 gb.seek(offsets[channel], SEEK_SET);
2696 samplesC = samples_p[channel];
2697
2698 if (avctx.codec_id == AV_CODEC_ID_ADPCM_EA_R1) {
2699 current_sample = sign_extend(gb.getLe16(), 16);
2700 previous_sample = sign_extend(gb.getLe16(), 16);
2701 } else {
2702 current_sample = c->status[channel].predictor;
2703 previous_sample = c->status[channel].prev_sample;
2704 }
2705
2706 for (count1 = 0; count1 < nb_samples / 28; count1++) {
2707 int byte = gb.getByte();
2708 if (byte == 0xEE) { /* only seen in R2 and R3 */
2709 current_sample = sign_extend(gb.getBe16(), 16);
2710 previous_sample = sign_extend(gb.getBe16(), 16);
2711
2712 for (int count2 = 0; count2 < 28; count2++)
2713 *samplesC++ = sign_extend(gb.getBe16(), 16);
2714 } else {
2715 coeff1 = ea_adpcm_table[byte >> 4];
2716 coeff2 = ea_adpcm_table[(byte >> 4) + 4];
2717 shift = 20 - (byte & 0x0F);
2718
2719 for (int count2 = 0; count2 < 28; count2++) {
2720 if (count2 & 1)
2721 next_sample = (unsigned)sign_extend(byte, 4) << shift;
2722 else {
2723 byte = gb.getByte();
2724 next_sample = (unsigned)sign_extend(byte >> 4, 4) << shift;
2725 }
2726
2727 next_sample +=
2728 (current_sample * coeff1) + (previous_sample * coeff2);
2729 next_sample = av_clip_int16(next_sample >> 8);
2730
2734 }
2735 }
2736 }
2737 if (!count) {
2738 count = count1;
2739 } else if (count != count1) {
2740 av_log(avctx, AV_LOG_WARNING, "per-channel sample count mismatch\n");
2741 count = FFMAX(count, count1);
2742 }
2743
2744 if (avctx.codec_id != AV_CODEC_ID_ADPCM_EA_R1) {
2745 c->status[channel].predictor = current_sample;
2746 c->status[channel].prev_sample = previous_sample;
2747 }
2748 }
2749
2750 frame->nb_samples = count * 28;
2751 gb.seek(0, SEEK_END);
2752 return AV_OK;
2753 }
2754};
2755
2757 public:
2758 DecoderADPCM_EA_R1() : DecoderADPCM_EA_RX(AV_CODEC_ID_ADPCM_EA_R1) {}
2759};
2760
2762 public:
2763 DecoderADPCM_EA_R2() : DecoderADPCM_EA_RX(AV_CODEC_ID_ADPCM_EA_R2) {}
2764};
2765
2767 public:
2768 DecoderADPCM_EA_R3() : DecoderADPCM_EA_RX(AV_CODEC_ID_ADPCM_EA_R3) {}
2769};
2770
2772 public:
2773 static ADPCMDecoder *create(AVCodecID id) {
2774 switch (id) {
2775 case AV_CODEC_ID_ADPCM_IMA_QT:
2776 return new DecoderADPCM_IMA_QT();
2777 case AV_CODEC_ID_ADPCM_IMA_WAV:
2778 return new DecoderADPCM_IMA_WAV();
2779 case AV_CODEC_ID_ADPCM_IMA_DK3:
2780 return new DecoderADPCM_IMA_DK3();
2781 case AV_CODEC_ID_ADPCM_IMA_DK4:
2782 return new DecoderADPCM_IMA_DK4();
2783 case AV_CODEC_ID_ADPCM_IMA_WS:
2784 return new DecoderADPCM_IMA_WS();
2785 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
2786 return new DecoderADPCM_IMA_SMJPEG();
2787 case AV_CODEC_ID_ADPCM_MS:
2788 return new DecoderADPCM_MS();
2789 case AV_CODEC_ID_ADPCM_4XM:
2790 return new DecoderADPCM_4XM();
2791 case AV_CODEC_ID_ADPCM_XA:
2792 return new DecoderADPCM_XA();
2793 case AV_CODEC_ID_ADPCM_EA:
2794 return new DecoderADPCM_EA();
2795 case AV_CODEC_ID_ADPCM_CT:
2796 return new DecoderADPCM_CT();
2797 case AV_CODEC_ID_ADPCM_SWF:
2798 return new DecoderADPCM_SWF();
2799 case AV_CODEC_ID_ADPCM_YAMAHA:
2800 return new DecoderADPCM_YAMAHA();
2801 case AV_CODEC_ID_ADPCM_IMA_AMV:
2802 return new DecoderADPCM_IMA_AMV();
2803 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
2804 return new DecoderADPCM_IMA_EA_SEAD();
2805 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
2806 return new DecoderADPCM_IMA_EA_EACS();
2807 case AV_CODEC_ID_ADPCM_EA_XAS:
2808 return new DecoderADPCM_EA_XAS();
2809 case AV_CODEC_ID_ADPCM_EA_MAXIS_XA:
2810 return new DecoderADPCM_EA_MAXIS_XA();
2811 case AV_CODEC_ID_ADPCM_IMA_ISS:
2812 return new DecoderADPCM_IMA_ISS();
2813 case AV_CODEC_ID_ADPCM_IMA_APC:
2814 return new DecoderADPCM_IMA_APC();
2815 case AV_CODEC_ID_ADPCM_AFC:
2816 return new DecoderADPCM_AFC();
2817 case AV_CODEC_ID_ADPCM_IMA_OKI:
2818 return new DecoderADPCM_IMA_OKI();
2819 case AV_CODEC_ID_ADPCM_DTK:
2820 return new DecoderADPCM_DTK();
2821 case AV_CODEC_ID_ADPCM_IMA_RAD:
2822 return new DecoderADPCM_IMA_RAD();
2823 case AV_CODEC_ID_ADPCM_PSX:
2824 return new DecoderADPCM_PSX();
2825 case AV_CODEC_ID_ADPCM_AICA:
2826 return new DecoderADPCM_AICA();
2827 case AV_CODEC_ID_ADPCM_IMA_DAT4:
2828 return new DecoderADPCM_IMA_DAT4();
2829 case AV_CODEC_ID_ADPCM_MTAF:
2830 return new DecoderADPCM_MTAF();
2831 case AV_CODEC_ID_ADPCM_AGM:
2832 return new DecoderADPCM_AGM();
2833 case AV_CODEC_ID_ADPCM_ARGO:
2834 return new DecoderADPCM_ARGO();
2835 case AV_CODEC_ID_ADPCM_IMA_SSI:
2836 return new DecoderADPCM_IMA_SSI();
2837 case AV_CODEC_ID_ADPCM_ZORK:
2838 return new DecoderADPCM_ZORK();
2839 case AV_CODEC_ID_ADPCM_IMA_APM:
2840 return new DecoderADPCM_IMA_APM();
2841 case AV_CODEC_ID_ADPCM_IMA_ALP:
2842 return new DecoderADPCM_IMA_ALP();
2843 case AV_CODEC_ID_ADPCM_IMA_MTF:
2844 return new DecoderADPCM_IMA_MTF();
2845 case AV_CODEC_ID_ADPCM_IMA_CUNNING:
2846 return new DecoderADPCM_IMA_CUNNING();
2847 case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
2848 return new DecoderADPCM_IMA_MOFLEX();
2849 case AV_CODEC_ID_ADPCM_IMA_ACORN:
2850 return new DecoderADPCM_IMA_ACORN();
2851 case AV_CODEC_ID_ADPCM_XMD:
2852 return new DecoderADPCM_XMD();
2853 case AV_CODEC_ID_ADPCM_SBPRO_4:
2854 return new DecoderADPCM_SBPRO_4();
2855 case AV_CODEC_ID_ADPCM_SBPRO_3:
2856 return new DecoderADPCM_SBPRO_3();
2857 case AV_CODEC_ID_ADPCM_SBPRO_2:
2858 return new DecoderADPCM_SBPRO_2();
2859 case AV_CODEC_ID_ADPCM_THP:
2860 return new DecoderADPCM_THP();
2861 case AV_CODEC_ID_ADPCM_THP_LE:
2862 return new DecoderADPCM_THP_LE();
2863 case AV_CODEC_ID_ADPCM_EA_R1:
2864 return new DecoderADPCM_EA_R1();
2865 case AV_CODEC_ID_ADPCM_EA_R2:
2866 return new DecoderADPCM_EA_R2();
2867 case AV_CODEC_ID_ADPCM_EA_R3:
2868 return new DecoderADPCM_EA_R3();
2869 default:
2870 av_log(avctx, AV_LOG_ERROR, "ERROR: decoder [%d] not implemented\n",
2871 id);
2872 return nullptr;
2873 };
2874 }
2875};
2876
2877} // namespace adpcm_ffmpeg
Common ADPCM Functionality.
Definition ADPCMCodec.h:23
Definition ADPCMDecoder.h:2771
ADPCM Decoder.
Definition ADPCMDecoder.h:17
virtual int adpcm_decode_frame(AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Decode a pcm frame.
Definition ADPCMDecoder.h:693
AVFrame & decode(AVPacket &packet)
Definition ADPCMDecoder.h:104
bool begin(int sampleRate, int channels, int frameSize, int blockAlign=0)
Starts the decoder with an explicit frame size (max samples per channel in one packet)....
Definition ADPCMDecoder.h:33
int get_nb_samples(ByteReader &gb, int buf_size, int *coded_samples, int *approx_nb_samples)
Definition ADPCMDecoder.h:419
virtual int adpcm_decode_init()
Init decoder.
Definition ADPCMDecoder.h:193
DataSource getDataSource(int16_t *frame_data, int16_t *ext_data, int len)
Definition ADPCMDecoder.h:182
virtual void adpcm_flush()
Flush decoder.
Definition ADPCMDecoder.h:644
Definition ADPCMEncoder.h:33
Vector implementation which provides the most important methods as defined by std::vector....
Definition ADPCMVector.h:16
Reads bits from a buffer. By default bits are taken from the least significant bit of each byte first...
Definition BitReader.h:30
int init(const uint8_t *buffer, int bit_size)
Definition BitReader.h:41
int bitsCount() const
number of bits consumed so far
Definition BitReader.h:62
void skip(int n)
Skips n bits.
Definition BitReader.h:92
uint32_t read(int n)
Reads n bits (0 to 32)
Definition BitReader.h:68
int32_t readSigned(int n)
Reads n bits and sign extends them (0 to 31)
Definition BitReader.h:83
void setMsbFirst(bool msb)
Selects the bit order used by read() and readSigned()
Definition BitReader.h:37
Bounds checked reader for a memory buffer. Reads past the end return 0 and move the position to the e...
Definition ByteReader.h:45
void skip(unsigned int size)
skips up to size bytes (stops at the end of the buffer)
Definition ByteReader.h:66
int seek(int offset, int whence)
moves the position; returns the new position or an error code
Definition ByteReader.h:71
Definition ADPCMDecoder.h:902
Definition ADPCMDecoder.h:2181
Definition ADPCMDecoder.h:935
Definition ADPCMDecoder.h:2161
Definition ADPCMDecoder.h:2360
Definition ADPCMDecoder.h:2034
Definition ADPCMDecoder.h:2242
Definition ADPCMDecoder.h:1845
Definition ADPCMDecoder.h:2756
Definition ADPCMDecoder.h:2761
Definition ADPCMDecoder.h:2766
Definition ADPCMDecoder.h:2665
Definition ADPCMDecoder.h:1885
Definition ADPCMDecoder.h:1783
Definition ADPCMDecoder.h:1927
Definition ADPCMDecoder.h:1389
Definition ADPCMDecoder.h:1954
Definition ADPCMDecoder.h:1335
Definition ADPCMDecoder.h:1369
Definition ADPCMDecoder.h:1436
Definition ADPCMDecoder.h:1313
Definition ADPCMDecoder.h:1170
Definition ADPCMDecoder.h:1142
Definition ADPCMDecoder.h:1735
Definition ADPCMDecoder.h:1766
Definition ADPCMDecoder.h:1242
Definition ADPCMDecoder.h:1279
Definition ADPCMDecoder.h:2467
Definition ADPCMDecoder.h:1456
Definition ADPCMDecoder.h:766
Definition ADPCMDecoder.h:1495
Definition ADPCMDecoder.h:2005
Definition ADPCMDecoder.h:1352
Definition ADPCMDecoder.h:815
Definition ADPCMDecoder.h:1531
Definition ADPCMDecoder.h:998
Definition ADPCMDecoder.h:1097
Definition ADPCMDecoder.h:2301
Definition ADPCMDecoder.h:2559
Definition ADPCMDecoder.h:2564
Definition ADPCMDecoder.h:2569
Definition ADPCMDecoder.h:2488
Definition ADPCMDecoder.h:2072
Definition ADPCMDecoder.h:2659
Definition ADPCMDecoder.h:2574
Definition ADPCMDecoder.h:1617
Definition ADPCMDecoder.h:1566
Definition ADPCMDecoder.h:2144
Definition ADPCMDecoder.h:2426
Definition adpcm.h:185
Definition adpcm.h:234
int vqa_version
Definition adpcm.h:236
int has_status
Definition adpcm.h:237
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
Definition adpcm.h:221
This structure provides the uncompressed PCM data.
Definition adpcm.h:130
int16_t ** extended_data
Definition adpcm.h:164
int nb_samples
Definition adpcm.h:148
uint8_t * data[AV_NUM_DATA_POINTERS]
Definition adpcm.h:143
This structure stores compressed data. It is typically exported by demuxers and then passed as input ...
Definition adpcm.h:122