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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 "internal.h"
25 #include "adx.h"
26 #include "get_bits.h"
27 
38 {
39  ADXContext *c = avctx->priv_data;
40  int ret, header_size;
41 
42  if (avctx->extradata_size >= 24) {
43  if ((ret = avpriv_adx_decode_header(avctx, avctx->extradata,
44  avctx->extradata_size, &header_size,
45  c->coeff)) < 0) {
46  av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n");
47  return AVERROR_INVALIDDATA;
48  }
49  c->channels = avctx->channels;
50  c->header_parsed = 1;
51  }
52 
54 
56  avctx->coded_frame = &c->frame;
57 
58  return 0;
59 }
60 
68 static int adx_decode(ADXContext *c, int16_t *out, const uint8_t *in, int ch)
69 {
70  ADXChannelState *prev = &c->prev[ch];
71  GetBitContext gb;
72  int scale = AV_RB16(in);
73  int i;
74  int s0, s1, s2, d;
75 
76  /* check if this is an EOF packet */
77  if (scale & 0x8000)
78  return -1;
79 
80  init_get_bits(&gb, in + 2, (BLOCK_SIZE - 2) * 8);
81  s1 = prev->s1;
82  s2 = prev->s2;
83  for (i = 0; i < BLOCK_SAMPLES; i++) {
84  d = get_sbits(&gb, 4);
85  s0 = ((d << COEFF_BITS) * scale + c->coeff[0] * s1 + c->coeff[1] * s2) >> COEFF_BITS;
86  s2 = s1;
87  s1 = av_clip_int16(s0);
88  *out = s1;
89  out += c->channels;
90  }
91  prev->s1 = s1;
92  prev->s2 = s2;
93 
94  return 0;
95 }
96 
97 static int adx_decode_frame(AVCodecContext *avctx, void *data,
98  int *got_frame_ptr, AVPacket *avpkt)
99 {
100  int buf_size = avpkt->size;
101  ADXContext *c = avctx->priv_data;
102  int16_t *samples;
103  const uint8_t *buf = avpkt->data;
104  int num_blocks, ch, ret;
105 
106  if (c->eof) {
107  *got_frame_ptr = 0;
108  return buf_size;
109  }
110 
111  if (!c->header_parsed && buf_size >= 2 && AV_RB16(buf) == 0x8000) {
112  int header_size;
113  if ((ret = avpriv_adx_decode_header(avctx, buf, buf_size, &header_size,
114  c->coeff)) < 0) {
115  av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n");
116  return AVERROR_INVALIDDATA;
117  }
118  c->channels = avctx->channels;
119  c->header_parsed = 1;
120  if (buf_size < header_size)
121  return AVERROR_INVALIDDATA;
122  buf += header_size;
123  buf_size -= header_size;
124  }
125  if (!c->header_parsed)
126  return AVERROR_INVALIDDATA;
127 
128  /* calculate number of blocks in the packet */
129  num_blocks = buf_size / (BLOCK_SIZE * c->channels);
130 
131  /* if the packet is not an even multiple of BLOCK_SIZE, check for an EOF
132  packet */
133  if (!num_blocks || buf_size % (BLOCK_SIZE * avctx->channels)) {
134  if (buf_size >= 4 && (AV_RB16(buf) & 0x8000)) {
135  c->eof = 1;
136  *got_frame_ptr = 0;
137  return avpkt->size;
138  }
139  return AVERROR_INVALIDDATA;
140  }
141 
142  /* get output buffer */
143  c->frame.nb_samples = num_blocks * BLOCK_SAMPLES;
144  if ((ret = ff_get_buffer(avctx, &c->frame)) < 0) {
145  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
146  return ret;
147  }
148  samples = (int16_t *)c->frame.data[0];
149 
150  while (num_blocks--) {
151  for (ch = 0; ch < c->channels; ch++) {
152  if (adx_decode(c, samples + ch, buf, ch)) {
153  c->eof = 1;
154  buf = avpkt->data + avpkt->size;
155  break;
156  }
157  buf_size -= BLOCK_SIZE;
158  buf += BLOCK_SIZE;
159  }
160  samples += BLOCK_SAMPLES * c->channels;
161  }
162 
163  *got_frame_ptr = 1;
164  *(AVFrame *)data = c->frame;
165 
166  return buf - avpkt->data;
167 }
168 
169 static void adx_decode_flush(AVCodecContext *avctx)
170 {
171  ADXContext *c = avctx->priv_data;
172  memset(c->prev, 0, sizeof(c->prev));
173  c->eof = 0;
174 }
175 
177  .name = "adpcm_adx",
178  .type = AVMEDIA_TYPE_AUDIO,
179  .id = CODEC_ID_ADPCM_ADX,
180  .priv_data_size = sizeof(ADXContext),
184  .capabilities = CODEC_CAP_DR1,
185  .long_name = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"),
186 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
AV_WL32 AV_WL24 AV_WL16 AV_WB32 AV_WB24 AV_RB16
Definition: bytestream.h:89
Audio Video Frame.
Definition: avcodec.h:985
static short * samples
Definition: ffmpeg.c:233
static av_cold int adx_decode_init(AVCodecContext *avctx)
Definition: adxdec.c:37
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2000
int size
Definition: avcodec.h:909
int coeff[2]
Definition: adx.h:49
signed 16 bits
Definition: samplefmt.h:30
AVCodec.
Definition: avcodec.h:3189
#define BLOCK_SAMPLES
Definition: adxdec.c:32
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:223
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1464
#define av_cold
Definition: attributes.h:71
AVFrame frame
Definition: adx.h:43
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1387
const char data[16]
Definition: mxf.c:60
uint8_t * data
Definition: avcodec.h:908
static int adx_decode(ADXContext *c, int16_t *out, const uint8_t *in, int ch)
Decode 32 samples from 18 bytes.
Definition: adxdec.c:68
bitstream reader API header.
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:336
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:719
#define COEFF_BITS
Definition: adx.h:52
#define s2
Definition: regdef.h:39
#define BLOCK_SIZE
Definition: adxdec.c:31
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
#define s0
Definition: regdef.h:37
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
const char * name
Name of the codec implementation.
Definition: avcodec.h:3196
static void adx_decode_flush(AVCodecContext *avctx)
Definition: adxdec.c:169
int header_parsed
Definition: adx.h:46
static int adx_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: adxdec.c:97
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Definition: utils.c:1867
external API header
main external API structure.
Definition: avcodec.h:1329
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
int extradata_size
Definition: avcodec.h:1388
Definition: adx.h:42
int s2
Definition: adx.h:39
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:354
#define s1
Definition: regdef.h:38
static const uint16_t scale[4]
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
int avpriv_adx_decode_header(AVCodecContext *avctx, const uint8_t *buf, int bufsize, int *header_size, int *coeff)
Decode ADX stream header.
Definition: adx.c:37
common internal api header.
static av_cold void flush(AVCodecContext *avctx)
Flush (reset) the frame ID after seeking.
Definition: alsdec.c:1779
AVCodec ff_adpcm_adx_decoder
Definition: adxdec.c:176
int eof
Definition: adx.h:47
int s1
Definition: adx.h:39
void * priv_data
Definition: avcodec.h:1531
ADXChannelState prev[2]
Definition: adx.h:45
int channels
number of audio channels
Definition: avcodec.h:1457
void avcodec_get_frame_defaults(AVFrame *pic)
Set the fields of the given AVFrame to default values.
Definition: utils.c:609
SEGA CRI adx codecs.
int nb_samples
number of audio samples (per channel) described by this frame
Definition: avcodec.h:1265
int channels
Definition: adx.h:44