FFmpeg
Loading...
Searching...
No Matches
adxdec.c
Go to the documentation of this file.
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
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 */
50static int adx_decode_header(AVCodecContext *avctx, const uint8_t *buf,
51 int bufsize, int *header_size, int *coeff)
52{
53 int offset, cutoff, channels;
54
55 if (bufsize < 24)
57
58 if (AV_RB16(buf) != 0x8000)
60 offset = AV_RB16(buf + 2) + 4;
61
62 /* if copyright string is within the provided data, validate it */
63 if (bufsize >= offset && offset >= 6 && memcmp(buf + offset - 6, "(c)CRI", 6))
65
66 /* check for encoding=3 block_size=18, sample_size=4 */
67 if (buf[4] != 3 || buf[5] != 18 || buf[6] != 4) {
68 avpriv_request_sample(avctx, "Support for this ADX format");
70 }
71
72 /* channels */
73 channels = buf[7];
76
77 if (avctx->ch_layout.nb_channels != channels) {
81 }
82
83 /* sample rate */
84 avctx->sample_rate = AV_RB32(buf + 8);
85 if (avctx->sample_rate < 1 ||
86 avctx->sample_rate > INT_MAX / (channels * BLOCK_SIZE * 8))
88
89 /* bit rate */
90 avctx->bit_rate = avctx->sample_rate * channels * BLOCK_SIZE * 8 / BLOCK_SAMPLES;
91
92 /* LPC coefficients */
93 if (coeff) {
94 cutoff = AV_RB16(buf + 16);
96 }
97
98 *header_size = offset;
99 return 0;
100}
101
103{
104 ADXContext *c = avctx->priv_data;
105 int ret, header_size;
106
107 if (avctx->extradata_size >= 24) {
108 if ((ret = adx_decode_header(avctx, avctx->extradata,
109 avctx->extradata_size, &header_size,
110 c->coeff)) < 0) {
111 av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n");
112 return AVERROR_INVALIDDATA;
113 }
114 c->channels = avctx->ch_layout.nb_channels;
115 c->header_parsed = 1;
116 }
117
119
120 return 0;
121}
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 */
130static int adx_decode(ADXContext *c, int16_t *out, int offset,
131 const uint8_t *in, int ch)
132{
133 ADXChannelState *prev = &c->prev[ch];
134 GetBitContext gb;
135 int scale = AV_RB16(in);
136 int i;
137 int s0, s1, s2, d;
138
139 /* check if this is an EOF packet */
140 if (scale & 0x8000)
141 return -1;
142
143 init_get_bits(&gb, in + 2, (BLOCK_SIZE - 2) * 8);
144 out += offset;
145 s1 = prev->s1;
146 s2 = prev->s2;
147 for (i = 0; i < BLOCK_SAMPLES; i++) {
148 d = get_sbits(&gb, 4);
149 s0 = d * scale + ((c->coeff[0] * s1 + c->coeff[1] * s2) >> COEFF_BITS);
150 s2 = s1;
151 s1 = av_clip_int16(s0);
152 *out++ = s1;
153 }
154 prev->s1 = s1;
155 prev->s2 = s2;
156
157 return 0;
158}
159
161 int *got_frame_ptr, AVPacket *avpkt)
162{
163 int buf_size = avpkt->size;
164 ADXContext *c = avctx->priv_data;
165 int16_t **samples;
166 int samples_offset;
167 const uint8_t *buf = avpkt->data;
168 const uint8_t *buf_end = buf + avpkt->size;
169 int num_blocks, ch, ret;
170 size_t new_extradata_size;
171 uint8_t *new_extradata;
172
174 &new_extradata_size);
175 if (new_extradata && new_extradata_size > 0) {
176 int old_channels = c->channels;
177 int header_size;
178 if ((ret = adx_decode_header(avctx, new_extradata,
179 new_extradata_size, &header_size,
180 c->coeff)) < 0) {
181 av_log(avctx, AV_LOG_ERROR, "error parsing new ADX extradata\n");
182 return AVERROR_INVALIDDATA;
183 }
184
185 c->channels = avctx->ch_layout.nb_channels;
186 c->header_parsed = 1;
187 if (old_channels != c->channels)
188 memset(c->prev, 0, sizeof(c->prev));
189 c->eof = 0;
190 }
191
192 if (c->eof) {
193 *got_frame_ptr = 0;
194 return buf_size;
195 }
196
197 if (!c->header_parsed && buf_size >= 2 && AV_RB16(buf) == 0x8000) {
198 int header_size;
199 if ((ret = adx_decode_header(avctx, buf, buf_size, &header_size,
200 c->coeff)) < 0) {
201 av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n");
202 return AVERROR_INVALIDDATA;
203 }
204 c->channels = avctx->ch_layout.nb_channels;
205 c->header_parsed = 1;
206 if (buf_size < header_size)
207 return AVERROR_INVALIDDATA;
208 buf += header_size;
209 buf_size -= header_size;
210 }
211 if (!c->header_parsed)
212 return AVERROR_INVALIDDATA;
213
214 /* calculate number of blocks in the packet */
215 num_blocks = buf_size / (BLOCK_SIZE * c->channels);
216
217 /* if the packet is not an even multiple of BLOCK_SIZE, check for an EOF
218 packet */
219 if (!num_blocks || buf_size % (BLOCK_SIZE * c->channels)) {
220 if (buf_size >= 4 && (AV_RB16(buf) & 0x8000)) {
221 c->eof = 1;
222 *got_frame_ptr = 0;
223 return avpkt->size;
224 }
225 return AVERROR_INVALIDDATA;
226 }
227
228 /* get output buffer */
229 frame->nb_samples = num_blocks * BLOCK_SAMPLES;
230 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
231 return ret;
232 samples = (int16_t **)frame->extended_data;
233 samples_offset = 0;
234
235 while (num_blocks--) {
236 for (ch = 0; ch < c->channels; ch++) {
237 if (buf_end - buf < BLOCK_SIZE || adx_decode(c, samples[ch], samples_offset, buf, ch)) {
238 c->eof = 1;
239 buf = avpkt->data + avpkt->size;
240 break;
241 }
242 buf_size -= BLOCK_SIZE;
243 buf += BLOCK_SIZE;
244 }
245 if (!c->eof)
246 samples_offset += BLOCK_SAMPLES;
247 }
248
249 frame->nb_samples = samples_offset;
250 *got_frame_ptr = 1;
251
252 return buf - avpkt->data;
253}
254
256{
257 ADXContext *c = avctx->priv_data;
258 memset(c->prev, 0, sizeof(c->prev));
259 c->eof = 0;
260}
261
263 .p.name = "adpcm_adx",
264 CODEC_LONG_NAME("SEGA CRI ADX ADPCM"),
265 .p.type = AVMEDIA_TYPE_AUDIO,
266 .p.id = AV_CODEC_ID_ADPCM_ADX,
267 .priv_data_size = sizeof(ADXContext),
270 .flush = adx_decode_flush,
271 .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF |
273};
#define MAX_CHANNELS
Definition aac.h:33
void ff_adx_calculate_coeffs(int cutoff, int sample_rate, int bits, int *coeff)
Calculate LPC coefficients based on cutoff frequency and sample rate.
Definition adx.c:25
SEGA CRI adx codecs.
#define COEFF_BITS
Definition adx.h:49
#define BLOCK_SAMPLES
Definition adx.h:52
#define BLOCK_SIZE
Definition adx.h:51
static FILE * out
channels
Definition aptx.h:31
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define av_clip_int16
Definition common.h:115
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
bitstream reader API header.
static int get_sbits(GetBitContext *s, int n)
Definition get_bits.h:322
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition get_bits.h:517
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition codec.h:94
@ AV_CODEC_ID_ADPCM_ADX
Definition codec_id.h:379
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition packet.h:56
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition packet.c:252
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition samplefmt.h:64
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
#define AV_RB32(p)
#define AV_RB16(p)
unsigned offset
Definition libaomenc.c:763
static av_cold void adx_decode_flush(AVCodecContext *avctx)
Definition adxdec.c:255
static int adx_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition adxdec.c:160
const FFCodec ff_adpcm_adx_decoder
Definition adxdec.c:262
static int adx_decode_header(AVCodecContext *avctx, const uint8_t *buf, int bufsize, int *header_size, int *coeff)
Decode ADX stream header.
Definition adxdec.c:50
static int adx_decode(ADXContext *c, int16_t *out, int offset, const uint8_t *in, int ch)
Decode 32 samples from 18 bytes.
Definition adxdec.c:130
static av_cold int adx_decode_init(AVCodecContext *avctx)
Definition adxdec.c:102
Macro definitions for various function/variable attributes.
#define av_cold
Definition attributes.h:117
enum AVChannelOrder order
Channel order used in this layout.
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
int sample_rate
samples per second
Definition avcodec.h:1040
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
#define avpriv_request_sample(...)
#define av_log(a,...)
static const double coeff[2][5]
static double c[64]