FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "get_bits.h"
26 #include "internal.h"
27 
28 /**
29  * @file
30  * SEGA CRI adx codecs.
31  *
32  * Reference documents:
33  * http://ku-www.ss.titech.ac.jp/~yatsushi/adx.html
34  * adx2wav & wav2adx http://www.geocities.co.jp/Playtown/2004/
35  */
36 
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 
61 /**
62  * Decode 32 samples from 18 bytes.
63  *
64  * A 16-bit scalar value is applied to 32 residuals, which then have a
65  * 2nd-order LPC filter applied to it to form the output signal for a single
66  * channel.
67  */
68 static int adx_decode(ADXContext *c, int16_t *out, int offset,
69  const uint8_t *in, int ch)
70 {
71  ADXChannelState *prev = &c->prev[ch];
72  GetBitContext gb;
73  int scale = AV_RB16(in);
74  int i;
75  int s0, s1, s2, d;
76 
77  /* check if this is an EOF packet */
78  if (scale & 0x8000)
79  return -1;
80 
81  init_get_bits(&gb, in + 2, (BLOCK_SIZE - 2) * 8);
82  out += offset;
83  s1 = prev->s1;
84  s2 = prev->s2;
85  for (i = 0; i < BLOCK_SAMPLES; i++) {
86  d = get_sbits(&gb, 4);
87  s0 = ((d << COEFF_BITS) * scale + c->coeff[0] * s1 + c->coeff[1] * s2) >> COEFF_BITS;
88  s2 = s1;
89  s1 = av_clip_int16(s0);
90  *out++ = s1;
91  }
92  prev->s1 = s1;
93  prev->s2 = s2;
94 
95  return 0;
96 }
97 
98 static int adx_decode_frame(AVCodecContext *avctx, void *data,
99  int *got_frame_ptr, AVPacket *avpkt)
100 {
101  int buf_size = avpkt->size;
102  ADXContext *c = avctx->priv_data;
103  int16_t **samples;
104  int samples_offset;
105  const uint8_t *buf = avpkt->data;
106  const uint8_t *buf_end = buf + avpkt->size;
107  int num_blocks, ch, ret;
108 
109  if (c->eof) {
110  *got_frame_ptr = 0;
111  return buf_size;
112  }
113 
114  if (!c->header_parsed && buf_size >= 2 && AV_RB16(buf) == 0x8000) {
115  int header_size;
116  if ((ret = avpriv_adx_decode_header(avctx, buf, buf_size, &header_size,
117  c->coeff)) < 0) {
118  av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n");
119  return AVERROR_INVALIDDATA;
120  }
121  c->channels = avctx->channels;
122  c->header_parsed = 1;
123  if (buf_size < header_size)
124  return AVERROR_INVALIDDATA;
125  buf += header_size;
126  buf_size -= header_size;
127  }
128  if (!c->header_parsed)
129  return AVERROR_INVALIDDATA;
130 
131  /* calculate number of blocks in the packet */
132  num_blocks = buf_size / (BLOCK_SIZE * c->channels);
133 
134  /* if the packet is not an even multiple of BLOCK_SIZE, check for an EOF
135  packet */
136  if (!num_blocks || buf_size % (BLOCK_SIZE * avctx->channels)) {
137  if (buf_size >= 4 && (AV_RB16(buf) & 0x8000)) {
138  c->eof = 1;
139  *got_frame_ptr = 0;
140  return avpkt->size;
141  }
142  return AVERROR_INVALIDDATA;
143  }
144 
145  /* get output buffer */
146  c->frame.nb_samples = num_blocks * BLOCK_SAMPLES;
147  if ((ret = ff_get_buffer(avctx, &c->frame)) < 0) {
148  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
149  return ret;
150  }
151  samples = (int16_t **)c->frame.extended_data;
152  samples_offset = 0;
153 
154  while (num_blocks--) {
155  for (ch = 0; ch < c->channels; ch++) {
156  if (buf_end - buf < BLOCK_SIZE || adx_decode(c, samples[ch], samples_offset, buf, ch)) {
157  c->eof = 1;
158  buf = avpkt->data + avpkt->size;
159  break;
160  }
161  buf_size -= BLOCK_SIZE;
162  buf += BLOCK_SIZE;
163  }
164  samples_offset += BLOCK_SAMPLES;
165  }
166 
167  *got_frame_ptr = 1;
168  *(AVFrame *)data = c->frame;
169 
170  return buf - avpkt->data;
171 }
172 
173 static void adx_decode_flush(AVCodecContext *avctx)
174 {
175  ADXContext *c = avctx->priv_data;
176  memset(c->prev, 0, sizeof(c->prev));
177  c->eof = 0;
178 }
179 
181  .name = "adpcm_adx",
182  .type = AVMEDIA_TYPE_AUDIO,
183  .id = AV_CODEC_ID_ADPCM_ADX,
184  .priv_data_size = sizeof(ADXContext),
188  .capabilities = CODEC_CAP_DR1,
189  .long_name = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"),
190  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
192 };