Coverage Report

Created: 2026-05-23 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/8svx.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2008 Jaikrishnan Menon
3
 * Copyright (C) 2011 Stefano Sabatini
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
/**
23
 * @file
24
 * 8svx audio decoder
25
 * @author Jaikrishnan Menon
26
 *
27
 * supports: fibonacci delta encoding
28
 *         : exponential encoding
29
 *
30
 * For more information about the 8SVX format:
31
 * http://netghost.narod.ru/gff/vendspec/iff/iff.txt
32
 * http://sox.sourceforge.net/AudioFormats-11.html
33
 * http://aminet.net/package/mus/misc/wavepak
34
 * http://amigan.1emu.net/reg/8SVX.txt
35
 *
36
 * Samples can be found here:
37
 * http://aminet.net/mods/smpl/
38
 */
39
40
#include "config_components.h"
41
42
#include "libavutil/avassert.h"
43
#include "libavutil/mem.h"
44
#include "avcodec.h"
45
#include "codec_internal.h"
46
#include "decode.h"
47
#include "libavutil/common.h"
48
49
/** decoder context */
50
typedef struct EightSvxContext {
51
    uint8_t fib_acc[2];
52
    const int8_t *table;
53
54
    /* buffer used to store the whole first packet.
55
       data is only sent as one large packet */
56
    uint8_t *data[2];
57
    int data_size;
58
    int data_idx;
59
} EightSvxContext;
60
61
static const int8_t fibonacci[16]   = { -34,  -21, -13,  -8, -5, -3, -2, -1, 0, 1, 2, 3, 5, 8,  13, 21 };
62
static const int8_t exponential[16] = { -128, -64, -32, -16, -8, -4, -2, -1, 0, 1, 2, 4, 8, 16, 32, 64 };
63
64
#define MAX_FRAME_SIZE 2048
65
66
/**
67
 * Delta decode the compressed values in src, and put the resulting
68
 * decoded samples in dst.
69
 *
70
 * @param[in,out] state starting value. it is saved for use in the next call.
71
 * @param table delta sequence table
72
 */
73
static void delta_decode(uint8_t *dst, const uint8_t *src, int src_size,
74
                         uint8_t *state, const int8_t *table)
75
20.4k
{
76
20.4k
    uint8_t val = *state;
77
78
39.6M
    while (src_size--) {
79
39.6M
        uint8_t d = *src++;
80
39.6M
        val = av_clip_uint8(val + table[d & 0xF]);
81
39.6M
        *dst++ = val;
82
39.6M
        val = av_clip_uint8(val + table[d >> 4]);
83
39.6M
        *dst++ = val;
84
39.6M
    }
85
86
20.4k
    *state = val;
87
20.4k
}
88
89
/** decode a frame */
90
static int eightsvx_decode_frame(AVCodecContext *avctx, AVFrame *frame,
91
                                 int *got_frame_ptr, AVPacket *avpkt)
