Revision 07ece20c libavcodec/adpcm.c

View differences:

libavcodec/adpcm.c
145 145
    int idelta;
146 146
} ADPCMChannelStatus;
147 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

  
148 161
typedef struct ADPCMContext {
149 162
    ADPCMChannelStatus status[6];
163
    TrellisPath *paths;
164
    TrellisNode *node_buf;
165
    TrellisNode **nodep_buf;
150 166
} ADPCMContext;
151 167

  
168
#define FREEZE_INTERVAL 128
169

  
152 170
/* XXX: implement encoding */
153 171

  
154 172
#if CONFIG_ENCODERS
155 173
static av_cold int adpcm_encode_init(AVCodecContext *avctx)
156 174
{
175
    ADPCMContext *s = avctx->priv_data;
157 176
    uint8_t *extradata;
158 177
    int i;
159 178
    if (avctx->channels > 2)
......
164 183
        return -1;
165 184
    }
166 185

  
186
    if (avctx->trellis) {
187
        int frontier = 1 << avctx->trellis;
188
        int max_paths =  frontier * FREEZE_INTERVAL;
189
        FF_ALLOC_OR_GOTO(avctx, s->paths,     max_paths * sizeof(*s->paths), error);
190
        FF_ALLOC_OR_GOTO(avctx, s->node_buf,  2 * frontier * sizeof(*s->node_buf), error);
191
        FF_ALLOC_OR_GOTO(avctx, s->nodep_buf, 2 * frontier * sizeof(*s->nodep_buf), error);
192
    }
193

  
167 194
    switch(avctx->codec->id) {
168 195
    case CODEC_ID_ADPCM_IMA_WAV:
169 196
        avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
......
199 226
            avctx->sample_rate != 22050 &&
200 227
            avctx->sample_rate != 44100) {
201 228
            av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, 22050 or 44100\n");
202
            return -1;
229
            goto error;
203 230
        }
204 231
        avctx->frame_size = 512 * (avctx->sample_rate / 11025);
205 232
        break;
206 233
    default:
207
        return -1;
234
        goto error;
208 235
    }
209 236

  
210 237
    avctx->coded_frame= avcodec_alloc_frame();
211 238
    avctx->coded_frame->key_frame= 1;
212 239

  
213 240
    return 0;
241
error:
242
    av_freep(&s->paths);
243
    av_freep(&s->node_buf);
244
    av_freep(&s->nodep_buf);
245
    return -1;
214 246
}
215 247

  
216 248
static av_cold int adpcm_encode_close(AVCodecContext *avctx)
217 249
{
250
    ADPCMContext *s = avctx->priv_data;
218 251
    av_freep(&avctx->coded_frame);
252
    av_freep(&s->paths);
253
    av_freep(&s->node_buf);
254
    av_freep(&s->nodep_buf);
219 255

  
220 256
    return 0;
221 257
}
......
276 312
    return nibble;
277 313
}
278 314

  
279
typedef struct TrellisPath {
280
    int nibble;
281
    int prev;
282
} TrellisPath;
283

  
284
typedef struct TrellisNode {
285
    uint32_t ssd;
286
    int path;
287
    int sample1;
288
    int sample2;
289
    int step;
290
} TrellisNode;
291

  
292 315
static void adpcm_compress_trellis(AVCodecContext *avctx, const short *samples,
293 316
                                   uint8_t *dst, ADPCMChannelStatus *c, int n)
