adx: remove useless comments
[libav.git] / libavcodec / adxdec.c
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
26 /**
27 * @file
28 * SEGA CRI adx codecs.
29 *
30 * Reference documents:
31 * http://ku-www.ss.titech.ac.jp/~yatsushi/adx.html
32 * adx2wav & wav2adx http://www.geocities.co.jp/Playtown/2004/
33 */
34
35 static av_cold int adx_decode_init(AVCodecContext *avctx)
36 {
37 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
38 return 0;
39 }
40
41 /* 18 bytes <-> 32 samples */
42
43 static void adx_decode(int16_t *out,const unsigned char *in,
44 ADXChannelState *prev)
45 {
46 int scale = AV_RB16(in);
47 int i;
48 int s0,s1,s2,d;
49
50 in+=2;
51 s1 = prev->s1;
52 s2 = prev->s2;
53 for(i=0;i<16;i++) {
54 d = in[i];
55 d = ((signed char)d >> 4);
56 s0 = (BASEVOL*d*scale + SCALE1*s1 - SCALE2*s2)>>14;
57 s2 = s1;
58 s1 = av_clip_int16(s0);
59 *out++=s1;
60
61 d = in[i];
62 d = ((signed char)(d<<4) >> 4);
63 s0 = (BASEVOL*d*scale + SCALE1*s1 - SCALE2*s2)>>14;
64 s2 = s1;
65 s1 = av_clip_int16(s0);
66 *out++=s1;
67 }
68 prev->s1 = s1;
69 prev->s2 = s2;
70
71 }
72
73 static void adx_decode_stereo(int16_t *out,const unsigned char *in,
74 ADXChannelState *prev)
75 {
76 short tmp[32*2];
77 int i;
78
79 adx_decode(tmp ,in ,prev);
80 adx_decode(tmp+32,in+18,prev+1);
81 for(i=0;i<32;i++) {
82 out[i*2] = tmp[i];
83 out[i*2+1] = tmp[i+32];
84 }
85 }
86
87 /* return data offset or 0 */
88 static int adx_decode_header(AVCodecContext *avctx,const unsigned char *buf,size_t bufsize)
89 {
90 int offset;
91
92 if (buf[0]!=0x80) return 0;
93 offset = (AV_RB32(buf)^0x80000000)+4;
94 if (bufsize<offset || memcmp(buf+offset-6,"(c)CRI",6)) return 0;
95
96 avctx->channels = buf[7];
97 avctx->sample_rate = AV_RB32(buf+8);
98 avctx->bit_rate = avctx->sample_rate*avctx->channels*18*8/32;
99
100 return offset;
101 }
102
103 static int adx_decode_frame(AVCodecContext *avctx,
104 void *data, int *data_size,
105 AVPacket *avpkt)
106 {
107 const uint8_t *buf0 = avpkt->data;
108 int buf_size = avpkt->size;
109 ADXContext *c = avctx->priv_data;
110 int16_t *samples = data;
111 const uint8_t *buf = buf0;
112 int rest = buf_size;
113
114 if (!c->header_parsed) {
115 int hdrsize = adx_decode_header(avctx,buf,rest);
116 if (hdrsize==0) return -1;
117 c->header_parsed = 1;
118 buf += hdrsize;
119 rest -= hdrsize;
120 }
121
122 /* 18 bytes of data are expanded into 32*2 bytes of audio,
123 so guard against buffer overflows */
124 if(rest/18 > *data_size/64)
125 rest = (*data_size/64) * 18;
126
127 if (c->in_temp) {
128 int copysize = 18*avctx->channels - c->in_temp;
129 memcpy(c->dec_temp+c->in_temp,buf,copysize);
130 rest -= copysize;
131 buf += copysize;
132 if (avctx->channels==1) {
133 adx_decode(samples,c->dec_temp,c->prev);
134 samples += 32;
135 } else {
136 adx_decode_stereo(samples,c->dec_temp,c->prev);
137 samples += 32*2;
138 }
139 }
140
141 if (avctx->channels==1) {
142 while(rest>=18) {
143 adx_decode(samples,buf,c->prev);
144 rest-=18;
145 buf+=18;
146 samples+=32;
147 }
148 } else {
149 while(rest>=18*2) {
150 adx_decode_stereo(samples,buf,c->prev);
151 rest-=18*2;
152 buf+=18*2;
153 samples+=32*2;
154 }
155 }
156
157 c->in_temp = rest;
158 if (rest) {
159 memcpy(c->dec_temp,buf,rest);
160 buf+=rest;
161 }
162 *data_size = (uint8_t*)samples - (uint8_t*)data;
163 return buf-buf0;
164 }
165
166 AVCodec ff_adpcm_adx_decoder = {
167 .name = "adpcm_adx",
168 .type = AVMEDIA_TYPE_AUDIO,
169 .id = CODEC_ID_ADPCM_ADX,
170 .priv_data_size = sizeof(ADXContext),
171 .init = adx_decode_init,
172 .decode = adx_decode_frame,
173 .long_name = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"),
174 };
175