Libav
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 "adx.h"
25 #include "get_bits.h"
26 #include "internal.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 
55  return 0;
56 }
57 
65 static int adx_decode(ADXContext *c, int16_t *out, int offset,
66  const uint8_t *in, int ch)
67 {
68  ADXChannelState *prev = &c->prev[ch];
69  GetBitContext gb;
70  int scale = AV_RB16(in);
71  int i;
72  int s0, s1, s2, d;
73 
74  /* check if this is an EOF packet */
75  if (scale & 0x8000)
76  return -1;
77 
78  init_get_bits(&gb, in + 2, (BLOCK_SIZE - 2) * 8);
79  out += offset;
80  s1 = prev->s1;
81  s2 = prev->s2;
82  for (i = 0; i < BLOCK_SAMPLES; i++) {
83  d = get_sbits(&gb, 4);
84  s0 = ((d << COEFF_BITS) * scale + c->coeff[0] * s1 + c->coeff[1] * s2) >> COEFF_BITS;
85  s2 = s1;
86  s1 = av_clip_int16(s0);
87  *out++ = s1;
88  }
89  prev->s1 = s1;
90  prev->s2 = s2;
91 
92  return 0;
93 }
94 
95 static int adx_decode_frame(AVCodecContext *avctx, void *data,
96  int *got_frame_ptr, AVPacket *avpkt)
97 {
98  AVFrame *frame = data;
99  int buf_size = avpkt->size;
100  ADXContext *c = avctx->priv_data;
101  int16_t **samples;
102  int samples_offset;
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  frame->nb_samples = num_blocks * BLOCK_SAMPLES;
144  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
145  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
146  return ret;
147  }
148  samples = (int16_t **)frame->extended_data;
149  samples_offset = 0;
150 
151  while (num_blocks--) {
152  for (ch = 0; ch < c->channels; ch++) {
153  if (adx_decode(c, samples[ch], samples_offset, buf, ch)) {
154  c->eof = 1;
155  buf = avpkt->data + avpkt->size;
156  break;
157  }
158  buf_size -= BLOCK_SIZE;
159  buf += BLOCK_SIZE;
160  }
161  samples_offset += BLOCK_SAMPLES;
162  }
163 
164  *got_frame_ptr = 1;
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  .long_name = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"),
179  .type = AVMEDIA_TYPE_AUDIO,
180  .id = AV_CODEC_ID_ADPCM_ADX,
181  .priv_data_size = sizeof(ADXContext),
185  .capabilities = CODEC_CAP_DR1,
186  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
188 };