Browse code

Merge remote-tracking branch 'qatar/master'

* qatar/master:
adpcm: split ADPCM encoders and decoders into separate files.
doc/avconv: fix typo.
rv34: check that subsequent slices have the same type as first one.
smacker demuxer: handle possible av_realloc() failure.
lavfi: add split filter from soc.
lavfi: add showinfo filter
libxavs: add private options corresponding to deprecated global options

Conflicts:
Changelog
libavcodec/adpcm.c
libavfilter/avfilter.h
libavfilter/vf_showinfo.c
libavfilter/vf_split.c
libavformat/smacker.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>

Michael Niedermayer authored on 2011/09/13 03:49:20
Showing 11 changed files
... ...
@@ -30,7 +30,7 @@ As a general rule, options are applied to the next specified
30 30
 file. Therefore, order is important, and you can have the same
31 31
 option on the command line multiple times. Each occurrence is
32 32
 then applied to the next input or output file.
33
-Exceptions from this rule are the global options (e.g. vebosity level),
33
+Exceptions from this rule are the global options (e.g. verbosity level),
34 34
 which should be specified first.
35 35
 
36 36
 @itemize
... ...
@@ -499,10 +499,10 @@ OBJS-$(CONFIG_PCM_U32LE_ENCODER)          += pcm.o
499 499
 OBJS-$(CONFIG_PCM_ZORK_DECODER)           += pcm.o
500 500
 OBJS-$(CONFIG_PCM_ZORK_ENCODER)           += pcm.o
501 501
 
502
-OBJS-$(CONFIG_ADPCM_4XM_DECODER)          += adpcm.o
502
+OBJS-$(CONFIG_ADPCM_4XM_DECODER)          += adpcm.o adpcm_data.o
503 503
 OBJS-$(CONFIG_ADPCM_ADX_DECODER)          += adxdec.o
504 504
 OBJS-$(CONFIG_ADPCM_ADX_ENCODER)          += adxenc.o
505
-OBJS-$(CONFIG_ADPCM_CT_DECODER)           += adpcm.o
505
+OBJS-$(CONFIG_ADPCM_CT_DECODER)           += adpcm.o adpcm_data.o
506 506
 OBJS-$(CONFIG_ADPCM_EA_DECODER)           += adpcm.o
507 507
 OBJS-$(CONFIG_ADPCM_EA_MAXIS_XA_DECODER)  += adpcm.o
508 508
 OBJS-$(CONFIG_ADPCM_EA_R1_DECODER)        += adpcm.o
... ...
@@ -513,29 +513,29 @@ OBJS-$(CONFIG_ADPCM_G722_DECODER)         += g722.o
513 513
 OBJS-$(CONFIG_ADPCM_G722_ENCODER)         += g722.o
514 514
 OBJS-$(CONFIG_ADPCM_G726_DECODER)         += g726.o
515 515
 OBJS-$(CONFIG_ADPCM_G726_ENCODER)         += g726.o
516
-OBJS-$(CONFIG_ADPCM_IMA_AMV_DECODER)      += adpcm.o
517
-OBJS-$(CONFIG_ADPCM_IMA_DK3_DECODER)      += adpcm.o
518
-OBJS-$(CONFIG_ADPCM_IMA_DK4_DECODER)      += adpcm.o
519
-OBJS-$(CONFIG_ADPCM_IMA_EA_EACS_DECODER)  += adpcm.o
520
-OBJS-$(CONFIG_ADPCM_IMA_EA_SEAD_DECODER)  += adpcm.o
521
-OBJS-$(CONFIG_ADPCM_IMA_ISS_DECODER)      += adpcm.o
522
-OBJS-$(CONFIG_ADPCM_IMA_QT_DECODER)       += adpcm.o
523
-OBJS-$(CONFIG_ADPCM_IMA_QT_ENCODER)       += adpcm.o
524
-OBJS-$(CONFIG_ADPCM_IMA_SMJPEG_DECODER)   += adpcm.o
525
-OBJS-$(CONFIG_ADPCM_IMA_WAV_DECODER)      += adpcm.o
526
-OBJS-$(CONFIG_ADPCM_IMA_WAV_ENCODER)      += adpcm.o
527
-OBJS-$(CONFIG_ADPCM_IMA_WS_DECODER)       += adpcm.o
528
-OBJS-$(CONFIG_ADPCM_MS_DECODER)           += adpcm.o
529
-OBJS-$(CONFIG_ADPCM_MS_ENCODER)           += adpcm.o
516
+OBJS-$(CONFIG_ADPCM_IMA_AMV_DECODER)      += adpcm.o adpcm_data.o
517
+OBJS-$(CONFIG_ADPCM_IMA_DK3_DECODER)      += adpcm.o adpcm_data.o
518
+OBJS-$(CONFIG_ADPCM_IMA_DK4_DECODER)      += adpcm.o adpcm_data.o
519
+OBJS-$(CONFIG_ADPCM_IMA_EA_EACS_DECODER)  += adpcm.o adpcm_data.o
520
+OBJS-$(CONFIG_ADPCM_IMA_EA_SEAD_DECODER)  += adpcm.o adpcm_data.o
521
+OBJS-$(CONFIG_ADPCM_IMA_ISS_DECODER)      += adpcm.o adpcm_data.o
522
+OBJS-$(CONFIG_ADPCM_IMA_QT_DECODER)       += adpcm.o adpcm_data.o
523
+OBJS-$(CONFIG_ADPCM_IMA_QT_ENCODER)       += adpcmenc.o adpcm_data.o
524
+OBJS-$(CONFIG_ADPCM_IMA_SMJPEG_DECODER)   += adpcm.o adpcm_data.o
525
+OBJS-$(CONFIG_ADPCM_IMA_WAV_DECODER)      += adpcm.o adpcm_data.o
526
+OBJS-$(CONFIG_ADPCM_IMA_WAV_ENCODER)      += adpcmenc.o adpcm_data.o
527
+OBJS-$(CONFIG_ADPCM_IMA_WS_DECODER)       += adpcm.o adpcm_data.o
528
+OBJS-$(CONFIG_ADPCM_MS_DECODER)           += adpcm.o adpcm_data.o
529
+OBJS-$(CONFIG_ADPCM_MS_ENCODER)           += adpcmenc.o adpcm_data.o
530 530
 OBJS-$(CONFIG_ADPCM_SBPRO_2_DECODER)      += adpcm.o
531 531
 OBJS-$(CONFIG_ADPCM_SBPRO_3_DECODER)      += adpcm.o
532 532
 OBJS-$(CONFIG_ADPCM_SBPRO_4_DECODER)      += adpcm.o
533
-OBJS-$(CONFIG_ADPCM_SWF_DECODER)          += adpcm.o
534
-OBJS-$(CONFIG_ADPCM_SWF_ENCODER)          += adpcm.o
533
+OBJS-$(CONFIG_ADPCM_SWF_DECODER)          += adpcm.o adpcm_data.o
534
+OBJS-$(CONFIG_ADPCM_SWF_ENCODER)          += adpcmenc.o adpcm_data.o
535 535
 OBJS-$(CONFIG_ADPCM_THP_DECODER)          += adpcm.o
536 536
 OBJS-$(CONFIG_ADPCM_XA_DECODER)           += adpcm.o
537
-OBJS-$(CONFIG_ADPCM_YAMAHA_DECODER)       += adpcm.o
538
-OBJS-$(CONFIG_ADPCM_YAMAHA_ENCODER)       += adpcm.o
537
+OBJS-$(CONFIG_ADPCM_YAMAHA_DECODER)       += adpcm.o adpcm_data.o
538
+OBJS-$(CONFIG_ADPCM_YAMAHA_ENCODER)       += adpcmenc.o adpcm_data.o
539 539
 
540 540
 # libavformat dependencies
541 541
 OBJS-$(CONFIG_ADTS_MUXER)              += mpeg4audio.o
... ...
@@ -1,5 +1,4 @@
1 1
 /*
2
- * ADPCM codecs
3 2
  * Copyright (c) 2001-2003 The ffmpeg Project
4 3
  *
5 4
  * This file is part of FFmpeg.
... ...
@@ -22,10 +21,12 @@
22 22
 #include "get_bits.h"
23 23
 #include "put_bits.h"
24 24
 #include "bytestream.h"
25
+#include "adpcm.h"
26
+#include "adpcm_data.h"
25 27
 
26 28
 /**
27 29
  * @file
28
- * ADPCM codecs.
30
+ * ADPCM decoders
29 31
  * First version by Francois Revol (revol@free.fr)
30 32
  * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
31 33
  *   by Mike Melanson (melanson@pcisys.net)
... ...
@@ -54,48 +55,6 @@
54 54
  * readstr http://www.geocities.co.jp/Playtown/2004/
55 55
  */
56 56
 
57
-#define BLKSIZE 1024
58
-
59
-/* step_table[] and index_table[] are from the ADPCM reference source */
60
-/* This is the index table: */
61
-static const int index_table[16] = {
62
-    -1, -1, -1, -1, 2, 4, 6, 8,
63
-    -1, -1, -1, -1, 2, 4, 6, 8,
64
-};
65
-
66
-/**
67
- * This is the step table. Note that many programs use slight deviations from
68
- * this table, but such deviations are negligible:
69
- */
70
-static const int step_table[89] = {
71
-    7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
72
-    19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
73
-    50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
74
-    130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
75
-    337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
76
-    876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
77
-    2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
78
-    5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
79
-    15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
80
-};
81
-
82
-/* These are for MS-ADPCM */
83
-/* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */
84
-static const int AdaptationTable[] = {
85
-        230, 230, 230, 230, 307, 409, 512, 614,
86
-        768, 614, 512, 409, 307, 230, 230, 230
87
-};
88
-
89
-/** Divided by 4 to fit in 8-bit integers */
90
-static const uint8_t AdaptCoeff1[] = {
91
-        64, 128, 0, 48, 60, 115, 98
92
-};
93
-
94
-/** Divided by 4 to fit in 8-bit integers */
95
-static const int8_t AdaptCoeff2[] = {
96
-        0, -64, 0, 16, 0, -52, -58
97
-};
98
-
99 57
 /* These are for CD-ROM XA ADPCM */
