Coverage Report

Created: 2025-11-16 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/adxdec.c
Line
Count
Source
1
/*
2
 * ADX ADPCM codecs
3
 * Copyright (c) 2001,2003 BERO
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * 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
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * 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/attributes.h"
23
#include "libavutil/intreadwrite.h"
24
#include "avcodec.h"
25
#include "adx.h"
26
#include "codec_internal.h"
27
#include "decode.h"
28
#include "get_bits.h"
29
30
/**
31
 * @file
32
 * SEGA CRI adx codecs.
33
 *
34
 * Reference documents:
35
 * http://ku-www.ss.titech.ac.jp/~yatsushi/adx.html
36
 * adx2wav & wav2adx http://www.geocities.co.jp/Playtown/2004/
37
 */
38
39
/**
40
 * Decode ADX stream header.
41
 * Sets avctx->channels and avctx->sample_rate.
42
 *
43
 * @param      avctx        codec context
44
 * @param      buf          header data
45
 * @param      bufsize      data size, should be at least 24 bytes
46
 * @param[out] header_size  size of ADX header
47
 * @param[out] coeff        2 LPC coefficients, can be NULL
48
 * @return data offset or negative error code if header is invalid
49
 */
50
static int adx_decode_header(AVCodecContext *avctx, const uint8_t *buf,
51
                             int bufsize, int *header_size, int *coeff)
52
3.35k
{
53
3.35k
    int offset, cutoff, channels;
54
55
3.35k
    if (bufsize < 24)
56
335
        return AVERROR_INVALIDDATA;
57
58
3.02k
    if (AV_RB16(buf) != 0x8000)
59
38
        return AVERROR_INVALIDDATA;
60
2.98k
    offset = AV_RB16(buf + 2) + 4;
61
62
    /* if copyright string is within the provided data, validate it */
63
2.98k
    if (bufsize >= offset && offset >= 6 && memcmp(buf + offset - 6, "(c)CRI", 6))
64
278
        return AVERROR_INVALIDDATA;
65
66
    /* check for encoding=3 block_size=18, sample_size=4 */
67
2.70k
    if (buf[4] != 3 || buf[5] != 18 || buf[6] != 4) {
68
723
        avpriv_request_sample(avctx, "Support for this ADX format");
69
723
        return AVERROR_PATCHWELCOME;
70
723
    }
71
72
    /* channels */
73
1.98k
    channels = buf[7];
74
1.98k
    if (channels <= 0 || channels > MAX_CHANNELS)
75
412
        return AVERROR_INVALIDDATA;
76
77
1.57k
    if (avctx->ch_layout.nb_channels != channels) {
78
1.03k
        av_channel_layout_uninit(&avctx->ch_layout);
79
1.03k
        avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
80
1.03k
        avctx->ch_layout.nb_channels = channels;
81
1.03k
    }
82
83
    /* sample rate */
84
1.57k
    avctx->sample_rate = AV_RB32(buf + 8);
85
1.57k
    if (avctx->sample_rate < 1 ||
86
1.14k
        avctx->sample_rate > INT_MAX / (channels * BLOCK_SIZE * 8))
87
840
        return AVERROR_INVALIDDATA;
88
89
    /* bit rate */
90
731
    avctx->bit_rate = avctx->sample_rate * channels * BLOCK_SIZE * 8 / BLOCK_SAMPLES;
91
92
    /* LPC coefficients */
93
731
    if (coeff) {
94
731
        cutoff = AV_RB16(buf + 16);
95
731
        ff_adx_calculate_coeffs(cutoff, avctx->sample_rate, COEFF_BITS, coeff);
96
731
    }
97
98
731
    *header_size = offset;
99
731
    return 0;
100
1.57k
}
101
102
static av_cold int adx_decode_init(AVCodecContext *avctx)
103
1.64k
{
104
1.64k
    ADXContext *c = avctx->priv_data;
105
1.64k
    int ret, header_size;
106
107
1.64k
    if (avctx->extradata_size >= 24) {
108
53
        if ((ret = adx_decode_header(avctx, avctx->extradata,
109
53
                                     avctx->extradata_size, &header_size,
110
53
                                     c->coeff)) < 0) {
111
38
            av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n");
112
38
            return AVERROR_INVALIDDATA;
113
38
        }
114
15
        c->channels      = avctx->ch_layout.nb_channels;
115
15
        c->header_parsed = 1;
116
15
    }
117
118
1.60k
    avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
119
120
1.60k
    return 0;
121
1.64k
}
122
123
/**
124
 * Decode 32 samples from 18 bytes.
125
 *
126
 * A 16-bit scalar value is applied to 32 residuals, which then have a
127
 * 2nd-order LPC filter applied to it to form the output signal for a single
128
 * channel.
129
 */
130
static int adx_decode(ADXContext *c, int16_t *out, int offset,
131
                      const uint8_t *in, int ch)
