Revision d764e3ec

View differences:

libavcodec/adpcm.c
163 163
    TrellisPath *paths;
164 164
    TrellisNode *node_buf;
165 165
    TrellisNode **nodep_buf;
166
    uint8_t *trellis_hash;
166 167
} ADPCMContext;
167 168

  
168 169
#define FREEZE_INTERVAL 128
......
189 190
        FF_ALLOC_OR_GOTO(avctx, s->paths,     max_paths * sizeof(*s->paths), error);
190 191
        FF_ALLOC_OR_GOTO(avctx, s->node_buf,  2 * frontier * sizeof(*s->node_buf), error);
191 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);
192 194
    }
193 195

  
194 196
    switch(avctx->codec->id) {
......
242 244
    av_freep(&s->paths);
243 245
    av_freep(&s->node_buf);
244 246
    av_freep(&s->nodep_buf);
247
    av_freep(&s->trellis_hash);
245 248
    return -1;
246 249
}
247 250

  
......
252 255
    av_freep(&s->paths);
253 256
    av_freep(&s->node_buf);
254 257
    av_freep(&s->nodep_buf);
258
    av_freep(&s->trellis_hash);
255 259

  
256 260
    return 0;
257 261
}
......
325 329
    TrellisNode **nodep_buf = s->nodep_buf;
326 330
    TrellisNode **nodes = nodep_buf; // nodes[] is always sorted by .ssd
327 331
    TrellisNode **nodes_next = nodep_buf + frontier;
328
    int pathn = 0, froze = -1, i, j, k;
332
    int pathn = 0, froze = -1, i, j, k, generation = 0;
333
    uint8_t *hash = s->trellis_hash;
334
    memset(hash, 0xff, 65536 * sizeof(*hash));
329 335

  
330 336
    memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
331 337
    nodes[0] = node_buf + frontier;
......
372 378
                    uint32_t ssd;\
373 379
                    int pos;\
374 380
                    TrellisNode *u;\
381
                    uint8_t *h;\
375 382
                    dec_sample = av_clip_int16(dec_sample);\
376 383
                    d = sample - dec_sample;\
377 384
                    ssd = nodes[j]->ssd + d*d;\
378 385
                    /* Collapse any two states with the same previous sample value. \
379 386
                     * One could also distinguish states by step and by 2nd to last
380
                     * sample, but the effects of that are negligible. */\
381
                    for(k=0; k<frontier && nodes_next[k]; k++) {\
382
                        if(dec_sample == nodes_next[k]->sample1) {\
383
                            assert(ssd >= nodes_next[k]->ssd);\
384
                            goto next_##NAME;\
385
                        }\
386
                    }\
387
                     * sample, but the effects of that are negligible.
388
                     * Since nodes in the previous generation are iterated
389
                     * through a heap, they're roughly ordered from better to
390
                     * worse, but not strictly ordered. Therefore, an earlier
391
                     * node with the same sample value is better in most cases
392
                     * (and thus the current is skipped), but not strictly
393
                     * in all cases. Only skipping samples where ssd >=
394
                     * ssd of the earlier node with the same sample gives
395
                     * slightly worse quality, though, for some reason. */ \
396
                    h = &hash[(uint16_t) dec_sample];\
397
                    if (*h == generation)\
398
                        goto next_##NAME;\
387 399
                    if (heap_pos < frontier) {\
388 400
                        pos = heap_pos++;\
389 401
                    } else {\
......
393 405
                        if (ssd > nodes_next[pos]->ssd)\
394 406
                            goto next_##NAME;\
395 407
                    }\
408
                    *h = generation;\
396 409
                    u = nodes_next[pos];\
397 410
                    if(!u) {\
398 411
                        assert(pathn < FREEZE_INTERVAL<<avctx->trellis);\
......
443 456
        nodes = nodes_next;
444 457
        nodes_next = u;
445 458

  
459
        generation++;
460
        if (generation == 255) {
461
            memset(hash, 0xff, 65536 * sizeof(*hash));
462
            generation = 0;
463
        }
464

  
446 465
        // prevent overflow
447 466
        if(nodes[0]->ssd > (1<<28)) {
448 467
            for(j=1; j<frontier && nodes[j]; j++)

Also available in: Unified diff