100 58
 static const int xa_adpcm_table[5][2] = {
101 59
    {   0,   0 },
... ...
@@ -118,668 +77,15 @@ static const int swf_index_tables[4][16] = {
118 118
     /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
119 119
 };
120 120
 
121
-static const int yamaha_indexscale[] = {
122
-    230, 230, 230, 230, 307, 409, 512, 614,
123
-    230, 230, 230, 230, 307, 409, 512, 614
124
-};
125
-
126
-static const int yamaha_difflookup[] = {
127
-    1, 3, 5, 7, 9, 11, 13, 15,
128
-    -1, -3, -5, -7, -9, -11, -13, -15
129
-};
130
-
131 121
 /* end of tables */
132 122
 
133
-typedef struct ADPCMChannelStatus {
134
-    int predictor;
135
-    short int step_index;
136
-    int step;
137
-    /* for encoding */
138
-    int prev_sample;
139
-
140
-    /* MS version */
141
-    short sample1;
142
-    short sample2;
143
-    int coeff1;
144
-    int coeff2;
145
-    int idelta;
146
-} ADPCMChannelStatus;
147
-
148
-typedef struct TrellisPath {
149
-    int nibble;
150
-    int prev;
151
-} TrellisPath;
152
-
153
-typedef struct TrellisNode {
154
-    uint32_t ssd;
155
-    int path;
156
-    int sample1;
157
-    int sample2;
158
-    int step;
159
-} TrellisNode;
160
-
161
-typedef struct ADPCMContext {
123
+typedef struct ADPCMDecodeContext {
162 124
     ADPCMChannelStatus status[6];
163
-    TrellisPath *paths;
164
-    TrellisNode *node_buf;
165
-    TrellisNode **nodep_buf;
166
-    uint8_t *trellis_hash;
167
-} ADPCMContext;
168
-
169
-#define FREEZE_INTERVAL 128
170
-
171
-/* XXX: implement encoding */
172
-
173
-#if CONFIG_ENCODERS
174
-static av_cold int adpcm_encode_init(AVCodecContext *avctx)
175
-{
176
-    ADPCMContext *s = avctx->priv_data;
177
-    uint8_t *extradata;
178
-    int i;
179
-    if (avctx->channels > 2)
180
-        return -1; /* only stereo or mono =) */
181
-
182
-    if(avctx->trellis && (unsigned)avctx->trellis > 16U){
183
-        av_log(avctx, AV_LOG_ERROR, "invalid trellis size\n");
184
-        return -1;
185
-    }
186
-
187
-    if (avctx->trellis) {
188
-        int frontier = 1 << avctx->trellis;
189
-        int max_paths =  frontier * FREEZE_INTERVAL;
190
-        FF_ALLOC_OR_GOTO(avctx, s->paths,     max_paths * sizeof(*s->paths), error);
191
-        FF_ALLOC_OR_GOTO(avctx, s->node_buf,  2 * frontier * sizeof(*s->node_buf), error);
192
-        FF_ALLOC_OR_GOTO(avctx, s->nodep_buf, 2 * frontier * sizeof(*s->nodep_buf), error);
193
-        FF_ALLOC_OR_GOTO(avctx, s->trellis_hash, 65536 * sizeof(*s->trellis_hash), error);
194
-    }
195
-
196
-    switch(avctx->codec->id) {
197
-    case CODEC_ID_ADPCM_IMA_WAV:
198
-        avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
199
-                                                             /* and we have 4 bytes per channel overhead */
200
-        avctx->block_align = BLKSIZE;
201
-        avctx->bits_per_coded_sample = 4;
202
-        /* seems frame_size isn't taken into account... have to buffer the samples :-( */
203
-        break;
204
-    case CODEC_ID_ADPCM_IMA_QT:
205
-        avctx->frame_size = 64;
206
-        avctx->block_align = 34 * avctx->channels;
207
-        break;
208
-    case CODEC_ID_ADPCM_MS:
209
-        avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2; /* each 16 bits sample gives one nibble */
210
-                                                             /* and we have 7 bytes per channel overhead */
211
-        avctx->block_align = BLKSIZE;
212
-        avctx->bits_per_coded_sample = 4;
213
-        avctx->extradata_size = 32;
214
-        extradata = avctx->extradata = av_malloc(avctx->extradata_size);
215
-        if (!extradata)
216
-            return AVERROR(ENOMEM);
217
-        bytestream_put_le16(&extradata, avctx->frame_size);
218
-        bytestream_put_le16(&extradata, 7); /* wNumCoef */
219
-        for (i = 0; i < 7; i++) {
220
-            bytestream_put_le16(&extradata, AdaptCoeff1[i] * 4);
221
-            bytestream_put_le16(&extradata, AdaptCoeff2[i] * 4);
222
-        }
223
-        break;
224
-    case CODEC_ID_ADPCM_YAMAHA:
225
-        avctx->frame_size = BLKSIZE * avctx->channels;
226
-        avctx->block_align = BLKSIZE;
227
-        break;
228
-    case CODEC_ID_ADPCM_SWF:
229
-        if (avctx->sample_rate != 11025 &&
230
-            avctx->sample_rate != 22050 &&
231
-            avctx->sample_rate != 44100) {
232
-            av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, 22050 or 44100\n");
233
-            goto error;
234
-        }
235
-        avctx->frame_size = 512 * (avctx->sample_rate / 11025);
236
-        break;
237
-    default:
238
-        goto error;
239
-    }
240
-
241
-    avctx->coded_frame= avcodec_alloc_frame();
242
-    avctx->coded_frame->key_frame= 1;
243
-
244
-    return 0;
245
-error:
246
-    av_freep(&s->paths);
247
-    av_freep(&s->node_buf);
248
-    av_freep(&s->nodep_buf);
249
-    av_freep(&s->trellis_hash);
250
-    return -1;
251
-}
252
-
253
-static av_cold int adpcm_encode_close(AVCodecContext *avctx)
254
-{
255
-    ADPCMContext *s = avctx->priv_data;
256
-    av_freep(&avctx->coded_frame);
257
-    av_freep(&s->paths);
258
-    av_freep(&s->node_buf);
259
-    av_freep(&s->nodep_buf);
260
-    av_freep(&s->trellis_hash);
261
-
262
-    return 0;
263
-}
264
-
265
-
266
-static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
267
-{
268
-    int delta = sample - c->prev_sample;
269
-    int nibble = FFMIN(7, abs(delta)*4/step_table[c->step_index]) + (delta<0)*8;
270
-    c->prev_sample += ((step_table[c->step_index] * yamaha_difflookup[nibble]) / 8);
271
-    c->prev_sample = av_clip_int16(c->prev_sample);
272
-    c->step_index = av_clip(c->step_index + index_table[nibble], 0, 88);
273
-    return nibble;
274
-}
275
-
276
-static inline unsigned char adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c, short sample)
277
-{
278
-    int delta = sample - c->prev_sample;
279
-    int diff, step = step_table[c->step_index];
280
-    int nibble = 8*(delta < 0);
281
-
282
-    delta= abs(delta);
283
-    diff = delta + (step >> 3);
284
-
285
-    if (delta >= step) {
286
-        nibble |= 4;
287
-        delta -= step;
288
-    }
289
-    step >>= 1;
290
-    if (delta >= step) {
291
-        nibble |= 2;
292
-        delta -= step;
293
-    }
294
-    step >>= 1;
295
-    if (delta >= step) {
296
-        nibble |= 1;
297
-        delta -= step;
298
-    }
299
-    diff -= delta;
300
-
301
-    if (nibble & 8)
302
-        c->prev_sample -= diff;
303
-    else
304
-        c->prev_sample += diff;
305
-
306
-    c->prev_sample = av_clip_int16(c->prev_sample);
307
-    c->step_index = av_clip(c->step_index + index_table[nibble], 0, 88);
308
-
309
-    return nibble;
310
-}
311
-
312
-static inline unsigned char adpcm_ms_compress_sample(ADPCMChannelStatus *c, short sample)
313
-{
314
-    int predictor, nibble, bias;
315
-
316
-    predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
317
-
318
-    nibble= sample - predictor;
319
-    if(nibble>=0) bias= c->idelta/2;
320
-    else          bias=-c->idelta/2;
321
-
322
-    nibble= (nibble + bias) / c->idelta;
323
-    nibble= av_clip(nibble, -8, 7)&0x0F;
324
-
325
-    predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
326
-
327
-    c->sample2 = c->sample1;
328
-    c->sample1 = av_clip_int16(predictor);
329
-
330
-    c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
331
-    if (c->idelta < 16) c->idelta = 16;
332
-
333
-    return nibble;
334
-}
335
-
336
-static inline unsigned char adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, short sample)
337
-{
338
-    int nibble, delta;
339
-
340
-    if(!c->step) {
341
-        c->predictor = 0;
342
-        c->step = 127;
343
-    }
344
-
345
-    delta = sample - c->predictor;
346
-
347
-    nibble = FFMIN(7, abs(delta)*4/c->step) + (delta<0)*8;
348
-
349
-    c->predictor += ((c->step * yamaha_difflookup[nibble]) / 8);
350
-    c->predictor = av_clip_int16(c->predictor);
351
-    c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
352
-    c->step = av_clip(c->step, 127, 24567);
353
-
354
-    return nibble;
355
-}
356
-
357
-static void adpcm_compress_trellis(AVCodecContext *avctx, const short *samples,
358
-                                   uint8_t *dst, ADPCMChannelStatus *c, int n)
359
-{
360
-    //FIXME 6% faster if frontier is a compile-time constant
361
-    ADPCMContext *s = avctx->priv_data;
362
-    const int frontier = 1 << avctx->trellis;
363
-    const int stride = avctx->channels;
364
-    const int version = avctx->codec->id;
365
-    TrellisPath *paths = s->paths, *p;
366
-    TrellisNode *node_buf = s->node_buf;
367
-    TrellisNode **nodep_buf = s->nodep_buf;
368
-    TrellisNode **nodes = nodep_buf; // nodes[] is always sorted by .ssd
369
-    TrellisNode **nodes_next = nodep_buf + frontier;
370
-    int pathn = 0, froze = -1, i, j, k, generation = 0;
371
-    uint8_t *hash = s->trellis_hash;
372
-    memset(hash, 0xff, 65536 * sizeof(*hash));
373
-
374
-    memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
375
-    nodes[0] = node_buf + frontier;
376
-    nodes[0]->ssd = 0;
377
-    nodes[0]->path = 0;
378
-    nodes[0]->step = c->step_index;
379
-    nodes[0]->sample1 = c->sample1;
380
-    nodes[0]->sample2 = c->sample2;
381
-    if((version == CODEC_ID_ADPCM_IMA_WAV) || (version == CODEC_ID_ADPCM_IMA_QT) || (version == CODEC_ID_ADPCM_SWF))
382
-        nodes[0]->sample1 = c->prev_sample;
383
-    if(version == CODEC_ID_ADPCM_MS)
384
-        nodes[0]->step = c->idelta;
385
-    if(version == CODEC_ID_ADPCM_YAMAHA) {
386
-        if(c->step == 0) {
387
-            nodes[0]->step = 127;
388
-            nodes[0]->sample1 = 0;
389
-        } else {
390
-            nodes[0]->step = c->step;
391
-            nodes[0]->sample1 = c->predictor;
392
-        }
393
-    }
394
-
395
-    for(i=0; i<n; i++) {
396
-        TrellisNode *t = node_buf + frontier*(i&1);
397
-        TrellisNode **u;
398
-        int sample = samples[i*stride];
399
-        int heap_pos = 0;
400
-        memset(nodes_next, 0, frontier*sizeof(TrellisNode*));
401
-        for(j=0; j<frontier && nodes[j]; j++) {
402
-            // higher j have higher ssd already, so they're likely to yield a suboptimal next sample too
403
-            const int range = (j < frontier/2) ? 1 : 0;
404
-            const int step = nodes[j]->step;
405
-            int nidx;
406
-            if(version == CODEC_ID_ADPCM_MS) {
407
-                const int predictor = ((nodes[j]->sample1 * c->coeff1) + (nodes[j]->sample2 * c->coeff2)) / 64;
408
-                const int div = (sample - predictor) / step;
409
-                const int nmin = av_clip(div-range, -8, 6);
410
-                const int nmax = av_clip(div+range, -7, 7);
411
-                for(nidx=nmin; nidx<=nmax; nidx++) {
412
-                    const int nibble = nidx & 0xf;
413
-                    int dec_sample = predictor + nidx * step;
414
-#define STORE_NODE(NAME, STEP_INDEX)\
415
-                    int d;\
416
-                    uint32_t ssd;\
417
-                    int pos;\
418
-                    TrellisNode *u;\
419
-                    uint8_t *h;\
420
-                    dec_sample = av_clip_int16(dec_sample);\
421
-                    d = sample - dec_sample;\
422
-                    ssd = nodes[j]->ssd + d*d;\
423
-                    /* Check for wraparound, skip such samples completely. \
424
-                     * Note, changing ssd to a 64 bit variable would be \
425
-                     * simpler, avoiding this check, but it's slower on \
426
-                     * x86 32 bit at the moment. */\
427
-                    if (ssd < nodes[j]->ssd)\
428
-                        goto next_##NAME;\
429
-                    /* Collapse any two states with the same previous sample value. \
430
-                     * One could also distinguish states by step and by 2nd to last
431
-                     * sample, but the effects of that are negligible.
432
-                     * Since nodes in the previous generation are iterated
433
-                     * through a heap, they're roughly ordered from better to
434
-                     * worse, but not strictly ordered. Therefore, an earlier
435
-                     * node with the same sample value is better in most cases
436
-                     * (and thus the current is skipped), but not strictly
437
-                     * in all cases. Only skipping samples where ssd >=
438
-                     * ssd of the earlier node with the same sample gives
439
-                     * slightly worse quality, though, for some reason. */ \
440
-                    h = &hash[(uint16_t) dec_sample];\
441
-                    if (*h == generation)\
442
-                        goto next_##NAME;\
443
-                    if (heap_pos < frontier) {\
444
-                        pos = heap_pos++;\
445
-                    } else {\
446
-                        /* Try to replace one of the leaf nodes with the new \
447
-                         * one, but try a different slot each time. */\
448
-                        pos = (frontier >> 1) + (heap_pos & ((frontier >> 1) - 1));\
449
-                        if (ssd > nodes_next[pos]->ssd)\
450
-                            goto next_##NAME;\
451
-                        heap_pos++;\
452
-                    }\
453
-                    *h = generation;\
454
-                    u = nodes_next[pos];\
455
-                    if(!u) {\
456
-                        assert(pathn < FREEZE_INTERVAL<<avctx->trellis);\
457
-                        u = t++;\
458
-                        nodes_next[pos] = u;\
459
-                        u->path = pathn++;\
460
-                    }\
461
-                    u->ssd = ssd;\
462
-                    u->step = STEP_INDEX;\
463
-                    u->sample2 = nodes[j]->sample1;\
464
-                    u->sample1 = dec_sample;\
465
-                    paths[u->path].nibble = nibble;\
466
-                    paths[u->path].prev = nodes[j]->path;\
467
-                    /* Sift the newly inserted node up in the heap to \
468
-                     * restore the heap property. */\
469
-                    while (pos > 0) {\
470
-                        int parent = (pos - 1) >> 1;\
471
-                        if (nodes_next[parent]->ssd <= ssd)\
472
-                            break;\
473
-                        FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
474
-                        pos = parent;\
475
-                    }\
476
-                    next_##NAME:;
477
-                    STORE_NODE(ms, FFMAX(16, (AdaptationTable[nibble] * step) >> 8));
478
-                }
479
-            } else if((version == CODEC_ID_ADPCM_IMA_WAV)|| (version == CODEC_ID_ADPCM_IMA_QT)|| (version == CODEC_ID_ADPCM_SWF)) {
480
-#define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
481
-                const int predictor = nodes[j]->sample1;\
482
-                const int div = (sample - predictor) * 4 / STEP_TABLE;\
483
-                int nmin = av_clip(div-range, -7, 6);\
484
-                int nmax = av_clip(div+range, -6, 7);\
485
-                if(nmin<=0) nmin--; /* distinguish -0 from +0 */\
486
-                if(nmax<0) nmax--;\
487
-                for(nidx=nmin; nidx<=nmax; nidx++) {\
488
-                    const int nibble = nidx<0 ? 7-nidx : nidx;\
489
-                    int dec_sample = predictor + (STEP_TABLE * yamaha_difflookup[nibble]) / 8;\
490
-                    STORE_NODE(NAME, STEP_INDEX);\
491
-                }
492
-                LOOP_NODES(ima, step_table[step], av_clip(step + index_table[nibble], 0, 88));
493
-            } else { //CODEC_ID_ADPCM_YAMAHA
494
-                LOOP_NODES(yamaha, step, av_clip((step * yamaha_indexscale[nibble]) >> 8, 127, 24567));
495
-#undef LOOP_NODES
496
-#undef STORE_NODE
497
-            }
498
-        }
499
-
500
-        u = nodes;
501
-        nodes = nodes_next;
502
-        nodes_next = u;
503
-
504
-        generation++;
505
-        if (generation == 255) {
506
-            memset(hash, 0xff, 65536 * sizeof(*hash));
507
-            generation = 0;
508
-        }
509
-
510
-        // prevent overflow
511
-        if(nodes[0]->ssd > (1<<28)) {
512
-            for(j=1; j<frontier && nodes[j]; j++)
513
-                nodes[j]->ssd -= nodes[0]->ssd;
514
-            nodes[0]->ssd = 0;
515
-        }
516
-
517
-        // merge old paths to save memory
518
-        if(i == froze + FREEZE_INTERVAL) {
519
-            p = &paths[nodes[0]->path];
520
-            for(k=i; k>froze; k--) {
521
-                dst[k] = p->nibble;
522
-                p = &paths[p->prev];
523
-            }
524
-            froze = i;
525
-            pathn = 0;
526
-            // other nodes might use paths that don't coincide with the frozen one.
527
-            // checking which nodes do so is too slow, so just kill them all.
528
-            // this also slightly improves quality, but I don't know why.
529
-            memset(nodes+1, 0, (frontier-1)*sizeof(TrellisNode*));
530
-        }
531
-    }
532
-
533
-    p = &paths[nodes[0]->path];
534
-    for(i=n-1; i>froze; i--) {
535
-        dst[i] = p->nibble;
536
-        p = &paths[p->prev];
537
-    }
538
-
539
-    c->predictor = nodes[0]->sample1;
540
-    c->sample1 = nodes[0]->sample1;
541
-    c->sample2 = nodes[0]->sample2;
542
-    c->step_index = nodes[0]->step;
543
-    c->step = nodes[0]->step;
544
-    c->idelta = nodes[0]->step;
545
-}
546
-
547
-static int adpcm_encode_frame(AVCodecContext *avctx,
548
-                            unsigned char *frame, int buf_size, void *data)
549
-{
550
-    int n, i, st;
551
-    short *samples;
552
-    unsigned char *dst;
553
-    ADPCMContext *c = avctx->priv_data;
554
-    uint8_t *buf;
555
-
556
-    dst = frame;
557
-    samples = (short *)data;
558
-    st= avctx->channels == 2;
559
-/*    n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
560
-
561
-    switch(avctx->codec->id) {
562
-    case CODEC_ID_ADPCM_IMA_WAV:
563
-        n = avctx->frame_size / 8;
564
-            c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
565
-/*            c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
566
-            bytestream_put_le16(&dst, c->status[0].prev_sample);
567
-            *dst++ = (unsigned char)c->status[0].step_index;
568
-            *dst++ = 0; /* unknown */
569
-            samples++;
570
-            if (avctx->channels == 2) {
571
-                c->status[1].prev_sample = (signed short)samples[0];
572
-/*                c->status[1].step_index = 0; */
573
-                bytestream_put_le16(&dst, c->status[1].prev_sample);
574
-                *dst++ = (unsigned char)c->status[1].step_index;
575
-                *dst++ = 0;
576
-                samples++;
577
-            }
578
-
579
-            /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
580
-            if(avctx->trellis > 0) {
581
-                FF_ALLOC_OR_GOTO(avctx, buf, 2*n*8, error);
582
-                adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n*8);
583
-                if(avctx->channels == 2)
584
-                    adpcm_compress_trellis(avctx, samples+1, buf + n*8, &c->status[1], n*8);
585
-                for(i=0; i<n; i++) {
586
-                    *dst++ = buf[8*i+0] | (buf[8*i+1] << 4);
587
-                    *dst++ = buf[8*i+2] | (buf[8*i+3] << 4);
588
-                    *dst++ = buf[8*i+4] | (buf[8*i+5] << 4);
589
-                    *dst++ = buf[8*i+6] | (buf[8*i+7] << 4);
590
-                    if (avctx->channels == 2) {
591
-                        uint8_t *buf1 = buf + n*8;
592
-                        *dst++ = buf1[8*i+0] | (buf1[8*i+1] << 4);
593
-                        *dst++ = buf1[8*i+2] | (buf1[8*i+3] << 4);
594
-                        *dst++ = buf1[8*i+4] | (buf1[8*i+5] << 4);
595
-                        *dst++ = buf1[8*i+6] | (buf1[8*i+7] << 4);
596
-                    }
597
-                }
598
-                av_free(buf);
599
-            } else
600
-            for (; n>0; n--) {
601
-                *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]);
602
-                *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4;
603
-                dst++;
604
-                *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]);
605
-                *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4;
606
-                dst++;
607
-                *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]);
608
-                *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4;
609
-                dst++;
610
-                *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]);
611
-                *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4;
612
-                dst++;
613
-                /* right channel */
614
-                if (avctx->channels == 2) {
615
-                    *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]);
616
-                    *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4;
617
-                    dst++;
618
-                    *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
619
-                    *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
620
-                    dst++;
621
-                    *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
622
-                    *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
623
-                    dst++;
624
-                    *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
625
-                    *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
626
-                    dst++;
627
-                }
628
-                samples += 8 * avctx->channels;
629
-            }
630
-        break;
631
-    case CODEC_ID_ADPCM_IMA_QT:
632
-    {
633
-        int ch, i;
634
-        PutBitContext pb;
635
-        init_put_bits(&pb, dst, buf_size*8);
636
-
637
-        for(ch=0; ch<avctx->channels; ch++){
638
-            put_bits(&pb, 9, (c->status[ch].prev_sample + 0x10000) >> 7);
639
-            put_bits(&pb, 7, c->status[ch].step_index);
640
-            if(avctx->trellis > 0) {
641
-                uint8_t buf[64];
642
-                adpcm_compress_trellis(avctx, samples+ch, buf, &c->status[ch], 64);
643
-                for(i=0; i<64; i++)
644
-                    put_bits(&pb, 4, buf[i^1]);
645
-            } else {
646
-                for (i=0; i<64; i+=2){
647
-                    int t1, t2;
648
-                    t1 = adpcm_ima_qt_compress_sample(&c->status[ch], samples[avctx->channels*(i+0)+ch]);
649
-                    t2 = adpcm_ima_qt_compress_sample(&c->status[ch], samples[avctx->channels*(i+1)+ch]);
650
-                    put_bits(&pb, 4, t2);
651
-                    put_bits(&pb, 4, t1);
652
-                }
653
-            }
654
-        }
655
-
656
-        flush_put_bits(&pb);
657
-        dst += put_bits_count(&pb)>>3;
658
-        break;
659
-    }
660
-    case CODEC_ID_ADPCM_SWF:
661
-    {
662
-        int i;
663
-        PutBitContext pb;
664
-        init_put_bits(&pb, dst, buf_size*8);
665
-
666
-        n = avctx->frame_size-1;
667
-
668
-        //Store AdpcmCodeSize
669
-        put_bits(&pb, 2, 2);                //Set 4bits flash adpcm format
670
-
671
-        //Init the encoder state
672
-        for(i=0; i<avctx->channels; i++){
673
-            c->status[i].step_index = av_clip(c->status[i].step_index, 0, 63); // clip step so it fits 6 bits
674
-            put_sbits(&pb, 16, samples[i]);
675
-            put_bits(&pb, 6, c->status[i].step_index);
676
-            c->status[i].prev_sample = (signed short)samples[i];
677
-        }
678
-
679
-        if(avctx->trellis > 0) {
680
-            FF_ALLOC_OR_GOTO(avctx, buf, 2*n, error);
681
-            adpcm_compress_trellis(avctx, samples+2, buf, &c->status[0], n);
682
-            if (avctx->channels == 2)
683
-                adpcm_compress_trellis(avctx, samples+3, buf+n, &c->status[1], n);
684
-            for(i=0; i<n; i++) {
685
-                put_bits(&pb, 4, buf[i]);
686
-                if (avctx->channels == 2)
687
-                    put_bits(&pb, 4, buf[n+i]);
688
-            }
689
-            av_free(buf);
690
-        } else {
691
-            for (i=1; i<avctx->frame_size; i++) {
692
-                put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels*i]));
693
-                if (avctx->channels == 2)
694
-                    put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1], samples[2*i+1]));
695
-            }
696
-        }
697
-        flush_put_bits(&pb);
698
-        dst += put_bits_count(&pb)>>3;
699
-        break;
700
-    }
701
-    case CODEC_ID_ADPCM_MS:
702
-        for(i=0; i<avctx->channels; i++){
703
-            int predictor=0;
704
-
705
-            *dst++ = predictor;
706
-            c->status[i].coeff1 = AdaptCoeff1[predictor];
707
-            c->status[i].coeff2 = AdaptCoeff2[predictor];
708
-        }
709
-        for(i=0; i<avctx->channels; i++){
710
-            if (c->status[i].idelta < 16)
711
-                c->status[i].idelta = 16;
712
-
713
-            bytestream_put_le16(&dst, c->status[i].idelta);
714
-        }
715
-        for(i=0; i<avctx->channels; i++){
716
-            c->status[i].sample2= *samples++;
717
-        }
718
-        for(i=0; i<avctx->channels; i++){
719
-            c->status[i].sample1= *samples++;
720
-
721
-            bytestream_put_le16(&dst, c->status[i].sample1);
722
-        }
723
-        for(i=0; i<avctx->channels; i++)
724
-            bytestream_put_le16(&dst, c->status[i].sample2);
725
-
726
-        if(avctx->trellis > 0) {
727
-            int n = avctx->block_align - 7*avctx->channels;
728
-            FF_ALLOC_OR_GOTO(avctx, buf, 2*n, error);
729
-            if(avctx->channels == 1) {
730
-                adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
731
-                for(i=0; i<n; i+=2)
732
-                    *dst++ = (buf[i] << 4) | buf[i+1];
733
-            } else {
734
-                adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
735
-                adpcm_compress_trellis(avctx, samples+1, buf+n, &c->status[1], n);
736
-                for(i=0; i<n; i++)
737
-                    *dst++ = (buf[i] << 4) | buf[n+i];
738
-            }
739
-            av_free(buf);
740
-        } else
741
-        for(i=7*avctx->channels; i<avctx->block_align; i++) {
742
-            int nibble;
743
-            nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++)<<4;
744
-            nibble|= adpcm_ms_compress_sample(&c->status[st], *samples++);
745
-            *dst++ = nibble;
746
-        }
747
-        break;
748
-    case CODEC_ID_ADPCM_YAMAHA:
749
-        n = avctx->frame_size / 2;
750
-        if(avctx->trellis > 0) {
751
-            FF_ALLOC_OR_GOTO(avctx, buf, 2*n*2, error);
752
-            n *= 2;
753
-            if(avctx->channels == 1) {
754
-                adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
755
-                for(i=0; i<n; i+=2)
756
-                    *dst++ = buf[i] | (buf[i+1] << 4);
757
-            } else {
758
-                adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
759
-                adpcm_compress_trellis(avctx, samples+1, buf+n, &c->status[1], n);
760
-                for(i=0; i<n; i++)
761
-                    *dst++ = buf[i] | (buf[n+i] << 4);
762
-            }
763
-            av_free(buf);
764
-        } else
765
-            for (n *= avctx->channels; n>0; n--) {
766
-                int nibble;
767
-                nibble  = adpcm_yamaha_compress_sample(&c->status[ 0], *samples++);
768
-                nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
769
-                *dst++ = nibble;
770
-            }
771
-        break;
772
-    default:
773
-    error:
774
-        return -1;
775
-    }
776
-    return dst - frame;
777
-}
778
-#endif //CONFIG_ENCODERS
125
+} ADPCMDecodeContext;
779 126
 
