3 * Copyright (c) 2001,2003 BERO
5 * This file is part of Libav.
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.
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.
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
22 #include "libavutil/intreadwrite.h"
30 * SEGA CRI adx codecs.
32 * Reference documents:
33 * http://ku-www.ss.titech.ac.jp/~yatsushi/adx.html
34 * adx2wav & wav2adx http://www.geocities.co.jp/Playtown/2004/
37 static av_cold
int adx_decode_init(AVCodecContext
*avctx
)
39 ADXContext
*c
= avctx
->priv_data
;
42 if (avctx
->extradata_size
>= 24) {
43 if ((ret
= avpriv_adx_decode_header(avctx
, avctx
->extradata
,
44 avctx
->extradata_size
, &header_size
,
46 av_log(avctx
, AV_LOG_ERROR
, "error parsing ADX header\n");
47 return AVERROR_INVALIDDATA
;
49 c
->channels
= avctx
->channels
;
53 avctx
->sample_fmt
= AV_SAMPLE_FMT_S16P
;
55 avcodec_get_frame_defaults(&c
->frame
);
56 avctx
->coded_frame
= &c
->frame
;
62 * Decode 32 samples from 18 bytes.
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
68 static int adx_decode(ADXContext
*c
, int16_t *out
, int offset
,
69 const uint8_t *in
, int ch
)
71 ADXChannelState
*prev
= &c
->prev
[ch
];
73 int scale
= AV_RB16(in
);
77 /* check if this is an EOF packet */
81 init_get_bits(&gb
, in
+ 2, (BLOCK_SIZE
- 2) * 8);
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
;
89 s1
= av_clip_int16(s0
);
98 static int adx_decode_frame(AVCodecContext
*avctx
, void *data
,
99 int *got_frame_ptr
, AVPacket
*avpkt
)
101 int buf_size
= avpkt
->size
;
102 ADXContext
*c
= avctx
->priv_data
;
105 const uint8_t *buf
= avpkt
->data
;
106 int num_blocks
, ch
, ret
;
113 if (!c
->header_parsed
&& buf_size
>= 2 && AV_RB16(buf
) == 0x8000) {
115 if ((ret
= avpriv_adx_decode_header(avctx
, buf
, buf_size
, &header_size
,
117 av_log(avctx
, AV_LOG_ERROR
, "error parsing ADX header\n");
118 return AVERROR_INVALIDDATA
;
120 c
->channels
= avctx
->channels
;
121 c
->header_parsed
= 1;
122 if (buf_size
< header_size
)
123 return AVERROR_INVALIDDATA
;
125 buf_size
-= header_size
;
127 if (!c
->header_parsed
)
128 return AVERROR_INVALIDDATA
;
130 /* calculate number of blocks in the packet */
131 num_blocks
= buf_size
/ (BLOCK_SIZE
* c
->channels
);
133 /* if the packet is not an even multiple of BLOCK_SIZE, check for an EOF
135 if (!num_blocks
|| buf_size
% (BLOCK_SIZE
* avctx
->channels
)) {
136 if (buf_size
>= 4 && (AV_RB16(buf
) & 0x8000)) {
141 return AVERROR_INVALIDDATA
;
144 /* get output buffer */
145 c
->frame
.nb_samples
= num_blocks
* BLOCK_SAMPLES
;
146 if ((ret
= ff_get_buffer(avctx
, &c
->frame
)) < 0) {
147 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
150 samples
= (int16_t **)c
->frame
.extended_data
;
153 while (num_blocks
--) {
154 for (ch
= 0; ch
< c
->channels
; ch
++) {
155 if (adx_decode(c
, samples
[ch
], samples_offset
, buf
, ch
)) {
157 buf
= avpkt
->data
+ avpkt
->size
;
160 buf_size
-= BLOCK_SIZE
;
163 samples_offset
+= BLOCK_SAMPLES
;
167 *(AVFrame
*)data
= c
->frame
;
169 return buf
- avpkt
->data
;
172 static void adx_decode_flush(AVCodecContext
*avctx
)
174 ADXContext
*c
= avctx
->priv_data
;
175 memset(c
->prev
, 0, sizeof(c
->prev
));
179 AVCodec ff_adpcm_adx_decoder
= {
181 .type
= AVMEDIA_TYPE_AUDIO
,
182 .id
= AV_CODEC_ID_ADPCM_ADX
,
183 .priv_data_size
= sizeof(ADXContext
),
184 .init
= adx_decode_init
,
185 .decode
= adx_decode_frame
,
186 .flush
= adx_decode_flush
,
187 .capabilities
= CODEC_CAP_DR1
,
188 .long_name
= NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"),
189 .sample_fmts
= (const enum AVSampleFormat
[]) { AV_SAMPLE_FMT_S16P
,
190 AV_SAMPLE_FMT_NONE
},