294 317
{
295
#define FREEZE_INTERVAL 128
296 318
    //FIXME 6% faster if frontier is a compile-time constant
319
    ADPCMContext *s = avctx->priv_data;
297 320
    const int frontier = 1 << avctx->trellis;
298 321
    const int stride = avctx->channels;
299 322
    const int version = avctx->codec->id;
300
    const int max_paths = frontier*FREEZE_INTERVAL;
301
    TrellisPath paths[max_paths], *p;
302
    TrellisNode node_buf[2][frontier];
303
    TrellisNode *nodep_buf[2][frontier];
304
    TrellisNode **nodes = nodep_buf[0]; // nodes[] is always sorted by .ssd
305
    TrellisNode **nodes_next = nodep_buf[1];
323
    TrellisPath *paths = s->paths, *p;
324
    TrellisNode *node_buf = s->node_buf;
325
    TrellisNode **nodep_buf = s->nodep_buf;
326
    TrellisNode **nodes = nodep_buf; // nodes[] is always sorted by .ssd
327
    TrellisNode **nodes_next = nodep_buf + frontier;
306 328
    int pathn = 0, froze = -1, i, j, k;
307 329

  
308
    assert(!(max_paths&(max_paths-1)));
309

  
310
    memset(nodep_buf, 0, sizeof(nodep_buf));
311
    nodes[0] = &node_buf[1][0];
330
    memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf));
331
    nodes[0] = node_buf + frontier;
312 332
    nodes[0]->ssd = 0;
313 333
    nodes[0]->path = 0;
314 334
    nodes[0]->step = c->step_index;
......
329 349
    }
330 350

  
331 351
    for(i=0; i<n; i++) {
332
        TrellisNode *t = node_buf[i&1];
352
        TrellisNode *t = node_buf + frontier*(i&1);
333 353
        TrellisNode **u;
334 354
        int sample = samples[i*stride];
335 355
        memset(nodes_next, 0, frontier*sizeof(TrellisNode*));
......
367 387
                        if(!nodes_next[k] || ssd < nodes_next[k]->ssd) {\
368 388
                            TrellisNode *u = nodes_next[frontier-1];\
369 389
                            if(!u) {\
370
                                assert(pathn < max_paths);\
390
                                assert(pathn < FREEZE_INTERVAL<<avctx->trellis);\
371 391
                                u = t++;\
372 392
                                u->path = pathn++;\
373 393
                            }\
......
454 474
    short *samples;
455 475
    unsigned char *dst;
456 476
    ADPCMContext *c = avctx->priv_data;
477
    uint8_t *buf;
457 478

  
458 479
    dst = frame;
459 480
    samples = (short *)data;
......
480 501

  
481 502
            /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
482 503
            if(avctx->trellis > 0) {
483
                uint8_t buf[2][n*8];
484
                adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n*8);
504
                FF_ALLOC_OR_GOTO(avctx, buf, 2*n*8, error);
505
                adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n*8);
485 506
                if(avctx->channels == 2)
486
                    adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n*8);
507
                    adpcm_compress_trellis(avctx, samples+1, buf + n*8, &c->status[1], n*8);
487 508
                for(i=0; i<n; i++) {
488
                    *dst++ = buf[0][8*i+0] | (buf[0][8*i+1] << 4);
489
                    *dst++ = buf[0][8*i+2] | (buf[0][8*i+3] << 4);
490
                    *dst++ = buf[0][8*i+4] | (buf[0][8*i+5] << 4);
491
                    *dst++ = buf[0][8*i+6] | (buf[0][8*i+7] << 4);
509
                    *dst++ = buf[8*i+0] | (buf[8*i+1] << 4);
510
                    *dst++ = buf[8*i+2] | (buf[8*i+3] << 4);
511
                    *dst++ = buf[8*i+4] | (buf[8*i+5] << 4);
512
                    *dst++ = buf[8*i+6] | (buf[8*i+7] << 4);
492 513
                    if (avctx->channels == 2) {
493
                        *dst++ = buf[1][8*i+0] | (buf[1][8*i+1] << 4);
494
                        *dst++ = buf[1][8*i+2] | (buf[1][8*i+3] << 4);
495
                        *dst++ = buf[1][8*i+4] | (buf[1][8*i+5] << 4);
496
                        *dst++ = buf[1][8*i+6] | (buf[1][8*i+7] << 4);
514
                        uint8_t *buf1 = buf + n*8;
515
                        *dst++ = buf1[8*i+0] | (buf1[8*i+1] << 4);
516
                        *dst++ = buf1[8*i+2] | (buf1[8*i+3] << 4);
517
                        *dst++ = buf1[8*i+4] | (buf1[8*i+5] << 4);
518
                        *dst++ = buf1[8*i+6] | (buf1[8*i+7] << 4);
497 519
                    }
498 520
                }