780 127
 static av_cold int adpcm_decode_init(AVCodecContext * avctx)
781 128
 {
782
-    ADPCMContext *c = avctx->priv_data;
129
+    ADPCMDecodeContext *c = avctx->priv_data;
783 130
     unsigned int max_channels = 2;
784 131
 
785 132
     switch(avctx->codec->id) {
... ...
@@ -823,8 +129,8 @@ static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble,
823 823
     int predictor;
824 824
     int sign, delta, diff, step;
825 825
 
826
-    step = step_table[c->step_index];
827
-    step_index = c->step_index + index_table[(unsigned)nibble];
826
+    step = ff_adpcm_step_table[c->step_index];
827
+    step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];
828 828
     if (step_index < 0) step_index = 0;
829 829
     else if (step_index > 88) step_index = 88;
830 830
 
... ...
@@ -850,8 +156,8 @@ static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble,
850 850
     int predictor;
851 851
     int diff, step;
852 852
 
853
-    step = step_table[c->step_index];
854
-    step_index = c->step_index + index_table[nibble];
853
+    step = ff_adpcm_step_table[c->step_index];
854
+    step_index = c->step_index + ff_adpcm_index_table[nibble];
855 855
     step_index = av_clip(step_index, 0, 88);
856 856
 
857 857
     diff = step >> 3;
... ...
@@ -879,7 +185,7 @@ static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
879 879
 
880 880
     c->sample2 = c->sample1;
881 881
     c->sample1 = av_clip_int16(predictor);
882
-    c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
882
+    c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
883 883
     if (c->idelta < 16) c->idelta = 16;
884 884
 
885 885
     return c->sample1;
... ...
@@ -900,7 +206,7 @@ static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
900 900
     c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
901 901
     c->predictor = av_clip_int16(c->predictor);
902 902
     /* calculate new step and clamp it to range 511..32767 */
903
-    new_step = (AdaptationTable[nibble & 7] * c->step) >> 8;
903
+    new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;
904 904
     c->step = av_clip(new_step, 511, 32767);
905 905
 
906 906
     return (short)c->predictor;
... ...
@@ -933,9 +239,9 @@ static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned c
933 933
         c->step = 127;
934 934
     }
