adxdec.c 5.65 KB
Newer Older
1 2 3 4
/*
 * ADX ADPCM codecs
 * Copyright (c) 2001,2003 BERO
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21 22

#include "libavutil/intreadwrite.h"
23
#include "avcodec.h"
24
#include "adx.h"
25
#include "get_bits.h"
26
#include "internal.h"
27 28

/**
29
 * @file
30 31 32 33 34 35 36
 * SEGA CRI adx codecs.
 *
 * Reference documents:
 * http://ku-www.ss.titech.ac.jp/~yatsushi/adx.html
 * adx2wav & wav2adx http://www.geocities.co.jp/Playtown/2004/
 */

37
static av_cold int adx_decode_init(AVCodecContext *avctx)
38
{
Justin Ruggles's avatar
Justin Ruggles committed
39 40 41
    ADXContext *c = avctx->priv_data;
    int ret, header_size;

42
    if (avctx->extradata_size >= 24) {
43 44 45
        if ((ret = ff_adx_decode_header(avctx, avctx->extradata,
                                        avctx->extradata_size, &header_size,
                                        c->coeff)) < 0) {
46 47 48
            av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n");
            return AVERROR_INVALIDDATA;
        }
49 50
        c->channels      = avctx->channels;
        c->header_parsed = 1;
Justin Ruggles's avatar
Justin Ruggles committed
51 52
    }

53
    avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
54

55 56 57
    return 0;
}

58 59 60 61 62 63 64
/**
 * Decode 32 samples from 18 bytes.
 *
 * A 16-bit scalar value is applied to 32 residuals, which then have a
 * 2nd-order LPC filter applied to it to form the output signal for a single
 * channel.
 */
65 66
static int adx_decode(ADXContext *c, int16_t *out, int offset,
                      const uint8_t *in, int ch)
67
{
68
    ADXChannelState *prev = &c->prev[ch];
69
    GetBitContext gb;
70
    int scale = AV_RB16(in);
71
    int i;
72
    int s0, s1, s2, d;
73

Justin Ruggles's avatar
Justin Ruggles committed
74 75 76
    /* check if this is an EOF packet */
    if (scale & 0x8000)
        return -1;
77

78
    init_get_bits(&gb, in + 2, (BLOCK_SIZE - 2) * 8);
79
    out += offset;
80 81
    s1 = prev->s1;
    s2 = prev->s2;
82
    for (i = 0; i < BLOCK_SAMPLES; i++) {
83
        d  = get_sbits(&gb, 4);
84
        s0 = ((d << COEFF_BITS) * scale + c->coeff[0] * s1 + c->coeff[1] * s2) >> COEFF_BITS;
85
        s2 = s1;
86
        s1 = av_clip_int16(s0);
87
        *out++ = s1;
88 89 90
    }
    prev->s1 = s1;
    prev->s2 = s2;
91

Justin Ruggles's avatar
Justin Ruggles committed
92
    return 0;
93 94
}

95 96
static int adx_decode_frame(AVCodecContext *avctx, void *data,
                            int *got_frame_ptr, AVPacket *avpkt)
97
{
98
    AVFrame *frame      = data;
99 100
    int buf_size        = avpkt->size;
    ADXContext *c       = avctx->priv_data;
101 102
    int16_t **samples;
    int samples_offset;
Justin Ruggles's avatar
Justin Ruggles committed
103
    const uint8_t *buf  = avpkt->data;
104
    const uint8_t *buf_end = buf + avpkt->size;
105
    int num_blocks, ch, ret;
Justin Ruggles's avatar
Justin Ruggles committed
106 107

    if (c->eof) {
108
        *got_frame_ptr = 0;
Justin Ruggles's avatar
Justin Ruggles committed
109
        return buf_size;
110 111
    }

112
    if (!c->header_parsed && buf_size >= 2 && AV_RB16(buf) == 0x8000) {
113
        int header_size;
114 115
        if ((ret = ff_adx_decode_header(avctx, buf, buf_size, &header_size,
                                        c->coeff)) < 0) {
116 117 118
            av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n");
            return AVERROR_INVALIDDATA;
        }
119 120 121
        c->channels      = avctx->channels;
        c->header_parsed = 1;
        if (buf_size < header_size)
122
            return AVERROR_INVALIDDATA;
123
        buf      += header_size;
124 125
        buf_size -= header_size;
    }
126
    if (!c->header_parsed)
127
        return AVERROR_INVALIDDATA;
128

129
    /* calculate number of blocks in the packet */
Justin Ruggles's avatar
Justin Ruggles committed
130
    num_blocks = buf_size / (BLOCK_SIZE * c->channels);
131 132 133 134

    /* if the packet is not an even multiple of BLOCK_SIZE, check for an EOF
       packet */
    if (!num_blocks || buf_size % (BLOCK_SIZE * avctx->channels)) {
Justin Ruggles's avatar
Justin Ruggles committed
135 136
        if (buf_size >= 4 && (AV_RB16(buf) & 0x8000)) {
            c->eof = 1;
137
            *got_frame_ptr = 0;
Justin Ruggles's avatar
Justin Ruggles committed
138
            return avpkt->size;
139
        }
140
        return AVERROR_INVALIDDATA;
141
    }
142

143
    /* get output buffer */
144
    frame->nb_samples = num_blocks * BLOCK_SAMPLES;
145
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
146
        return ret;
147
    samples = (int16_t **)frame->extended_data;
148
    samples_offset = 0;
149

150
    while (num_blocks--) {
Justin Ruggles's avatar
Justin Ruggles committed
151
        for (ch = 0; ch < c->channels; ch++) {
152
            if (buf_end - buf < BLOCK_SIZE || adx_decode(c, samples[ch], samples_offset, buf, ch)) {
Justin Ruggles's avatar
Justin Ruggles committed
153 154 155 156 157 158 159
                c->eof = 1;
                buf = avpkt->data + avpkt->size;
                break;
            }
            buf_size -= BLOCK_SIZE;
            buf      += BLOCK_SIZE;
        }
160 161
        if (!c->eof)
            samples_offset += BLOCK_SAMPLES;
162
    }
163

164
    frame->nb_samples = samples_offset;
165
    *got_frame_ptr = 1;
166

Justin Ruggles's avatar
Justin Ruggles committed
167
    return buf - avpkt->data;
168 169
}

170 171 172 173 174 175 176
static void adx_decode_flush(AVCodecContext *avctx)
{
    ADXContext *c = avctx->priv_data;
    memset(c->prev, 0, sizeof(c->prev));
    c->eof = 0;
}

177
AVCodec ff_adpcm_adx_decoder = {
178
    .name           = "adpcm_adx",
179
    .long_name      = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"),
180
    .type           = AVMEDIA_TYPE_AUDIO,
181
    .id             = AV_CODEC_ID_ADPCM_ADX,
182 183 184
    .priv_data_size = sizeof(ADXContext),
    .init           = adx_decode_init,
    .decode         = adx_decode_frame,
185
    .flush          = adx_decode_flush,
186
    .capabilities   = AV_CODEC_CAP_DR1,
187 188
    .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
                                                      AV_SAMPLE_FMT_NONE },
189
};