FFmpeg
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 
22 #include "libavutil/intreadwrite.h"
23 #include "avcodec.h"
24 #include "adx.h"
25 #include "codec_internal.h"
26 #include "decode.h"
27 #include "get_bits.h"
28 
29 /**
30  * @file
31  * SEGA CRI adx codecs.
32  *
33  * Reference documents:
34  * http://ku-www.ss.titech.ac.jp/~yatsushi/adx.html
35  * adx2wav & wav2adx http://www.geocities.co.jp/Playtown/2004/
36  */
37 
38 /**
39  * Decode ADX stream header.
40  * Sets avctx->channels and avctx->sample_rate.
41  *
42  * @param avctx codec context
43  * @param buf header data
44  * @param bufsize data size, should be at least 24 bytes
45  * @param[out] header_size size of ADX header
46  * @param[out] coeff 2 LPC coefficients, can be NULL
47  * @return data offset or negative error code if header is invalid
48  */
49 static int adx_decode_header(AVCodecContext *avctx, const uint8_t *buf,
50  int bufsize, int *header_size, int *coeff)
51 {
52  int offset, cutoff, channels;
53 
54  if (bufsize < 24)
55  return AVERROR_INVALIDDATA;
56 
57  if (AV_RB16(buf) != 0x8000)
58  return AVERROR_INVALIDDATA;
59  offset = AV_RB16(buf + 2) + 4;
60 
61  /* if copyright string is within the provided data, validate it */
62  if (bufsize >= offset && offset >= 6 && memcmp(buf + offset - 6, "(c)CRI", 6))
63  return AVERROR_INVALIDDATA;
64 
65  /* check for encoding=3 block_size=18, sample_size=4 */
66  if (buf[4] != 3 || buf[5] != 18 || buf[6] != 4) {
67  avpriv_request_sample(avctx, "Support for this ADX format");
68  return AVERROR_PATCHWELCOME;
69  }
70 
71  /* channels */
72  channels = buf[7];
74  return AVERROR_INVALIDDATA;
75 
76  if (avctx->ch_layout.nb_channels != channels) {
80  }
81 
82  /* sample rate */
83  avctx->sample_rate = AV_RB32(buf + 8);
84  if (avctx->sample_rate < 1 ||
85  avctx->sample_rate > INT_MAX / (channels * BLOCK_SIZE * 8))
86  return AVERROR_INVALIDDATA;
87 
88  /* bit rate */
89  avctx->bit_rate = avctx->sample_rate * channels * BLOCK_SIZE * 8 / BLOCK_SAMPLES;
90 
91  /* LPC coefficients */
92  if (coeff) {
93  cutoff = AV_RB16(buf + 16);
95  }
96 
97  *header_size = offset;
98  return 0;
99 }
100 
102 {
103  ADXContext *c = avctx->priv_data;
104  int ret, header_size;
105 
106  if (avctx->extradata_size >= 24) {
107  if ((ret = adx_decode_header(avctx, avctx->extradata,
108  avctx->extradata_size, &header_size,
109  c->coeff)) < 0) {
110  av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n");
111  return AVERROR_INVALIDDATA;
112  }
113  c->channels = avctx->ch_layout.nb_channels;
114  c->header_parsed = 1;
115  }
116 
118 
119  return 0;
120 }
121 
122 /**
123  * Decode 32 samples from 18 bytes.
124  *
125  * A 16-bit scalar value is applied to 32 residuals, which then have a
126  * 2nd-order LPC filter applied to it to form the output signal for a single
127  * channel.
128  */
129 static int adx_decode(ADXContext *c, int16_t *out, int offset,
130  const uint8_t *in, int ch)
131 {
132  ADXChannelState *prev = &c->prev[ch];
133  GetBitContext gb;
134  int scale = AV_RB16(in);
135  int i;
136  int s0, s1, s2, d;
137 
138  /* check if this is an EOF packet */
139  if (scale & 0x8000)
140  return -1;
141 
142  init_get_bits(&gb, in + 2, (BLOCK_SIZE - 2) * 8);
143  out += offset;
144  s1 = prev->s1;
145  s2 = prev->s2;
146  for (i = 0; i < BLOCK_SAMPLES; i++) {
147  d = get_sbits(&gb, 4);
148  s0 = d * scale + ((c->coeff[0] * s1 + c->coeff[1] * s2) >> COEFF_BITS);
149  s2 = s1;
150  s1 = av_clip_int16(s0);
151  *out++ = s1;
152  }
153  prev->s1 = s1;
154  prev->s2 = s2;
155 
156  return 0;
157 }
158 
160  int *got_frame_ptr, AVPacket *avpkt)
161 {
162  int buf_size = avpkt->size;
163  ADXContext *c = avctx->priv_data;
164  int16_t **samples;
165  int samples_offset;
166  const uint8_t *buf = avpkt->data;
167  const uint8_t *buf_end = buf + avpkt->size;
168  int num_blocks, ch, ret;
169  size_t new_extradata_size;
170  uint8_t *new_extradata;
171 
172  new_extradata = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA,
173  &new_extradata_size);
174  if (new_extradata && new_extradata_size > 0) {
175  int header_size;
176  if ((ret = adx_decode_header(avctx, new_extradata,
177  new_extradata_size, &header_size,
178  c->coeff)) < 0) {
179  av_log(avctx, AV_LOG_ERROR, "error parsing new ADX extradata\n");
180  return AVERROR_INVALIDDATA;
181  }
182 
183  c->eof = 0;
184  }
185 
186  if (c->eof) {
187  *got_frame_ptr = 0;
188  return buf_size;
189  }
190 
191  if (!c->header_parsed && buf_size >= 2 && AV_RB16(buf) == 0x8000) {
192  int header_size;
193  if ((ret = adx_decode_header(avctx, buf, buf_size, &header_size,
194  c->coeff)) < 0) {
195  av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n");
196  return AVERROR_INVALIDDATA;
197  }
198  c->channels = avctx->ch_layout.nb_channels;
199  c->header_parsed = 1;
200  if (buf_size < header_size)
201  return AVERROR_INVALIDDATA;
202  buf += header_size;
203  buf_size -= header_size;
204  }
205  if (!c->header_parsed)
206  return AVERROR_INVALIDDATA;
207 
208  /* calculate number of blocks in the packet */
209  num_blocks = buf_size / (BLOCK_SIZE * c->channels);
210 
211  /* if the packet is not an even multiple of BLOCK_SIZE, check for an EOF
212  packet */
213  if (!num_blocks || buf_size % (BLOCK_SIZE * c->channels)) {
214  if (buf_size >= 4 && (AV_RB16(buf) & 0x8000)) {
215  c->eof = 1;
216  *got_frame_ptr = 0;
217  return avpkt->size;
218  }
219  return AVERROR_INVALIDDATA;
220  }
221 
222  /* get output buffer */
223  frame->nb_samples = num_blocks * BLOCK_SAMPLES;
224  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
225  return ret;
226  samples = (int16_t **)frame->extended_data;
227  samples_offset = 0;
228 
229  while (num_blocks--) {
230  for (ch = 0; ch < c->channels; ch++) {
231  if (buf_end - buf < BLOCK_SIZE || adx_decode(c, samples[ch], samples_offset, buf, ch)) {
232  c->eof = 1;
233  buf = avpkt->data + avpkt->size;
234  break;
235  }
236  buf_size -= BLOCK_SIZE;
237  buf += BLOCK_SIZE;
238  }
239  if (!c->eof)
240  samples_offset += BLOCK_SAMPLES;
241  }
242 
243  frame->nb_samples = samples_offset;
244  *got_frame_ptr = 1;
245 
246  return buf - avpkt->data;
247 }
248 
249 static void adx_decode_flush(AVCodecContext *avctx)
250 {
251  ADXContext *c = avctx->priv_data;
252  memset(c->prev, 0, sizeof(c->prev));
253  c->eof = 0;
254 }
255 
257  .p.name = "adpcm_adx",
258  CODEC_LONG_NAME("SEGA CRI ADX ADPCM"),
259  .p.type = AVMEDIA_TYPE_AUDIO,
260  .p.id = AV_CODEC_ID_ADPCM_ADX,
261  .priv_data_size = sizeof(ADXContext),
264  .flush = adx_decode_flush,
265  .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF |
267  .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
269 };
ADXChannelState::s2
int s2
Definition: adx.h:35
out
FILE * out
Definition: movenc.c:55
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1056
adx_decode_flush
static void adx_decode_flush(AVCodecContext *avctx)
Definition: adxdec.c:249
AV_PKT_DATA_NEW_EXTRADATA
@ 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
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
BLOCK_SAMPLES
#define BLOCK_SAMPLES
Definition: adxdec.c:32
AVPacket::data
uint8_t * data
Definition: packet.h:539
FFCodec
Definition: codec_internal.h:127
adx_decode
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:129
AVChannelLayout::order
enum AVChannelOrder order
Channel order used in this layout.
Definition: channel_layout.h:322
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:327
ADXChannelState::s1
int s1
Definition: adx.h:35
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:514
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1071
GetBitContext
Definition: get_bits.h:108
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
av_cold
#define av_cold
Definition: attributes.h:90
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:530
ff_adpcm_adx_decoder
const FFCodec ff_adpcm_adx_decoder
Definition: adxdec.c:256
BLOCK_SIZE
#define BLOCK_SIZE
Definition: adxdec.c:31
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:311
intreadwrite.h
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_CHANNEL_ORDER_UNSPEC
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
Definition: channel_layout.h:119
get_sbits
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:320
channels
channels
Definition: aptx.h:31
decode.h
get_bits.h
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
av_clip_int16
#define av_clip_int16
Definition: common.h:115
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:501
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
adx_decode_init
static av_cold int adx_decode_init(AVCodecContext *avctx)
Definition: adxdec.c:101
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:106
ff_adx_calculate_coeffs
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
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1697
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AV_CODEC_ID_ADPCM_ADX
@ AV_CODEC_ID_ADPCM_ADX
Definition: codec_id.h:382
AVPacket::size
int size
Definition: packet.h:540
codec_internal.h
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1063
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
MAX_CHANNELS
#define MAX_CHANNELS
Definition: aac.h:33
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:64
ADXChannelState
Definition: adx.h:34
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
adx_decode_header
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:49
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:529
av_packet_get_side_data
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
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
avcodec.h
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AVCodecContext
main external API structure.
Definition: avcodec.h:451
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:440
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
ADXContext
Definition: adx.h:40
COEFF_BITS
#define COEFF_BITS
Definition: adx.h:49
adx.h
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
scale
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition: intra.c:291
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
coeff
static const double coeff[2][5]
Definition: vf_owdenoise.c:80
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
adx_decode_frame
static int adx_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: adxdec.c:159
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98