92
446k
{
93
446k
    EightSvxContext *esc = avctx->priv_data;
94
446k
    int channels         = avctx->ch_layout.nb_channels;
95
446k
    int buf_size;
96
446k
    int ch, ret;
97
446k
    int hdr_size = 2;
98
99
    /* decode and interleave the first packet */
100
446k
    if (!esc->data[0] && avpkt) {
101
98.1k
        int chan_size = avpkt->size / channels - hdr_size;
102
103
98.1k
        if (avpkt->size % channels) {
104
24.2k
            av_log(avctx, AV_LOG_WARNING, "Packet with odd size, ignoring last byte\n");
105
24.2k
        }
106
98.1k
        if (avpkt->size < (hdr_size + 1) * channels) {
107
97.1k
            av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
108
97.1k
            return AVERROR_INVALIDDATA;
109
97.1k
        }
110
111
1.04k
        esc->fib_acc[0] = avpkt->data[1] + 128;
112
1.04k
        if (channels == 2)
113
377
            esc->fib_acc[1] = avpkt->data[2+chan_size+1] + 128;
114
115
1.04k
        esc->data_idx  = 0;
116
1.04k
        esc->data_size = chan_size;
117
1.04k
        if (!(esc->data[0] = av_malloc(chan_size)))
118
0
            return AVERROR(ENOMEM);
119
1.04k
        if (channels == 2) {
120
377
            if (!(esc->data[1] = av_malloc(chan_size))) {
121
0
                av_freep(&esc->data[0]);
122
0
                return AVERROR(ENOMEM);
123
0
            }
124
377
        }
125
1.04k
        memcpy(esc->data[0], &avpkt->data[hdr_size], chan_size);
126
1.04k
        if (channels == 2)
127
377
            memcpy(esc->data[1], &avpkt->data[2*hdr_size+chan_size], chan_size);
128
1.04k
    }
129
349k
    if (!esc->data[0]) {
130
0
        av_log(avctx, AV_LOG_ERROR, "unexpected empty packet\n");
131
0
        return AVERROR_INVALIDDATA;
132
0
    }
133
134
    /* decode next piece of data from the buffer */
135
349k
    buf_size = FFMIN(MAX_FRAME_SIZE, esc->data_size - esc->data_idx);
136
349k
    if (buf_size <= 0) {
137
330k
        *got_frame_ptr = 0;
138
330k
        return avpkt->size;
139
330k
    }
140
141
    /* get output buffer */
142
18.5k
    frame->nb_samples = buf_size * 2;
143
18.5k
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
144
0
        return ret;
145
146
39.0k
    for (ch = 0; ch < channels; ch++) {
147
20.4k
        delta_decode(frame->data[ch], &esc->data[ch][esc->data_idx],
148
20.4k
                     buf_size, &esc->fib_acc[ch], esc->table);
149
20.4k
    }
150
151
18.5k
    esc->data_idx += buf_size;
152
153
18.5k
    *got_frame_ptr = 1;
154
155
18.5k
    return ((avctx->frame_num == 0) * hdr_size + buf_size) * channels;
156
18.5k
}
157
158
static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
159
1.25k
{
160
1.25k
    EightSvxContext *esc = avctx->priv_data;
161
162
1.25k
    if (avctx->ch_layout.nb_channels < 1 || avctx->ch_layout.nb_channels > 2) {
163
91
        av_log(avctx, AV_LOG_ERROR, "8SVX does not support more than 2 channels\n");
164
91
        return AVERROR_INVALIDDATA;
165
91
    }
166
167
1.16k
    switch (avctx->codec->id) {
168
599
    case AV_CODEC_ID_8SVX_FIB: esc->table = fibonacci;    break;
169
568
    case AV_CODEC_ID_8SVX_EXP: esc->table = exponential;  break;
170
0
    default:
171
0
        av_assert1(0);
172
1.16k
    }
173
1.16k
    avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
174
175
1.16k
    return 0;
176
1.16k
}
177
178
static av_cold int eightsvx_decode_close(AVCodecContext *avctx)
179
1.16k
{
180
1.16k
    EightSvxContext *esc = avctx->priv_data;
181
182
1.16k
    av_freep(&esc->data[0]);
183
1.16k
    av_freep(&esc->data[1]);
184
1.16k
    esc->data_size = 0;
185
1.16k
    esc->data_idx = 0;
186
187
1.16k
    return 0;
188
1.16k
}
189
190
#if CONFIG_EIGHTSVX_FIB_DECODER
191
const FFCodec ff_eightsvx_fib_decoder = {
192
  .p.name         = "8svx_fib",
193
  CODEC_LONG_NAME("8SVX fibonacci"),
194
  .p.type         = AVMEDIA_TYPE_AUDIO,
195
  .p.id           = AV_CODEC_ID_8SVX_FIB,
196
  .priv_data_size = sizeof (EightSvxContext),
197
  .init           = eightsvx_decode_init,
198
  FF_CODEC_DECODE_CB(eightsvx_decode_frame),
199
  .close          = eightsvx_decode_close,
200
  .p.capabilities = AV_CODEC_CAP_DR1,
201
};
202
#endif
203
#if CONFIG_EIGHTSVX_EXP_DECODER
204
const FFCodec ff_eightsvx_exp_decoder = {
205
  .p.name         = "8svx_exp",
206
  CODEC_LONG_NAME("8SVX exponential"),
207
  .p.type         = AVMEDIA_TYPE_AUDIO,
208
  .p.id           = AV_CODEC_ID_8SVX_EXP,
209
  .priv_data_size = sizeof (EightSvxContext),
210
  .init           = eightsvx_decode_init,
211
  FF_CODEC_DECODE_CB(eightsvx_decode_frame),
212
  .close          = eightsvx_decode_close,
213
  .p.capabilities = AV_CODEC_CAP_DR1,
214
};
215
#endif