Revision 2ff4524e libavcodec/adpcm.c

View differences:

libavcodec/adpcm.c
117 117
    /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
118 118
};
119 119

  
120
static const int yamaha_indexscale[] = {
121
    230, 230, 230, 230, 307, 409, 512, 614,
122
    230, 230, 230, 230, 307, 409, 512, 614
123
};
124

  
125
static const int yamaha_difflookup[] = {
126
    1, 3, 5, 7, 9, 11, 13, 15,
127
    -1, -3, -5, -7, -9, -11, -13, -15
128
};
129

  
120 130
/* end of tables */
121 131

  
122 132
typedef struct ADPCMChannelStatus {
......
168 178
                                                             /* and we have 7 bytes per channel overhead */
169 179
        avctx->block_align = BLKSIZE;
170 180
        break;
181
    case CODEC_ID_ADPCM_YAMAHA:
182
        avctx->frame_size = BLKSIZE * avctx->channels;
183
        avctx->block_align = BLKSIZE;
184
        break;
171 185
    default:
172 186
        return -1;
173 187
        break;
......
260 274
    return nibble;
261 275
}
262 276

  
277
static inline unsigned char adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, short sample)
278
{
279
    int i1 = 0, j1;
280

  
281
    if(!c->step) {
282
        c->predictor = 0;
283
        c->step = 127;
284
    }
285
    j1 = sample - c->predictor;
286

  
287
    j1 = (j1 * 8) / c->step;
288
    i1 = abs(j1) / 2;
289
    if (i1 > 7)
290
        i1 = 7;
291
    if (j1 < 0)
292
        i1 += 8;
293

  
294
    c->predictor = c->predictor + ((c->step * yamaha_difflookup[i1]) / 8);
295
    CLAMP_TO_SHORT(c->predictor);
296
    c->step = (c->step * yamaha_indexscale[i1]) >> 8;
297
    c->step = clip(c->step, 127, 24567);
298

  
299
    return i1;
300
}
301

  
263 302
static int adpcm_encode_frame(AVCodecContext *avctx,
264 303
			    unsigned char *frame, int buf_size, void *data)
265 304
{
......
362 401
            *dst++ = nibble;
363 402
        }
364 403
        break;
404
    case CODEC_ID_ADPCM_YAMAHA:
405
        n = avctx->frame_size / 2;
406
        for (; n>0; n--) {
407
            for(i = 0; i < avctx->channels; i++) {
408
                int nibble;
409
                nibble  = adpcm_yamaha_compress_sample(&c->status[i], samples[i]) << 4;
410
                nibble |= adpcm_yamaha_compress_sample(&c->status[i], samples[i+avctx->channels]);
411
                *dst++ = nibble;
412
            }
413
            samples += 2 * avctx->channels;
414
        }
415
        break;
365 416
    default:
366 417
        return -1;
367 418
    }
......
463 514
    return (short)predictor;
464 515
}
465 516

  
517
static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
518
{
519
    if(!c->step) {
520
        c->predictor = 0;
521
        c->step = 127;
522
    }
523

  
524
    c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;
525
    CLAMP_TO_SHORT(c->predictor);
526
    c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
527
    c->step = clip(c->step, 127, 24567);
528
    return c->predictor;
529
}
530

  
466 531
static void xa_decode(short *out, const unsigned char *in, 
467 532
    ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
468 533
{
......
978 1043
	
979 1044
	break;
980 1045
    }
1046
    case CODEC_ID_ADPCM_YAMAHA:
1047
        while (src < buf + buf_size) {
1048
            if (st) {
1049
                *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1050
                        (src[0] >> 4) & 0x0F);
1051
                *samples++ = adpcm_yamaha_expand_nibble(&c->status[1],
1052
                        src[0] & 0x0F);
1053
            } else {
1054
                *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1055
                        (src[0] >> 4) & 0x0F);
1056
                *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1057
                        src[0] & 0x0F);
1058
            }
1059
            src++;
1060
        }
1061
        break;
981 1062
    default:
982 1063
        return -1;
983 1064
    }
......
1035 1116
ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea);
1036 1117
ADPCM_CODEC(CODEC_ID_ADPCM_CT, adpcm_ct);
1037 1118
ADPCM_CODEC(CODEC_ID_ADPCM_SWF, adpcm_swf);
1119
ADPCM_CODEC(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha);
1038 1120

  
1039 1121
#undef ADPCM_CODEC

Also available in: Unified diff