935 935
 
936
-    c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;
936
+    c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;
937 937
     c->predictor = av_clip_int16(c->predictor);
938
-    c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
938
+    c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
939 939
     c->step = av_clip(c->step, 127, 24567);
940 940
     return c->predictor;
941 941
 }
... ...
@@ -1027,7 +333,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
1027 1027
 {
1028 1028
     const uint8_t *buf = avpkt->data;
1029 1029
     int buf_size = avpkt->size;
1030
-    ADPCMContext *c = avctx->priv_data;
1030
+    ADPCMDecodeContext *c = avctx->priv_data;
1031 1031
     ADPCMChannelStatus *cs;
1032 1032
     int n, m, channel, i;
1033 1033
     int block_predictor[2];
... ...
@@ -1183,10 +489,10 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
1183 1183
         if (st){
1184 1184
             c->status[1].idelta = (int16_t)bytestream_get_le16(&src);
1185 1185
         }
1186
-        c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
1187
-        c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
1188
-        c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
1189
-        c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
1186
+        c->status[0].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor[0]];
1187
+        c->status[0].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor[0]];
1188
+        c->status[1].coeff1 = ff_adpcm_AdaptCoeff1[block_predictor[1]];
1189
+        c->status[1].coeff2 = ff_adpcm_AdaptCoeff2[block_predictor[1]];
1190 1190
 
1191 1191
         c->status[0].sample1 = bytestream_get_le16(&src);
1192 1192
         if (st) c->status[1].sample1 = bytestream_get_le16(&src);
... ...
@@ -1655,7 +961,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
1655 1655
                 for (i = 0; i < avctx->channels; i++) {
1656 1656
                     // similar to IMA adpcm
1657 1657
                     int delta = get_bits(&gb, nb_bits);
1658
-                    int step = step_table[c->status[i].step_index];
1658
+                    int step = ff_adpcm_step_table[c->status[i].step_index];
1659 1659
                     long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
1660 1660
                     int k = k0;
1661 1661
 
... ...
@@ -1774,44 +1080,18 @@ static int adpcm_decode_frame(AVCodecContext *avctx,
1774 1774
 }
1775 1775
 
1776 1776
 
1777
-
1778
-#if CONFIG_ENCODERS
1779
-#define ADPCM_ENCODER(id,name,long_name_)       \
1780
-AVCodec ff_ ## name ## _encoder = {             \
1781
-    #name,                                      \
1782
-    AVMEDIA_TYPE_AUDIO,                         \
1783
-    id,                                         \
1784
-    sizeof(ADPCMContext),                       \
1785
-    adpcm_encode_init,                          \
1786
-    adpcm_encode_frame,                         \
1787
-    adpcm_encode_close,                         \
1788
-    NULL,                                       \
1789
-    .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE}, \
1790
-    .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1791
-}
1792
-#else
1793
-#define ADPCM_ENCODER(id,name,long_name_)
1794
-#endif
1795
-
1796
-#if CONFIG_DECODERS
1797 1777
 #define ADPCM_DECODER(id,name,long_name_)       \
1798 1778
 AVCodec ff_ ## name ## _decoder = {             \
1799 1779
     #name,                                      \
1800 1780
     AVMEDIA_TYPE_AUDIO,                         \
1801 1781
     id,                                         \
1802
-    sizeof(ADPCMContext),                       \
1782
+    sizeof(ADPCMDecodeContext),                 \
1803 1783
     adpcm_decode_init,                          \
1804 1784
     NULL,                                       \
1805 1785
     NULL,                                       \
1806 1786
     adpcm_decode_frame,                         \
1807 1787
     .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
1808 1788
 }
1809
-#else
1810
-#define ADPCM_DECODER(id,name,long_name_)
1811
-#endif
1812
-
1813
-#define ADPCM_CODEC(id,name,long_name_)         \
1814
-    ADPCM_ENCODER(id,name,long_name_); ADPCM_DECODER(id,name,long_name_)
1815 1789
 
1816 1790
 /* Note: Do not forget to add new entries to the Makefile as well. */
1817 1791
 ADPCM_DECODER(CODEC_ID_ADPCM_4XM, adpcm_4xm, "ADPCM 4X Movie");
... ...
@@ -1828,15 +1108,15 @@ ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
1828 1828
 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_EACS, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
1829 1829
 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_SEAD, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
1830 1830
 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_ISS, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
1831
-ADPCM_CODEC  (CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, "ADPCM IMA QuickTime");
1831
+ADPCM_DECODER(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, "ADPCM IMA QuickTime");
1832 1832
 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
1833
-ADPCM_CODEC  (CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, "ADPCM IMA WAV");
1833
+ADPCM_DECODER(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, "ADPCM IMA WAV");
1834 1834
 ADPCM_DECODER(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws, "ADPCM IMA Westwood");
1835
-ADPCM_CODEC  (CODEC_ID_ADPCM_MS, adpcm_ms, "ADPCM Microsoft");
1835
+ADPCM_DECODER(CODEC_ID_ADPCM_MS, adpcm_ms, "ADPCM Microsoft");
1836 1836
 ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
1837 1837
 ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
1838 1838
 ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
1839
-ADPCM_CODEC  (CODEC_ID_ADPCM_SWF, adpcm_swf, "ADPCM Shockwave Flash");
1839
+ADPCM_DECODER(CODEC_ID_ADPCM_SWF, adpcm_swf, "ADPCM Shockwave Flash");
1840 1840
 ADPCM_DECODER(CODEC_ID_ADPCM_THP, adpcm_thp, "ADPCM Nintendo Gamecube THP");
1841 1841
 ADPCM_DECODER(CODEC_ID_ADPCM_XA, adpcm_xa, "ADPCM CDROM XA");