132
531k
{
133
531k
    ADXChannelState *prev = &c->prev[ch];
134
531k
    GetBitContext gb;
135
531k
    int scale = AV_RB16(in);
136
531k
    int i;
137
531k
    int s0, s1, s2, d;
138
139
    /* check if this is an EOF packet */
140
531k
    if (scale & 0x8000)
141
29.5k
        return -1;
142
143
501k
    init_get_bits(&gb, in + 2, (BLOCK_SIZE - 2) * 8);
144
501k
    out += offset;
145
501k
    s1 = prev->s1;
146
501k
    s2 = prev->s2;
147
16.5M
    for (i = 0; i < BLOCK_SAMPLES; i++) {
148
16.0M
        d  = get_sbits(&gb, 4);
149
16.0M
        s0 = d * scale + ((c->coeff[0] * s1 + c->coeff[1] * s2) >> COEFF_BITS);
150
16.0M
        s2 = s1;
151
16.0M
        s1 = av_clip_int16(s0);
152
16.0M
        *out++ = s1;
153
16.0M
    }
154
501k
    prev->s1 = s1;
155
501k
    prev->s2 = s2;
156
157
501k
    return 0;
158
531k
}
159
160
static int adx_decode_frame(AVCodecContext *avctx, AVFrame *frame,
161
                            int *got_frame_ptr, AVPacket *avpkt)
162
570k
{
163
570k
    int buf_size        = avpkt->size;
164
570k
    ADXContext *c       = avctx->priv_data;
165
570k
    int16_t **samples;
166
570k
    int samples_offset;
167
570k
    const uint8_t *buf  = avpkt->data;
168
570k
    const uint8_t *buf_end = buf + avpkt->size;
169
570k
    int num_blocks, ch, ret;
170
570k
    size_t new_extradata_size;
171
570k
    uint8_t *new_extradata;
172
173
570k
    new_extradata = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA,
174
570k
                                            &new_extradata_size);
175
570k
    if (new_extradata && new_extradata_size > 0) {
176
0
        int header_size;
177
0
        if ((ret = adx_decode_header(avctx, new_extradata,
178
0
                                     new_extradata_size, &header_size,
179
0
                                     c->coeff)) < 0) {
180
0
            av_log(avctx, AV_LOG_ERROR, "error parsing new ADX extradata\n");
181
0
            return AVERROR_INVALIDDATA;
182
0
        }
183
184
0
        c->eof = 0;
185
0
    }
186
187
570k
    if (c->eof) {
188
127k
        *got_frame_ptr = 0;
189
127k
        return buf_size;
190
127k
    }
191
192
443k
    if (!c->header_parsed && buf_size >= 2 && AV_RB16(buf) == 0x8000) {
193
3.30k
        int header_size;
194
3.30k
        if ((ret = adx_decode_header(avctx, buf, buf_size, &header_size,
195
3.30k
                                     c->coeff)) < 0) {
196
2.58k
            av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n");
197
2.58k
            return AVERROR_INVALIDDATA;
198
2.58k
        }
199
716
        c->channels      = avctx->ch_layout.nb_channels;
200
716
        c->header_parsed = 1;
201
716
        if (buf_size < header_size)
202
157
            return AVERROR_INVALIDDATA;
203
559
        buf      += header_size;
204
559
        buf_size -= header_size;
205
559
    }
206
440k
    if (!c->header_parsed)
207
119k
        return AVERROR_INVALIDDATA;
208
209
    /* calculate number of blocks in the packet */
210
321k
    num_blocks = buf_size / (BLOCK_SIZE * c->channels);
211
212
    /* if the packet is not an even multiple of BLOCK_SIZE, check for an EOF
213
       packet */
214
321k
    if (!num_blocks || buf_size % (BLOCK_SIZE * c->channels)) {
215
47.7k
        if (buf_size >= 4 && (AV_RB16(buf) & 0x8000)) {
216
2.19k
            c->eof = 1;
217
2.19k
            *got_frame_ptr = 0;
218
2.19k
            return avpkt->size;
219
2.19k
        }
220
45.5k
        return AVERROR_INVALIDDATA;
221
47.7k
    }
222
223
    /* get output buffer */
224
273k
    frame->nb_samples = num_blocks * BLOCK_SAMPLES;
225
273k
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
226
0
        return ret;
227
273k
    samples = (int16_t **)frame->extended_data;
228
273k
    samples_offset = 0;
229
230
1.10M
    while (num_blocks--) {
231
1.33M
        for (ch = 0; ch < c->channels; ch++) {
232
835k
            if (buf_end - buf < BLOCK_SIZE || adx_decode(c, samples[ch], samples_offset, buf, ch)) {
233
334k
                c->eof = 1;
234
334k
                buf = avpkt->data + avpkt->size;
235
334k
                break;
236
334k
            }
237
501k
            buf_size -= BLOCK_SIZE;
238
501k
            buf      += BLOCK_SIZE;
239
501k
        }
240
833k
        if (!c->eof)
241
499k
            samples_offset += BLOCK_SAMPLES;
242
833k
    }
243
244
273k
    frame->nb_samples = samples_offset;
245
273k
    *got_frame_ptr = 1;
246
247
273k
    return buf - avpkt->data;
248
273k
}
249
250
static av_cold void adx_decode_flush(AVCodecContext *avctx)
251
213k
{
252
213k
    ADXContext *c = avctx->priv_data;
253
213k
    memset(c->prev, 0, sizeof(c->prev));
254
213k
    c->eof = 0;
255
213k
}
256
257
const FFCodec ff_adpcm_adx_decoder = {
258
    .p.name         = "adpcm_adx",
259
    CODEC_LONG_NAME("SEGA CRI ADX ADPCM"),
260
    .p.type         = AVMEDIA_TYPE_AUDIO,
261
    .p.id           = AV_CODEC_ID_ADPCM_ADX,
262
    .priv_data_size = sizeof(ADXContext),
263
    .init           = adx_decode_init,
264
    FF_CODEC_DECODE_CB(adx_decode_frame),
265
    .flush          = adx_decode_flush,
266
    .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF |
267
                      AV_CODEC_CAP_DR1,
268
    CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16P),
269
};