521
                av_free(buf);
499 522
            } else
500 523
            for (; n>0; n--) {
501 524
                *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]);
......
578 601
        }
579 602

  
580 603
        if(avctx->trellis > 0) {
581
            uint8_t buf[2][n];
582
            adpcm_compress_trellis(avctx, samples+2, buf[0], &c->status[0], n);
604
            FF_ALLOC_OR_GOTO(avctx, buf, 2*n, error);
605
            adpcm_compress_trellis(avctx, samples+2, buf, &c->status[0], n);
583 606
            if (avctx->channels == 2)
584
                adpcm_compress_trellis(avctx, samples+3, buf[1], &c->status[1], n);
607
                adpcm_compress_trellis(avctx, samples+3, buf+n, &c->status[1], n);
585 608
            for(i=0; i<n; i++) {
586
                put_bits(&pb, 4, buf[0][i]);
609
                put_bits(&pb, 4, buf[i]);
587 610
                if (avctx->channels == 2)
588
                    put_bits(&pb, 4, buf[1][i]);
611
                    put_bits(&pb, 4, buf[n+i]);
589 612
            }
613
            av_free(buf);
590 614
        } else {
591 615
            for (i=1; i<avctx->frame_size; i++) {
592 616
                put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels*i]));
......
625 649

  
626 650
        if(avctx->trellis > 0) {
627 651
            int n = avctx->block_align - 7*avctx->channels;
628
            uint8_t buf[2][n];
652
            FF_ALLOC_OR_GOTO(avctx, buf, 2*n, error);
629 653
            if(avctx->channels == 1) {
630
                n *= 2;
631
                adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
654
                adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
632 655
                for(i=0; i<n; i+=2)
633
                    *dst++ = (buf[0][i] << 4) | buf[0][i+1];
656
                    *dst++ = (buf[i] << 4) | buf[i+1];
634 657
            } else {
635
                adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
636
                adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n);
658
                adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
659
                adpcm_compress_trellis(avctx, samples+1, buf+n, &c->status[1], n);
637 660
                for(i=0; i<n; i++)
638
                    *dst++ = (buf[0][i] << 4) | buf[1][i];
661
                    *dst++ = (buf[i] << 4) | buf[n+i];
639 662
            }
663
            av_free(buf);
640 664
        } else
641 665
        for(i=7*avctx->channels; i<avctx->block_align; i++) {
642 666
            int nibble;
......
648 672
    case CODEC_ID_ADPCM_YAMAHA:
649 673
        n = avctx->frame_size / 2;
650 674
        if(avctx->trellis > 0) {
651
            uint8_t buf[2][n*2];
675
            FF_ALLOC_OR_GOTO(avctx, buf, 2*n*2, error);
652 676
            n *= 2;
653 677
            if(avctx->channels == 1) {
654
                adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
678
                adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
655 679
                for(i=0; i<n; i+=2)
656
                    *dst++ = buf[0][i] | (buf[0][i+1] << 4);
680
                    *dst++ = buf[i] | (buf[i+1] << 4);
657 681
            } else {
658
                adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
659
                adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n);
682
                adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n);
683
                adpcm_compress_trellis(avctx, samples+1, buf+n, &c->status[1], n);
660 684
                for(i=0; i<n; i++)
661
                    *dst++ = buf[0][i] | (buf[1][i] << 4);
685
                    *dst++ = buf[i] | (buf[n+i] << 4);
662 686
            }
687
            av_free(buf);
663 688
        } else
664 689
            for (n *= avctx->channels; n>0; n--) {
665 690
                int nibble;
......
669 694
            }
670 695
        break;
671 696
    default:
697
    error:
672 698
        return -1;
673 699
    }
674 700
    return dst - frame;

Also available in: Unified diff