1842
-ADPCM_CODEC  (CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, "ADPCM Yamaha");
1842
+ADPCM_DECODER(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, "ADPCM Yamaha");
1843 1843
new file mode 100644
... ...
@@ -0,0 +1,46 @@
0
+/*
1
+ * Copyright (c) 2001-2003 The ffmpeg Project
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+ * @file
22
+ * ADPCM encoder/decoder common header.
23
+ */
24
+
25
+#ifndef AVCODEC_ADPCM_H
26
+#define AVCODEC_ADPCM_H
27
+
28
+#define BLKSIZE 1024
29
+
30
+typedef struct ADPCMChannelStatus {
31
+    int predictor;
32
+    short int step_index;
33
+    int step;
34
+    /* for encoding */
35
+    int prev_sample;
36
+
37
+    /* MS version */
38
+    short sample1;
39
+    short sample2;
40
+    int coeff1;
41
+    int coeff2;
42
+    int idelta;
43
+} ADPCMChannelStatus;
44
+
45
+#endif /* AVCODEC_ADPCM_H */
0 46
new file mode 100644
... ...
@@ -0,0 +1,78 @@
0
+/*
1
+ * Copyright (c) 2001-2003 The ffmpeg Project
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+ * @file
22
+ * ADPCM tables
23
+ */
24
+
25
+#include <stdint.h>
26
+
27
+/* ff_adpcm_step_table[] and ff_adpcm_index_table[] are from the ADPCM
28
+   reference source */
29
+/* This is the index table: */
30
+const int8_t ff_adpcm_index_table[16] = {
31
+    -1, -1, -1, -1, 2, 4, 6, 8,
32
+    -1, -1, -1, -1, 2, 4, 6, 8,
33
+};
34
+
35
+/**
36
+ * This is the step table. Note that many programs use slight deviations from
37
+ * this table, but such deviations are negligible:
38
+ */
39
+const int16_t ff_adpcm_step_table[89] = {
40
+    7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
41
+    19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
42
+    50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
43
+    130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
44
+    337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
45
+    876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
46
+    2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
47
+    5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
48
+    15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
49
+};
50
+
51
+/* These are for MS-ADPCM */
52
+/* ff_adpcm_AdaptationTable[], ff_adpcm_AdaptCoeff1[], and
53
+   ff_adpcm_AdaptCoeff2[] are from libsndfile */
54
+const int16_t ff_adpcm_AdaptationTable[] = {
55
+        230, 230, 230, 230, 307, 409, 512, 614,
56
+        768, 614, 512, 409, 307, 230, 230, 230
57
+};
58
+
59
+/** Divided by 4 to fit in 8-bit integers */
60
+const uint8_t ff_adpcm_AdaptCoeff1[] = {
61
+        64, 128, 0, 48, 60, 115, 98
62
+};
63
+
64
+/** Divided by 4 to fit in 8-bit integers */
65
+const int8_t ff_adpcm_AdaptCoeff2[] = {
66
+        0, -64, 0, 16, 0, -52, -58
67
+};
68
+
69
+const int16_t ff_adpcm_yamaha_indexscale[] = {
70
+    230, 230, 230, 230, 307, 409, 512, 614,
71
+    230, 230, 230, 230, 307, 409, 512, 614
72
+};
73
+
74
+const int8_t ff_adpcm_yamaha_difflookup[] = {
75
+    1, 3, 5, 7, 9, 11, 13, 15,
76
+    -1, -3, -5, -7, -9, -11, -13, -15
77
+};
0 78
new file mode 100644
... ...
@@ -0,0 +1,37 @@
0
+/*
1
+ * Copyright (c) 2001-2003 The ffmpeg Project
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+ * @file
22
+ * ADPCM tables
23
+ */
24
+
25
+#ifndef AVCODEC_ADPCM_DATA_H
26
+#define AVCODEC_ADPCM_DATA_H
27
+
28
+extern const int8_t  ff_adpcm_index_table[16];
29
+extern const int16_t ff_adpcm_step_table[89];
30
+extern const int16_t ff_adpcm_AdaptationTable[];
31
+extern const uint8_t ff_adpcm_AdaptCoeff1[];
32
+extern const int8_t  ff_adpcm_AdaptCoeff2[];
33
+extern const int16_t ff_adpcm_yamaha_indexscale[];
34
+extern const int8_t  ff_adpcm_yamaha_difflookup[];
35
+
36
+#endif /* AVCODEC_ADPCM_DATA_H */
0 37
new file mode 100644
... ...
@@ -0,0 +1,691 @@
0
+/*
1
+ * Copyright (c) 2001-2003 The ffmpeg Project
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include "avcodec.h"
21
+#include "get_bits.h"
22
+#include "put_bits.h"
23
+#include "bytestream.h"
24
+#include "adpcm.h"
25
+#include "adpcm_data.h"
26
+
27
+/**
28
+ * @file
29
+ * ADPCM encoders
30
+ * First version by Francois Revol (revol@free.fr)
31
+ * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
32
+ *   by Mike Melanson (melanson@pcisys.net)
33
+ *
34
+ * Reference documents:
35
+ * http://www.pcisys.net/~melanson/codecs/simpleaudio.html
36
+ * http://www.geocities.com/SiliconValley/8682/aud3.txt
37
+ * http://openquicktime.sourceforge.net/plugins.htm
38
+ * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html
39
+ * http://www.cs.ucla.edu/~leec/mediabench/applications.html
40
+ * SoX source code http://home.sprynet.com/~cbagwell/sox.html
41
+ */
42
+
43
+typedef struct TrellisPath {
44
+    int nibble;
45
+    int prev;
46
+} TrellisPath;
47
+
48
+typedef struct TrellisNode {
49
+    uint32_t ssd;
50
+    int path;
51
+    int sample1;
52
+    int sample2;
53
+    int step;
54
+} TrellisNode;
55
+
56
+typedef struct ADPCMEncodeContext {
57
+    ADPCMChannelStatus status[6];
58
+    TrellisPath *paths;
59
+    TrellisNode *node_buf;
60
+    TrellisNode **nodep_buf;
61
+    uint8_t *trellis_hash;
62
+} ADPCMEncodeContext;
63
+
64
+#define FREEZE_INTERVAL 128
65
+
66
+static av_cold int adpcm_encode_init(AVCodecContext *avctx)
67
+{
68
+    ADPCMEncodeContext *s = avctx->priv_data;
69
+    uint8_t *extradata;
70
+    int i;
71
+    if (avctx->channels > 2)
72
+        return -1; /* only stereo or mono =) */
73
+
74
+    if(avctx->trellis && (unsigned)avctx->trellis > 16U){
75
+        av_log(avctx, AV_LOG_ERROR, "invalid trellis size\n");
76
+        return -1;
77
+    }
78
+
79
+    if (avctx->trellis) {
80
+        int frontier = 1 << avctx->trellis;
81
+        int max_paths =  frontier * FREEZE_INTERVAL;
82
+        FF_ALLOC_OR_GOTO(avctx, s->paths,     max_paths * sizeof(*s->paths), error);
83
+        FF_ALLOC_OR_GOTO(avctx, s->node_buf,  2 * frontier * sizeof(*s->node_buf), error);
84
+        FF_ALLOC_OR_GOTO(avctx, s->nodep_buf, 2 * frontier * sizeof(*s->nodep_buf), error);
85
+        FF_ALLOC_OR_GOTO(avctx, s->trellis_hash, 65536 * sizeof(*s->trellis_hash), error);
86
+    }
87
+
88
+    switch(avctx->codec->id) {
89
+    case CODEC_ID_ADPCM_IMA_WAV:
90
+        avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
91
+                                                             /* and we have 4 bytes per channel overhead */
92
+        avctx->block_align = BLKSIZE;
93
+        avctx->bits_per_coded_sample = 4;
94
+        /* seems frame_size isn't taken into account... have to buffer the samples :-( */
95
+        break;
96
+    case CODEC_ID_ADPCM_IMA_QT:
97
+        avctx->frame_size = 64;
98
+        avctx->block_align = 34 * avctx->channels;
99
+        break;
100
+    case CODEC_ID_ADPCM_MS:
101
+        avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2; /* each 16 bits sample gives one nibble */
102
+                                                             /* and we have 7 bytes per channel overhead */
103
+        avctx->block_align = BLKSIZE;
104
+        avctx->bits_per_coded_sample = 4;
105
+        avctx->extradata_size = 32;
106
+        extradata = avctx->extradata = av_malloc(avctx->extradata_size);
107
+        if (!extradata)
108
+            return AVERROR(ENOMEM);
109
+        bytestream_put_le16(&extradata, avctx->frame_size);
110
+        bytestream_put_le16(&extradata, 7); /* wNumCoef */
111
+        for (i = 0; i < 7; i++) {
112
+            bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff1[i] * 4);
113
+            bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff2[i] * 4);
114
+        }
115
+        break;
116
+    case CODEC_ID_ADPCM_YAMAHA:
117
+        avctx->frame_size = BLKSIZE * avctx->channels;
118
+        avctx->block_align = BLKSIZE;
119
+        break;
120
+    case CODEC_ID_ADPCM_SWF:
121
+        if (avctx->sample_rate != 11025 &&
122
+            avctx->sample_rate != 22050 &&
123
+            avctx->sample_rate != 44100) {
124
+            av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, 22050 or 44100\n");
125
+            goto error;
126
+        }
127
+        avctx->frame_size = 512 * (avctx->sample_rate / 11025);
128
+        break;
129
+    default:
130
+        goto error;
131
+    }
132
+
133
+    avctx->coded_frame= avcodec_alloc_frame();
134
+    avctx->coded_frame->key_frame= 1;
135
+
136
+    return 0;
137
+error:
138
+    av_freep(&s->paths);
139
+    av_freep(&s->node_buf);
140
+    av_freep(&s->nodep_buf);
141
+    av_freep(&s->trellis_hash);
142
+    return -1;
143
+}
144
+
145
+static av_cold int adpcm_encode_close(AVCodecContext *avctx)
146
+{
147
+    ADPCMEncodeContext *s = avctx->priv_data;
148
+    av_freep(&avctx->coded_frame);
149
+    av_freep(&s->paths);
150
+    av_freep(&s->node_buf);
151
+    av_freep(&s->nodep_buf);
152
+    av_freep(&s->trellis_hash);
153
+
154
+    return 0;
155
+}
156
+
157
+
158
+static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
159
+{
160
+    int delta = sample - c->prev_sample;
161
+    int nibble = FFMIN(7, abs(delta)*4/ff_adpcm_step_table[c->step_index]) + (delta<0)*8;
162
+    c->prev_sample += ((ff_adpcm_step_table[c->step_index] * ff_adpcm_yamaha_difflookup[nibble]) / 8);
163
+    c->prev_sample = av_clip_int16(c->prev_sample);
164
+    c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
165
+    return nibble;
166
+}
167
+
168
+static inline unsigned char adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c, short sample)
169
+{
170
+    int delta = sample - c->prev_sample;
171
+    int diff, step = ff_adpcm_step_table[c->step_index];
172
+    int nibble = 8*(delta < 0);
173
+
174
+    delta= abs(delta);
175
+    diff = delta + (step >> 3);
176
+
177
+    if (delta >= step) {
178
+        nibble |= 4;
179
+        delta -= step;
180
+    }
181
+    step >>= 1;
182
+    if (delta >= step) {
183
+        nibble |= 2;
184
+        delta -= step;
185
+    }
186
+    step >>= 1;
187
+    if (delta >= step) {
188
+        nibble |= 1;
189
+        delta -= step;
190
+    }
191
+    diff -= delta;
192
+
193
+    if (nibble & 8)
194
+        c->prev_sample -= diff;
195
+    else
196
+        c->prev_sample += diff;
197
+
198
+    c->prev_sample = av_clip_int16(c->prev_sample);
199
+    c->step_index = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
200
+
201
+    return nibble;
202
+}
203
+
204
+static inline unsigned char adpcm_ms_compress_sample(ADPCMChannelStatus *c, short sample)
205
+{
206
+    int predictor, nibble, bias;
207
+
208
+    predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
209
+
210
+    nibble= sample - predictor;
211
+    if(nibble>=0) bias= c->idelta/2;
212
+    else          bias=-c->idelta/2;
213
+
214
+    nibble= (nibble + bias) / c->idelta;
215
+    nibble= av_clip(nibble, -8, 7)&0x0F;
216
+
217
+    predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
218
+
219
+    c->sample2 = c->sample1;
220
+    c->sample1 = av_clip_int16(predictor);
221
+
222
+    c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;
223
+    if (c->idelta < 16) c->idelta = 16;
224
+
225
+    return nibble;
226
+}
227
+
228
+static inline unsigned char adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, short sample)
229
+{
230
+    int nibble, delta;
231
+
232
+    if(!c->step) {
233
+        c->predictor = 0;
234
+        c->step = 127;
235
+    }
236
+
237
+    delta = sample - c->predictor;
238
+
239
+    nibble = FFMIN(7, abs(delta)*4/c->step) + (delta<0)*8;
240
+
241
+    c->predictor += ((c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8);
242
+    c->predictor = av_clip_int16(c->predictor);
243
+    c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;
244
+    c->step = av_clip(c->step, 127, 24567);
245
+
246
+    return nibble;
247
+}
248
+
249
+static void adpcm_compress_trellis(AVCodecContext *avctx, const short *samples,
250
+                                   uint8_t *dst, ADPCMChannelStatus *c, int n)
251
+{
252
+    //FIXME 6% faster if frontier is a compile-time constant
253
+    ADPCMEncodeContext *s = avctx->priv_data;
254
+    const int frontier = 1 << avctx->trellis;
255
+    const int stride = avctx->channels;
256
+    const int version = avctx->codec->id;
257
+    TrellisPath *paths = s->paths, *p;
258
+    TrellisNode *node_buf = s->node_buf;
259
+    TrellisNode **nodep_buf = s->nodep_buf;
260
+    TrellisNode **nodes = nodep_buf; // nodes[] is always sorted by .ssd
261
+    TrellisNode **nodes_next = nodep_buf + frontier;
262
+    int pathn = 0, froze = -1, i, j, k, generation = 0;
263
+    uint8_t *hash = s->trellis_hash;
264
+    memset(hash, 0xff, 65536 * sizeof(*hash));
265
+
266
+    memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
267
+    nodes[0] = node_buf + frontier;
268
+    nodes[0]->ssd = 0;
269
+    nodes[0]->path = 0;
270
+    nodes[0]->step = c->step_index;
271
+    nodes[0]->sample1 = c->sample1;
272
+    nodes[0]->sample2 = c->sample2;
273
+    if((version == CODEC_ID_ADPCM_IMA_WAV) || (version == CODEC_ID_ADPCM_IMA_QT) || (version == CODEC_ID_ADPCM_SWF))
274
+        nodes[0]->sample1 = c->prev_sample;
275
+    if(version == CODEC_ID_ADPCM_MS)
276
+        nodes[0]->step = c->idelta;
277
+    if(version == CODEC_ID_ADPCM_YAMAHA) {
278
+        if(c->step == 0) {
279
+            nodes[0]->step = 127;
280
+            nodes[0]->sample1 = 0;
281
+        } else {
282
+            nodes[0]->step = c->step;
283
+            nodes[0]->sample1 = c->predictor;
284
+        }
285
+    }
286
+
287
+    for(i=0; i<n; i++) {
288
+        TrellisNode *t = node_buf + frontier*(i&1);
289
+        TrellisNode **u;
290
+        int sample = samples[i*stride];
291
+        int heap_pos = 0;
292
+        memset(nodes_next, 0, frontier*sizeof(TrellisNode*));
293
+        for(j=0; j<frontier && nodes[j]; j++) {
294
+            // higher j have higher ssd already, so they're likely to yield a suboptimal next sample too
295
+            const int range = (j < frontier/2) ? 1 : 0;
296
+            const int step = nodes[j]->step;
297
+            int nidx;
298
+            if(version == CODEC_ID_ADPCM_MS) {
299
+                const int predictor = ((nodes[j]->sample1 * c->coeff1) + (nodes[j]->sample2 * c->coeff2)) / 64;
300
+                const int div = (sample - predictor) / step;
301
+                const int nmin = av_clip(div-range, -8, 6);
302
+                const int nmax = av_clip(div+range, -7, 7);
303
+                for(nidx=nmin; nidx<=nmax; nidx++) {
304
+                    const int nibble = nidx & 0xf;
305
+                    int dec_sample = predictor + nidx * step;
306
+#define STORE_NODE(NAME, STEP_INDEX)\
307
+                    int d;\
308
+                    uint32_t ssd;\
309
+                    int pos;\
310
+                    TrellisNode *u;\
311
+                    uint8_t *h;\
312
+                    dec_sample = av_clip_int16(dec_sample);\
313
+                    d = sample - dec_sample;\
314
+                    ssd = nodes[j]->ssd + d*d;\
315
+                    /* Check for wraparound, skip such samples completely. \
316
+                     * Note, changing ssd to a 64 bit variable would be \
317
+                     * simpler, avoiding this check, but it's slower on \
318
+                     * x86 32 bit at the moment. */\
319
+                    if (ssd < nodes[j]->ssd)\
320
+                        goto next_##NAME;\
321
+                    /* Collapse any two states with the same previous sample value. \
322
+                     * One could also distinguish states by step and by 2nd to last
323
+                     * sample, but the effects of that are negligible.
324
+                     * Since nodes in the previous generation are iterated
325
+                     * through a heap, they're roughly ordered from better to
326
+                     * worse, but not strictly ordered. Therefore, an earlier
327
+                     * node with the same sample value is better in most cases
328
+                     * (and thus the current is skipped), but not strictly
329
+                     * in all cases. Only skipping samples where ssd >=
330
+                     * ssd of the earlier node with the same sample gives
331
+                     * slightly worse quality, though, for some reason. */ \
332
+                    h = &hash[(uint16_t) dec_sample];\
333
+                    if (*h == generation)\
334
+                        goto next_##NAME;\
335
+                    if (heap_pos < frontier) {\
336
+                        pos = heap_pos++;\
337
+                    } else {\
338
+                        /* Try to replace one of the leaf nodes with the new \
339
+                         * one, but try a different slot each time. */\
340
+                        pos = (frontier >> 1) + (heap_pos & ((frontier >> 1) - 1));\
341
+                        if (ssd > nodes_next[pos]->ssd)\
342
+                            goto next_##NAME;\
343
+                        heap_pos++;\
344
+                    }\
345
+                    *h = generation;\
346
+                    u = nodes_next[pos];\
347
+                    if(!u) {\
348
+                        assert(pathn < FREEZE_INTERVAL<<avctx->trellis);\
349
+                        u = t++;\
350
+                        nodes_next[pos] = u;\
351
+                        u->path = pathn++;\
352
+                    }\
353
+                    u->ssd = ssd;\
354
+                    u->step = STEP_INDEX;\
355
+                    u->sample2 = nodes[j]->sample1;\
356
+                    u->sample1 = dec_sample;\
357
+                    paths[u->path].nibble = nibble;\
358
+                    paths[u->path].prev = nodes[j]->path;\
359
+                    /* Sift the newly inserted node up in the heap to \
360
+                     * restore the heap property. */\
361
+                    while (pos > 0) {\
362
+                        int parent = (pos - 1) >> 1;\
363
+                        if (nodes_next[parent]->ssd <= ssd)\
364
+                            break;\
365
+                        FFSWAP(TrellisNode*, nodes_next[parent], nodes_next[pos]);\
366
+                        pos = parent;\
367
+                    }\
368
+                    next_##NAME:;
369
+                    STORE_NODE(ms, FFMAX(16, (ff_adpcm_AdaptationTable[nibble] * step) >> 8));
370
+                }
371
+            } else if((version == CODEC_ID_ADPCM_IMA_WAV)|| (version == CODEC_ID_ADPCM_IMA_QT)|| (version == CODEC_ID_ADPCM_SWF)) {
372
+#define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
373
+                const int predictor = nodes[j]->sample1;\
374
+                const int div = (sample - predictor) * 4 / STEP_TABLE;\
375
+                int nmin = av_clip(div-range, -7, 6);\
376
+                int nmax = av_clip(div+range, -6, 7);\
377
+                if(nmin<=0) nmin--; /* distinguish -0 from +0 */\
378
+                if(nmax<0) nmax--;\
379
+                for(nidx=nmin; nidx<=nmax; nidx++) {\
380
+                    const int nibble = nidx<0 ? 7-nidx : nidx;\
381
+                    int dec_sample = predictor + (STEP_TABLE * ff_adpcm_yamaha_difflookup[nibble]) / 8;\
382
+                    STORE_NODE(NAME, STEP_INDEX);\
383
+                }
384
+                LOOP_NODES(ima, ff_adpcm_step_table[step], av_clip(step + ff_adpcm_index_table[nibble], 0, 88));
385
+            } else { //CODEC_ID_ADPCM_YAMAHA
386
+                LOOP_NODES(yamaha, step, av_clip((step * ff_adpcm_yamaha_indexscale[nibble]) >> 8, 127, 24567));
387
+#undef LOOP_NODES
388
+#undef STORE_NODE
389
+            }
390
+        }
391
+
392
+        u = nodes;
393
+        nodes = nodes_next;
394
+        nodes_next = u;
395
+
396
+        generation++;
397
+        if (generation == 255) {
398
+            memset(hash, 0xff, 65536 * sizeof(*hash));
399
+            generation = 0;
400
+        }
401
+
402
+        // prevent overflow
403
+        if(nodes[0]->ssd > (1<<28)) {
404
+            for(j=1; j<frontier && nodes[j]; j++)
405
+                nodes[j]->ssd -= nodes[0]->ssd;
406
+            nodes[0]->ssd = 0;
407
+        }
408
+
409
+        // merge old paths to save memory
410
+        if(i == froze + FREEZE_INTERVAL) {
411
+            p = &paths[nodes[0]->path];
412
+            for(k=i; k>froze; k--) {
413
+                dst[k] = p->nibble;
414
+                p = &paths[p->prev];
415
+            }
416
+            froze = i;
417
+            pathn = 0;
418
+            // other nodes might use paths that don't coincide with the frozen one.
419
+            // checking which nodes do so is too slow, so just kill them all.
420
+            // this also slightly improves quality, but I don't know why.
421
+            memset(nodes+1, 0, (frontier-1)*sizeof(TrellisNode*));
422
+        }
423
+    }
424
+
425
+    p = &paths[nodes[0]->path];
426
+    for(i=n-1; i>froze; i--) {
427
+        dst[i] = p->nibble;
428
+        p = &paths[p->prev];
429
+    }
430
+
431
+    c->predictor = nodes[0]->sample1;
432
+    c->sample1 = nodes[0]->sample1;
433
+    c->sample2 = nodes[0]->sample2;
434
+    c->step_index = nodes[0]->step;
435
+    c->step = nodes[0]->step;
436
+    c->idelta = nodes[0]->step;
437
+}
438
+
439
+static int adpcm_encode_frame(AVCodecContext *avctx,
440
+                            unsigned char *frame, int buf_size, void *data)
441
+{
442
+    int n, i, st;
443
+    short *samples;
444
+    unsigned char *dst;
445
+    ADPCMEncodeContext *c = avctx->priv_data;
446
+    uint8_t *buf;
447
+
448
+    dst = frame;
449
+    samples = (short *)data;
450
+    st= avctx->channels == 2;
451
+/*    n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
452
+
453
+    switch(avctx->codec->id) {
454
+    case CODEC_ID_ADPCM_IMA_WAV:
455
+        n = avctx->frame_size / 8;
456
+            c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
457
+/*            c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
458
+            bytestream_put_le16(&dst, c->status[0].prev_sample);
459
+            *dst++ = (unsigned char)c->status[0].step_index;
460
+            *dst++ = 0; /* unknown */
461
+            samples++;
462
+            if (avctx->channels == 2) {
463
+                c->status[1].prev_sample = (signed short)samples[0];
464
+/*                c->status[1].step_index = 0; */
465
+                bytestream_put_le16(&dst, c->status[1].prev_sample);
466
+                *dst++ = (unsigned char)c->status[1].step_index;
467
+                *dst++ = 0;
468
+                samples++;
469
+            }
470
+
471
+            /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
472
+            if(avctx->trellis > 0) {
473
+                FF_ALLOC_OR_GOTO(avctx, buf, 2*n*8, error);
474
+                adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n*8);
475
+                if(avctx->channels == 2)
476
+                    adpcm_compress_trellis(avctx, samples+1, buf + n*8, &c->status[1], n*8);
477
+                for(i=0; i<n; i++) {
478
+                    *dst++ = buf[8*i+0] | (buf[8*i+1] << 4);
479
+                    *dst++ = buf[8*i+2] | (buf[8*i+3] << 4);
480
+                    *dst++ = buf[8*i+4] | (buf[8*i+5] << 4);
481
+                    *dst++ = buf[8*i+6] | (buf[8*i+7] << 4);
482
+                    if (avctx->channels == 2) {
483
+                        uint8_t *buf1 = buf + n*8;
484
+                        *dst++ = buf1[8*i+0] | (buf1[8*i+1] << 4);
485
+                        *dst++ = buf1[8*i+2] | (buf1[8*i+3] << 4);
486
+                        *dst++ = buf1[8*i+4] | (buf1[8*i+5] << 4);
487
+                        *dst++ = buf1[8*i+6] | (buf1[8*i+7] << 4);
488
+                    }
489
+                }
490
+                av_free(buf);
491
+            } else
492
+            for (; n>0; n--) {
493
+                *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]);
494
+                *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4;
495
+                dst++;
496
+                *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]);
497
+                *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4;
498
+                dst++;
499
+                *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]);
500
+                *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4;
501
+                dst++;
502
+                *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]);
503
+                *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4;
504
+                dst++;
505
+                /* right channel */
506
+                if (avctx->channels == 2) {
507
+                    *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]);
508
+                    *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4;
509
+                    dst++;
510
+                    *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
511
+                    *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
512
+                    dst++;
513
+                    *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
514
+                    *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
515
+                    dst++;
516
+                    *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
517
+                    *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
518
+                    dst++;
519
+                }
520
+                samples += 8 * avctx->channels;
521
+            }
522
+        break;
523
+    case CODEC_ID_ADPCM_IMA_QT:
524
+    {
525
+        int ch, i;
526
+        PutBitContext pb;
527
+        init_put_bits(&pb, dst, buf_size*8);
528
+
529
+        for(ch=0; ch<avctx->channels; ch++){
530
+            put_bits(&pb, 9, (c->status[ch].prev_sample + 0x10000) >> 7);
531
+            put_bits(&pb, 7, c->status[ch].step_index);
532
+            if(avctx->trellis > 0) {
533
+                uint8_t buf[64];
534
+                adpcm_compress_trellis(avctx, samples+ch, buf, &c->status[ch], 64);
535
+                for(i=0; i<64; i++)
536
+                    put_bits(&pb, 4, buf[i^1]);
537
+            } else {
538
+                for (i=0; i<64; i+=2){
539
+                    int t1, t2;
540
+                    t1 = adpcm_ima_qt_compress_sample(&c->status[ch], samples[avctx->channels*(i+0)+ch]);
541
+                    t2 = adpcm_ima_qt_compress_sample(&c->status[ch], samples[avctx->channels*(i+1)+ch]);
542
+                    put_bits(&pb, 4, t2);
543
+                    put_bits(&pb, 4, t1);
544
+                }
545
+            }
546
+        }
547
+
548
+        flush_put_bits(&pb);
549
+        dst += put_bits_count(&pb)>>3;
550
+        break;
551
+    }
552
+    case CODEC_ID_ADPCM_SWF:
553
+    {
554
+        int i;
555
+        PutBitContext pb;
556
+        init_put_bits(&pb, dst, buf_size*8);
557
+
558
+        n = avctx->frame_size-1;
559
+
560
+        //Store AdpcmCodeSize
561
+        put_bits(&pb, 2, 2);                //Set 4bits flash adpcm format
562
+
563
+        //Init the encoder state
564
+        for(i=0; i<avctx->channels; i++){
565
+            c->status[i].step_index = av_clip(c->status[i].step_index, 0, 63); // clip step so it fits 6 bits
566
+            put_sbits(&pb, 16, samples[i]);
567
+            put_bits(&pb, 6, c->status[i].step_index);
568
+            c->status[i].prev_sample = (signed short)samples[i];
569
+        }
570
+
571
+        if(avctx->trellis > 0) {
572
+            FF_ALLOC_OR_GOTO(avctx, buf, 2*n, error);
573
+            adpcm_compress_trellis(avctx, samples+2, buf, &c->status[0], n);
574
+            if (avctx->channels == 2)
575
+                adpcm_compress_trellis(avctx, samples+3, buf+n, &c->status[1], n);
576
+            for(i=0; i<n; i++) {
577
+                put_bits(&pb, 4, buf[i]);
578
+                if (avctx->channels == 2)
579
+                    put_bits(&pb, 4, buf[n+i]);
580
+            }
581
+            av_free(buf);
582
+        } else {
583
+            for (i=1; i<avctx->frame_size; i++) {
584
+                put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels*i]));
585
+                if (avctx->channels == 2)
586
+                    put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1], samples[2*i+1]));
587
+            }
588
+        }
589
+        flush_put_bits(&pb);
590
+        dst += put_bits_count(&pb)>>3;
591
+        break;
592
+    }
593
+    case CODEC_ID_ADPCM_MS:
594
+        for(i=0; i<avctx->channels; i++){
595
+            int predictor=0;
596
+
597
+            *dst++ = predictor;
598
+            c->status[i].coeff1 = ff_adpcm_AdaptCoeff1[predictor];
599
+            c->status[i].coeff2 = ff_adpcm_AdaptCoeff2[predictor];
600
+        }
601
+        for(i=0; i<avctx->channels; i++){
602
+            if (c->status[i].idelta < 16)
603
+                c->status[i].idelta = 16;
604
+
605
+            bytestream_put_le16(&dst, c->status[i].idelta);
606
+        }
607
+        for(i=0; i<avctx->channels; i++){
608
+            c->status[i].sample2= *samples++;
609
+        }
610
+        for(i=0; i<avctx->channels; i++){
611
+            c->status[i].sample1= *samples++;
612
+
613
+            bytestream_put_le16(&dst, c->status[i].sample1);
614
+        }
615
+        for(i=0; i<avctx->channels; i++)
616
+            bytestream_put_le16(&dst, c->status[i].sample2);
617
+
618
+        if(avctx->trellis > 0) {
619
+            int n = avctx->block_align - 7*avctx->channels;
620
+            FF_ALLOC_OR_GOTO(avctx, buf, 2*n, error);
621
+            if(avctx->channels == 1) {
622
+                adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
623
+                for(i=0; i<n; i+=2)
624
+                    *dst++ = (buf[i] << 4) | buf[i+1];
625
+            } else {
626
+                adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
627
+                adpcm_compress_trellis(avctx, samples+1, buf+n, &c->status[1], n);
628
+                for(i=0; i<n; i++)
629
+                    *dst++ = (buf[i] << 4) | buf[n+i];
630
+            }
631
+            av_free(buf);
632
+        } else
633
+        for(i=7*avctx->channels; i<avctx->block_align; i++) {
634
+            int nibble;
635
+            nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++)<<4;
636
+            nibble|= adpcm_ms_compress_sample(&c->status[st], *samples++);
637
+            *dst++ = nibble;
638
+        }
639
+        break;
640
+    case CODEC_ID_ADPCM_YAMAHA:
641
+        n = avctx->frame_size / 2;
642
+        if(avctx->trellis > 0) {
643
+            FF_ALLOC_OR_GOTO(avctx, buf, 2*n*2, error);
644
+            n *= 2;
645
+            if(avctx->channels == 1) {
646
+                adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
647
+                for(i=0; i<n; i+=2)
648
+                    *dst++ = buf[i] | (buf[i+1] << 4);
649
+            } else {
650
+                adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
651
+                adpcm_compress_trellis(avctx, samples+1, buf+n, &c->status[1], n);
652
+                for(i=0; i<n; i++)
653
+                    *dst++ = buf[i] | (buf[n+i] << 4);
654
+            }
655
+            av_free(buf);
656
+        } else
657
+            for (n *= avctx->channels; n>0; n--) {
658
+                int nibble;
659
+                nibble  = adpcm_yamaha_compress_sample(&c->status[ 0], *samples++);
660
+                nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
661
+                *dst++ = nibble;
662
+            }
663
+        break;
664
+    default:
665
+    error:
666
+        return -1;
667
+    }
668
+    return dst - frame;
669
+}
670
+
671
+
672
+#define ADPCM_ENCODER(id,name,long_name_)       \
673
+AVCodec ff_ ## name ## _encoder = {             \
674
+    #name,                                      \
675
+    AVMEDIA_TYPE_AUDIO,                         \
676
+    id,                                         \
677
+    sizeof(ADPCMEncodeContext),                 \
678
+    adpcm_encode_init,                          \
679
+    adpcm_encode_frame,                         \
680
+    adpcm_encode_close,                         \
681
+    NULL,                                       \
682
+    .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE}, \
683
+    .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
684
+}
685
+
686
+ADPCM_ENCODER(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, "ADPCM IMA QuickTime");
687
+ADPCM_ENCODER(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, "ADPCM IMA WAV");
688
+ADPCM_ENCODER(CODEC_ID_ADPCM_MS, adpcm_ms, "ADPCM Microsoft");
689
+ADPCM_ENCODER(CODEC_ID_ADPCM_SWF, adpcm_swf, "ADPCM Shockwave Flash");
690
+ADPCM_ENCODER(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, "ADPCM Yamaha");
... ...
@@ -24,8 +24,11 @@
24 24
 #include <string.h>
