Move doxygen comments for av_gettime from utils.c to avformat.h.
[FFMpeg-mirror/ffmpeg-vdpau.git] / libavcodec / adxdec.c
blob5512d7fc562d552a15f311fc62166b10793fe2d2
1 /*
2 * ADX ADPCM codecs
3 * Copyright (c) 2001,2003 BERO
5 * This file is part of FFmpeg.
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.
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.
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
21 #include "avcodec.h"
22 #include "adx.h"
24 /**
25 * @file adx.c
26 * SEGA CRI adx codecs.
28 * Reference documents:
29 * http://ku-www.ss.titech.ac.jp/~yatsushi/adx.html
30 * adx2wav & wav2adx http://www.geocities.co.jp/Playtown/2004/
33 static av_cold int adx_decode_init(AVCodecContext *avctx)
35 avctx->sample_fmt = SAMPLE_FMT_S16;
36 return 0;
39 /* 18 bytes <-> 32 samples */
41 static void adx_decode(short *out,const unsigned char *in,PREV *prev)
43 int scale = AV_RB16(in);
44 int i;
45 int s0,s1,s2,d;
47 // printf("%x ",scale);
49 in+=2;
50 s1 = prev->s1;
51 s2 = prev->s2;
52 for(i=0;i<16;i++) {
53 d = in[i];
54 // d>>=4; if (d&8) d-=16;
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;
61 d = in[i];
62 //d&=15; if (d&8) d-=16;
63 d = ((signed char)(d<<4) >> 4);
64 s0 = (BASEVOL*d*scale + SCALE1*s1 - SCALE2*s2)>>14;
65 s2 = s1;
66 s1 = av_clip_int16(s0);
67 *out++=s1;
69 prev->s1 = s1;
70 prev->s2 = s2;
74 static void adx_decode_stereo(short *out,const unsigned char *in,PREV *prev)
76 short tmp[32*2];
77 int i;
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];
87 /* return data offset or 0 */
88 static int adx_decode_header(AVCodecContext *avctx,const unsigned char *buf,size_t bufsize)
90 int offset;
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;
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;
100 return offset;
103 static int adx_decode_frame(AVCodecContext *avctx,
104 void *data, int *data_size,
105 const uint8_t *buf0, int buf_size)
107 ADXContext *c = avctx->priv_data;
108 short *samples = data;
109 const uint8_t *buf = buf0;
110 int rest = buf_size;
112 if (!c->header_parsed) {
113 int hdrsize = adx_decode_header(avctx,buf,rest);
114 if (hdrsize==0) return -1;
115 c->header_parsed = 1;
116 buf += hdrsize;
117 rest -= hdrsize;
120 /* 18 bytes of data are expanded into 32*2 bytes of audio,
121 so guard against buffer overflows */
122 if(rest/18 > *data_size/64)
123 rest = (*data_size/64) * 18;
125 if (c->in_temp) {
126 int copysize = 18*avctx->channels - c->in_temp;
127 memcpy(c->dec_temp+c->in_temp,buf,copysize);
128 rest -= copysize;
129 buf += copysize;
130 if (avctx->channels==1) {
131 adx_decode(samples,c->dec_temp,c->prev);
132 samples += 32;
133 } else {
134 adx_decode_stereo(samples,c->dec_temp,c->prev);
135 samples += 32*2;
139 if (avctx->channels==1) {
140 while(rest>=18) {
141 adx_decode(samples,buf,c->prev);
142 rest-=18;
143 buf+=18;
144 samples+=32;
146 } else {
147 while(rest>=18*2) {
148 adx_decode_stereo(samples,buf,c->prev);
149 rest-=18*2;
150 buf+=18*2;
151 samples+=32*2;
155 c->in_temp = rest;
156 if (rest) {
157 memcpy(c->dec_temp,buf,rest);
158 buf+=rest;
160 *data_size = (uint8_t*)samples - (uint8_t*)data;
161 // printf("%d:%d ",buf-buf0,*data_size); fflush(stdout);
162 return buf-buf0;
165 AVCodec adpcm_adx_decoder = {
166 "adpcm_adx",
167 CODEC_TYPE_AUDIO,
168 CODEC_ID_ADPCM_ADX,
169 sizeof(ADXContext),
170 adx_decode_init,
171 NULL,
172 NULL,
173 adx_decode_frame,
174 .long_name = NULL_IF_CONFIG_SMALL("SEGA CRI ADX"),