25 25
 #include <math.h>
26 26
 #include <stdint.h>
27
+#include <float.h>
27 28
 #include <xavs.h>
28 29
 #include "avcodec.h"
30
+#include "internal.h"
31
+#include "libavutil/opt.h"
29 32
 
30 33
 #define END_OF_STREAM 0x001
31 34
 
... ...
@@ -41,6 +44,15 @@ typedef struct XavsContext {
41 41
     int             sei_size;
42 42
     AVFrame         out_pic;
43 43
     int             end_of_stream;
44
+    float crf;
45
+    int cqp;
46
+    int b_bias;
47
+    float cplxblur;
48
+    int direct_pred;
49
+    int aud;
50
+    int fast_pskip;
51
+    int mbtree;
52
+    int mixed_refs;
44 53
 } XavsContext;
45 54
 
46 55
 static void XAVS_log(void *p, int level, const char *fmt, va_list args)
... ...
@@ -181,13 +193,17 @@ static av_cold int XAVS_init(AVCodecContext *avctx)
181 181
     x4->params.pf_log               = XAVS_log;
182 182
     x4->params.p_log_private        = avctx;
183 183
     x4->params.i_keyint_max         = avctx->gop_size;
184
-    x4->params.rc.i_bitrate         = avctx->bit_rate       / 1000;
184
+    if (avctx->bit_rate) {
185
+        x4->params.rc.i_bitrate   = avctx->bit_rate / 1000;
186
+        x4->params.rc.i_rc_method = XAVS_RC_ABR;
187
+    }
185 188
     x4->params.rc.i_vbv_buffer_size = avctx->rc_buffer_size / 1000;
186 189
     x4->params.rc.i_vbv_max_bitrate = avctx->rc_max_rate    / 1000;
187 190
     x4->params.rc.b_stat_write      = avctx->flags & CODEC_FLAG_PASS1;
188 191
     if (avctx->flags & CODEC_FLAG_PASS2) {
189 192
         x4->params.rc.b_stat_read = 1;
190 193
     } else {
194
+#if FF_API_X264_GLOBAL_OPTS
191 195
         if (avctx->crf) {
192 196
             x4->params.rc.i_rc_method   = XAVS_RC_CRF;
193 197
             x4->params.rc.f_rf_constant = avctx->crf;
... ...
@@ -195,19 +211,63 @@ static av_cold int XAVS_init(AVCodecContext *avctx)
195 195
             x4->params.rc.i_rc_method   = XAVS_RC_CQP;
196 196
             x4->params.rc.i_qp_constant = avctx->cqp;
197 197
         }
198
+#endif
199
+
200
+        if (x4->crf >= 0) {
201
+            x4->params.rc.i_rc_method   = XAVS_RC_CRF;
202
+            x4->params.rc.f_rf_constant = x4->crf;
203
+        } else if (x4->cqp >= 0) {
204
+            x4->params.rc.i_rc_method   = XAVS_RC_CQP;
205
+            x4->params.rc.i_qp_constant = x4->cqp;
206
+        }
198 207
     }
199 208
 
200
-    /* if neither crf nor cqp modes are selected we have to enable the RC */
201
-    /* we do it this way because we cannot check if the bitrate has been set */
202
-    if (!(avctx->crf || (avctx->cqp > -1)))
203
-        x4->params.rc.i_rc_method = XAVS_RC_ABR;
209
+#if FF_API_X264_GLOBAL_OPTS
210
+    if (avctx->bframebias)
211
+        x4->params.i_bframe_bias              = avctx->bframebias;
212
+    if (avctx->deblockalpha)
213
+        x4->params.i_deblocking_filter_alphac0 = avctx->deblockalpha;
214
+    if (avctx->deblockbeta)
215
+        x4->params.i_deblocking_filter_beta    = avctx->deblockbeta;
216
+    if (avctx->complexityblur >= 0)
217
+        x4->params.rc.f_complexity_blur        = avctx->complexityblur;
218
+    if (avctx->directpred >= 0)
219
+        x4->params.analyse.i_direct_mv_pred    = avctx->directpred;
220
+    if (avctx->partitions) {
221
+        if (avctx->partitions & XAVS_PART_I8X8)
222
+            x4->params.analyse.inter |= XAVS_ANALYSE_I8x8;
223
+        if (avctx->partitions & XAVS_PART_P8X8)
224
+            x4->params.analyse.inter |= XAVS_ANALYSE_PSUB16x16;
225
+        if (avctx->partitions & XAVS_PART_B8X8)
226
+            x4->params.analyse.inter |= XAVS_ANALYSE_BSUB16x16;
227
+    }
228
+    x4->params.rc.b_mb_tree               = !!(avctx->flags2 & CODEC_FLAG2_MBTREE);
229
+    x4->params.b_aud          = avctx->flags2 & CODEC_FLAG2_AUD;
230
+    x4->params.analyse.b_mixed_references = avctx->flags2 & CODEC_FLAG2_MIXED_REFS;
231
+    x4->params.analyse.b_fast_pskip       = avctx->flags2 & CODEC_FLAG2_FASTPSKIP;
232
+    x4->params.analyse.b_weighted_bipred = avctx->flags2 & CODEC_FLAG2_WPRED;
233
+#endif
234
+
235
+    if (x4->aud >= 0)
236
+        x4->params.b_aud                      = x4->aud;
237
+    if (x4->mbtree >= 0)
238
+        x4->params.rc.b_mb_tree               = x4->mbtree;
239
+    if (x4->direct_pred >= 0)
240
+        x4->params.analyse.i_direct_mv_pred   = x4->direct_pred;
241
+    if (x4->fast_pskip >= 0)
242
+        x4->params.analyse.b_fast_pskip       = x4->fast_pskip;
243
+    if (x4->mixed_refs >= 0)
244
+        x4->params.analyse.b_mixed_references = x4->mixed_refs;
245
+    if (x4->b_bias != INT_MIN)
246
+        x4->params.i_bframe_bias              = x4->b_bias;
247
+    if (x4->cplxblur >= 0)
248
+        x4->params.rc.f_complexity_blur = x4->cplxblur;
204 249
 
205 250
     x4->params.i_bframe          = avctx->max_b_frames;
206 251
     /* cabac is not included in AVS JiZhun Profile */
207 252
     x4->params.b_cabac           = 0;
208 253
 
209 254
     x4->params.i_bframe_adaptive = avctx->b_frame_strategy;
210
-    x4->params.i_bframe_bias     = avctx->bframebias;
211 255
 
212 256
     avctx->has_b_frames          = !!avctx->max_b_frames;
213 257
 
... ...
@@ -220,8 +280,6 @@ static av_cold int XAVS_init(AVCodecContext *avctx)
220 220
     x4->params.i_scenecut_threshold        = avctx->scenechange_threshold;
221 221
 
222 222
    // x4->params.b_deblocking_filter       = avctx->flags & CODEC_FLAG_LOOP_FILTER;
223
-    x4->params.i_deblocking_filter_alphac0 = avctx->deblockalpha;
224
-    x4->params.i_deblocking_filter_beta    = avctx->deblockbeta;
225 223
 
226 224
     x4->params.rc.i_qp_min                 = avctx->qmin;
227 225
     x4->params.rc.i_qp_max                 = avctx->qmax;
... ...
@@ -229,7 +287,6 @@ static av_cold int XAVS_init(AVCodecContext *avctx)
229 229
 
230 230
     x4->params.rc.f_qcompress       = avctx->qcompress; /* 0.0 => cbr, 1.0 => constant qp */
231 231
     x4->params.rc.f_qblur           = avctx->qblur;     /* temporally blur quants */
232
-    x4->params.rc.f_complexity_blur = avctx->complexityblur;
233 232
 
234 233
     x4->params.i_frame_reference    = avctx->refs;
235 234
 
... ...
@@ -241,20 +298,6 @@ static av_cold int XAVS_init(AVCodecContext *avctx)
241 241
     x4->params.i_fps_num            = avctx->time_base.den;
242 242
     x4->params.i_fps_den            = avctx->time_base.num;
243 243
     x4->params.analyse.inter        = XAVS_ANALYSE_I8x8 |XAVS_ANALYSE_PSUB16x16| XAVS_ANALYSE_BSUB16x16;
244
-    if (avctx->partitions) {
245
-        if (avctx->partitions & XAVS_PART_I8X8)
246
-            x4->params.analyse.inter |= XAVS_ANALYSE_I8x8;
247
-
248
-        if (avctx->partitions & XAVS_PART_P8X8)
249
-            x4->params.analyse.inter |= XAVS_ANALYSE_PSUB16x16;
250
-
251
-        if (avctx->partitions & XAVS_PART_B8X8)
252
-            x4->params.analyse.inter |= XAVS_ANALYSE_BSUB16x16;
253
-    }
254
-
255
-    x4->params.analyse.i_direct_mv_pred  = avctx->directpred;
256
-
257
-    x4->params.analyse.b_weighted_bipred = avctx->flags2 & CODEC_FLAG2_WPRED;
258 244
 
259 245
     switch (avctx->me_method) {
260 246
          case  ME_EPZS:
... ...
@@ -279,11 +322,9 @@ static av_cold int XAVS_init(AVCodecContext *avctx)
279 279
     x4->params.analyse.i_me_range = avctx->me_range;
280 280
     x4->params.analyse.i_subpel_refine    = avctx->me_subpel_quality;
281 281
 
282
-    x4->params.analyse.b_mixed_references = avctx->flags2 & CODEC_FLAG2_MIXED_REFS;
283 282
     x4->params.analyse.b_chroma_me        = avctx->me_cmp & FF_CMP_CHROMA;
284 283
     /* AVS P2 only enables 8x8 transform */
285 284
     x4->params.analyse.b_transform_8x8    = 1; //avctx->flags2 & CODEC_FLAG2_8X8DCT;
286
-    x4->params.analyse.b_fast_pskip       = avctx->flags2 & CODEC_FLAG2_FASTPSKIP;
287 285
 
288 286
     x4->params.analyse.i_trellis          = avctx->trellis;
289 287
     x4->params.analyse.i_noise_reduction  = avctx->noise_reduction;
... ...
@@ -303,14 +344,12 @@ static av_cold int XAVS_init(AVCodecContext *avctx)
303 303
 
304 304
     /* TAG:do we have MB tree RC method */
305 305
     /* what is the RC method we are now using? Default NO */
306
-    x4->params.rc.b_mb_tree               = !!(avctx->flags2 & CODEC_FLAG2_MBTREE);
307 306
     x4->params.rc.f_ip_factor             = 1 / fabs(avctx->i_quant_factor);
308 307
     x4->params.rc.f_pb_factor             = avctx->b_quant_factor;
309 308
     x4->params.analyse.i_chroma_qp_offset = avctx->chromaoffset;
310 309
 
311 310
     x4->params.analyse.b_psnr = avctx->flags & CODEC_FLAG_PSNR;
312 311
     x4->params.i_log_level    = XAVS_LOG_DEBUG;
313
-    x4->params.b_aud          = avctx->flags2 & CODEC_FLAG2_AUD;
314 312
     x4->params.i_threads      = avctx->thread_count;
315 313
     x4->params.b_interlaced   = avctx->flags & CODEC_FLAG_INTERLACED_DCT;
316 314
 
... ...
@@ -336,6 +375,37 @@ static av_cold int XAVS_init(AVCodecContext *avctx)
336 336
     return 0;
337 337
 }
338 338
 
339
+#define OFFSET(x) offsetof(XavsContext, x)
340
+#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
341
+static const AVOption options[] = {
342
+    { "crf",           "Select the quality for constant quality mode",    OFFSET(crf),           FF_OPT_TYPE_FLOAT,  {-1 }, -1, FLT_MAX, VE },
343
+    { "qp",            "Constant quantization parameter rate control method",OFFSET(cqp),        FF_OPT_TYPE_INT,    {-1 }, -1, INT_MAX, VE },
344
+    { "b-bias",        "Influences how often B-frames are used",          OFFSET(b_bias),        FF_OPT_TYPE_INT,    {INT_MIN}, INT_MIN, INT_MAX, VE },
345
+    { "cplxblur",      "Reduce fluctuations in QP (before curve compression)", OFFSET(cplxblur), FF_OPT_TYPE_FLOAT,  {-1 }, -1, FLT_MAX, VE},
346
+    { "direct-pred",   "Direct MV prediction mode",                       OFFSET(direct_pred),   FF_OPT_TYPE_INT,    {-1 }, -1, INT_MAX, VE, "direct-pred" },
347
+    { "none",          NULL,      0,    FF_OPT_TYPE_CONST, { XAVS_DIRECT_PRED_NONE },     0, 0, VE, "direct-pred" },
348
+    { "spatial",       NULL,      0,    FF_OPT_TYPE_CONST, { XAVS_DIRECT_PRED_SPATIAL },  0, 0, VE, "direct-pred" },
349
+    { "temporal",      NULL,      0,    FF_OPT_TYPE_CONST, { XAVS_DIRECT_PRED_TEMPORAL }, 0, 0, VE, "direct-pred" },
350
+    { "auto",          NULL,      0,    FF_OPT_TYPE_CONST, { XAVS_DIRECT_PRED_AUTO },     0, 0, VE, "direct-pred" },
351
+    { "aud",           "Use access unit delimiters.",                     OFFSET(aud),           FF_OPT_TYPE_INT,    {-1 }, -1, 1, VE},
352
+    { "mbtree",        "Use macroblock tree ratecontrol.",                OFFSET(mbtree),        FF_OPT_TYPE_INT,    {-1 }, -1, 1, VE},
353
+    { "mixed-refs",    "One reference per partition, as opposed to one reference per macroblock", OFFSET(mixed_refs), FF_OPT_TYPE_INT, {-1}, -1, 1, VE },
354
+    { "fast-pskip",    NULL,                                              OFFSET(fast_pskip),    FF_OPT_TYPE_INT,    {-1 }, -1, 1, VE},
355
+    { NULL },
356
+};
357
+
358
+static const AVClass class = {
359
+    .class_name = "libxavs",
360
+    .item_name  = av_default_item_name,
361
+    .option     = options,
362
+    .version    = LIBAVUTIL_VERSION_INT,
363
+};
364
+
365
+static const AVCodecDefault xavs_defaults[] = {
366
+    { "b",                "0" },
367
+    { NULL },
368
+};
369
+
339 370
 AVCodec ff_libxavs_encoder = {
340 371
     .name           = "libxavs",
341 372
     .type           = AVMEDIA_TYPE_VIDEO,
... ...
@@ -347,5 +417,7 @@ AVCodec ff_libxavs_encoder = {
347 347
     .capabilities   = CODEC_CAP_DELAY,
348 348
     .pix_fmts       = (const enum PixelFormat[]) { PIX_FMT_YUV420P, PIX_FMT_NONE },
349 349
     .long_name      = NULL_IF_CONFIG_SMALL("libxavs - the Chinese Audio Video Standard Encoder"),
350
+    .priv_class     = &class,
351
+    .defaults       = xavs_defaults,
350 352
 };
351 353
 
... ...
@@ -1336,6 +1336,13 @@ static int rv34_decode_slice(RV34DecContext *r, int end, const uint8_t* buf, int
1336 1336
             }
1337 1337
         }
1338 1338
         s->mb_x = s->mb_y = 0;
1339
+    } else {
1340
+        int slice_type = r->si.type ? r->si.type : AV_PICTURE_TYPE_I;
1341
+
1342
+        if (slice_type != s->pict_type) {
1343
+            av_log(s->avctx, AV_LOG_ERROR, "Slice type mismatch\n");
1344
+            return AVERROR_INVALIDDATA;
1345
+        }
1339 1346
     }
1340 1347
 
1341 1348
     r->si.end = end;
... ...
@@ -19,7 +19,7 @@
19 19
 
20 20
 /**
21 21
  * @file
22
- * filter fow showing textual video frame information
22
+ * filter for showing textual video frame information
23 23
  */
24 24
 
25 25
 #include "libavutil/adler32.h"
... ...
@@ -286,15 +286,16 @@ static int smacker_read_packet(AVFormatContext *s, AVPacket *pkt)
286 286
         for(i = 0; i < 7; i++) {
287 287
             if(flags & 1) {
288 288
                 int size;
289
+                uint8_t *tmpbuf;
290
+
289 291
                 size = avio_rl32(s->pb) - 4;
290 292
                 frame_size -= size;
291 293
                 frame_size -= 4;
292 294
                 smk->curstream++;
293
-                smk->bufs[smk->curstream] = av_realloc(smk->bufs[smk->curstream], size);
294
-                if (!smk->bufs[smk->curstream]) {
295
-                    smk->buf_sizes[smk->curstream] = 0;
295
+                tmpbuf = av_realloc(smk->bufs[smk->curstream], size);
296
+                if (!tmpbuf)
296 297
                     return AVERROR(ENOMEM);
297
-                }
298
+                smk->bufs[smk->curstream] = tmpbuf;
298 299
                 smk->buf_sizes[smk->curstream] = size;
299 300
                 ret = avio_read(s->pb, smk->bufs[smk->curstream], size);
300 301
                 if(ret != size)