You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

408 lines
9.5KB

  1. /*
  2. * ADX ADPCM codecs
  3. * Copyright (c) 2001,2003 BERO
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avcodec.h"
  20. /**
  21. * @file adx.c
  22. * SEGA CRI adx codecs.
  23. *
  24. * Reference documents:
  25. * http://ku-www.ss.titech.ac.jp/~yatsushi/adx.html
  26. * adx2wav & wav2adx http://www.geocities.co.jp/Playtown/2004/
  27. */
  28. typedef struct {
  29. int s1,s2;
  30. } PREV;
  31. typedef struct {
  32. PREV prev[2];
  33. int header_parsed;
  34. unsigned char dec_temp[18*2];
  35. unsigned short enc_temp[32*2];
  36. int in_temp;
  37. } ADXContext;
  38. //#define BASEVOL 0x11e0
  39. #define BASEVOL 0x4000
  40. #define SCALE1 0x7298
  41. #define SCALE2 0x3350
  42. #define CLIP(s) if (s>32767) s=32767; else if (s<-32768) s=-32768
  43. /* 18 bytes <-> 32 samples */
  44. #ifdef CONFIG_ENCODERS
  45. static void adx_encode(unsigned char *adx,const short *wav,PREV *prev)
  46. {
  47. int scale;
  48. int i;
  49. int s0,s1,s2,d;
  50. int max=0;
  51. int min=0;
  52. int data[32];
  53. s1 = prev->s1;
  54. s2 = prev->s2;
  55. for(i=0;i<32;i++) {
  56. s0 = wav[i];
  57. d = ((s0<<14) - SCALE1*s1 + SCALE2*s2)/BASEVOL;
  58. data[i]=d;
  59. if (max<d) max=d;
  60. if (min>d) min=d;
  61. s2 = s1;
  62. s1 = s0;
  63. }
  64. prev->s1 = s1;
  65. prev->s2 = s2;
  66. /* -8..+7 */
  67. if (max==0 && min==0) {
  68. memset(adx,0,18);
  69. return;
  70. }
  71. if (max/7>-min/8) scale = max/7;
  72. else scale = -min/8;
  73. if (scale==0) scale=1;
  74. adx[0] = scale>>8;
  75. adx[1] = scale;
  76. for(i=0;i<16;i++) {
  77. adx[i+2] = ((data[i*2]/scale)<<4) | ((data[i*2+1]/scale)&0xf);
  78. }
  79. }
  80. #endif //CONFIG_ENCODERS
  81. static void adx_decode(short *out,const unsigned char *in,PREV *prev)
  82. {
  83. int scale = ((in[0]<<8)|(in[1]));
  84. int i;
  85. int s0,s1,s2,d;
  86. // printf("%x ",scale);
  87. in+=2;
  88. s1 = prev->s1;
  89. s2 = prev->s2;
  90. for(i=0;i<16;i++) {
  91. d = in[i];
  92. // d>>=4; if (d&8) d-=16;
  93. d = ((signed char)d >> 4);
  94. s0 = (BASEVOL*d*scale + SCALE1*s1 - SCALE2*s2)>>14;
  95. CLIP(s0);
  96. *out++=s0;
  97. s2 = s1;
  98. s1 = s0;
  99. d = in[i];
  100. //d&=15; if (d&8) d-=16;
  101. d = ((signed char)(d<<4) >> 4);
  102. s0 = (BASEVOL*d*scale + SCALE1*s1 - SCALE2*s2)>>14;
  103. CLIP(s0);
  104. *out++=s0;
  105. s2 = s1;
  106. s1 = s0;
  107. }
  108. prev->s1 = s1;
  109. prev->s2 = s2;
  110. }
  111. static void adx_decode_stereo(short *out,const unsigned char *in,PREV *prev)
  112. {
  113. short tmp[32*2];
  114. int i;
  115. adx_decode(tmp ,in ,prev);
  116. adx_decode(tmp+32,in+18,prev+1);
  117. for(i=0;i<32;i++) {
  118. out[i*2] = tmp[i];
  119. out[i*2+1] = tmp[i+32];
  120. }
  121. }
  122. #ifdef CONFIG_ENCODERS
  123. static void write_long(unsigned char *p,uint32_t v)
  124. {
  125. p[0] = v>>24;
  126. p[1] = v>>16;
  127. p[2] = v>>8;
  128. p[3] = v;
  129. }
  130. static int adx_encode_header(AVCodecContext *avctx,unsigned char *buf,size_t bufsize)
  131. {
  132. #if 0
  133. struct {
  134. uint32_t offset; /* 0x80000000 + sample start - 4 */
  135. unsigned char unknown1[3]; /* 03 12 04 */
  136. unsigned char channel; /* 1 or 2 */
  137. uint32_t freq;
  138. uint32_t size;
  139. uint32_t unknown2; /* 01 f4 03 00 */
  140. uint32_t unknown3; /* 00 00 00 00 */
  141. uint32_t unknown4; /* 00 00 00 00 */
  142. /* if loop
  143. unknown3 00 15 00 01
  144. unknown4 00 00 00 01
  145. long loop_start_sample;
  146. long loop_start_byte;
  147. long loop_end_sample;
  148. long loop_end_byte;
  149. long
  150. */
  151. } adxhdr; /* big endian */
  152. /* offset-6 "(c)CRI" */
  153. #endif
  154. write_long(buf+0x00,0x80000000|0x20);
  155. write_long(buf+0x04,0x03120400|avctx->channels);
  156. write_long(buf+0x08,avctx->sample_rate);
  157. write_long(buf+0x0c,0); /* FIXME: set after */
  158. write_long(buf+0x10,0x01040300);
  159. write_long(buf+0x14,0x00000000);
  160. write_long(buf+0x18,0x00000000);
  161. memcpy(buf+0x1c,"\0\0(c)CRI",8);
  162. return 0x20+4;
  163. }
  164. static int adx_decode_init(AVCodecContext *avctx);
  165. static int adx_encode_init(AVCodecContext *avctx)
  166. {
  167. if (avctx->channels > 2)
  168. return -1; /* only stereo or mono =) */
  169. avctx->frame_size = 32;
  170. avctx->coded_frame= avcodec_alloc_frame();
  171. avctx->coded_frame->key_frame= 1;
  172. // avctx->bit_rate = avctx->sample_rate*avctx->channels*18*8/32;
  173. av_log(avctx, AV_LOG_DEBUG, "adx encode init\n");
  174. adx_decode_init(avctx);
  175. return 0;
  176. }
  177. static int adx_encode_close(AVCodecContext *avctx)
  178. {
  179. av_freep(&avctx->coded_frame);
  180. return 0;
  181. }
  182. static int adx_encode_frame(AVCodecContext *avctx,
  183. uint8_t *frame, int buf_size, void *data)
  184. {
  185. ADXContext *c = avctx->priv_data;
  186. const short *samples = data;
  187. unsigned char *dst = frame;
  188. int rest = avctx->frame_size;
  189. /*
  190. input data size =
  191. ffmpeg.c: do_audio_out()
  192. frame_bytes = enc->frame_size * 2 * enc->channels;
  193. */
  194. // printf("sz=%d ",buf_size); fflush(stdout);
  195. if (!c->header_parsed) {
  196. int hdrsize = adx_encode_header(avctx,dst,buf_size);
  197. dst+=hdrsize;
  198. c->header_parsed = 1;
  199. }
  200. if (avctx->channels==1) {
  201. while(rest>=32) {
  202. adx_encode(dst,samples,c->prev);
  203. dst+=18;
  204. samples+=32;
  205. rest-=32;
  206. }
  207. } else {
  208. while(rest>=32*2) {
  209. short tmpbuf[32*2];
  210. int i;
  211. for(i=0;i<32;i++) {
  212. tmpbuf[i] = samples[i*2];
  213. tmpbuf[i+32] = samples[i*2+1];
  214. }
  215. adx_encode(dst,tmpbuf,c->prev);
  216. adx_encode(dst+18,tmpbuf+32,c->prev+1);
  217. dst+=18*2;
  218. samples+=32*2;
  219. rest-=32*2;
  220. }
  221. }
  222. return dst-frame;
  223. }
  224. #endif //CONFIG_ENCODERS
  225. static uint32_t read_long(const unsigned char *p)
  226. {
  227. return (p[0]<<24)|(p[1]<<16)|(p[2]<<8)|p[3];
  228. }
  229. int is_adx(const unsigned char *buf,size_t bufsize)
  230. {
  231. int offset;
  232. if (buf[0]!=0x80) return 0;
  233. offset = (read_long(buf)^0x80000000)+4;
  234. if (bufsize<offset || memcmp(buf+offset-6,"(c)CRI",6)) return 0;
  235. return offset;
  236. }
  237. /* return data offset or 6 */
  238. static int adx_decode_header(AVCodecContext *avctx,const unsigned char *buf,size_t bufsize)
  239. {
  240. int offset;
  241. int channels,freq,size;
  242. offset = is_adx(buf,bufsize);
  243. if (offset==0) return 0;
  244. channels = buf[7];
  245. freq = read_long(buf+8);
  246. size = read_long(buf+12);
  247. // printf("freq=%d ch=%d\n",freq,channels);
  248. avctx->sample_rate = freq;
  249. avctx->channels = channels;
  250. avctx->bit_rate = freq*channels*18*8/32;
  251. // avctx->frame_size = 18*channels;
  252. return offset;
  253. }
  254. static int adx_decode_init(AVCodecContext * avctx)
  255. {
  256. ADXContext *c = avctx->priv_data;
  257. // printf("adx_decode_init\n"); fflush(stdout);
  258. c->prev[0].s1 = 0;
  259. c->prev[0].s2 = 0;
  260. c->prev[1].s1 = 0;
  261. c->prev[1].s2 = 0;
  262. c->header_parsed = 0;
  263. c->in_temp = 0;
  264. return 0;
  265. }
  266. static void dump(unsigned char *buf,size_t len)
  267. {
  268. int i;
  269. for(i=0;i<len;i++) {
  270. if ((i&15)==0) av_log(NULL, AV_LOG_DEBUG, "%04x ",i);
  271. av_log(NULL, AV_LOG_DEBUG, "%02x ",buf[i]);
  272. if ((i&15)==15) av_log(NULL, AV_LOG_DEBUG, "\n");
  273. }
  274. av_log(NULL, AV_LOG_ERROR, "\n");
  275. }
  276. static int adx_decode_frame(AVCodecContext *avctx,
  277. void *data, int *data_size,
  278. uint8_t *buf0, int buf_size)
  279. {
  280. ADXContext *c = avctx->priv_data;
  281. short *samples = data;
  282. const uint8_t *buf = buf0;
  283. int rest = buf_size;
  284. if (!c->header_parsed) {
  285. int hdrsize = adx_decode_header(avctx,buf,rest);
  286. if (hdrsize==0) return -1;
  287. c->header_parsed = 1;
  288. buf += hdrsize;
  289. rest -= hdrsize;
  290. }
  291. if (c->in_temp) {
  292. int copysize = 18*avctx->channels - c->in_temp;
  293. memcpy(c->dec_temp+c->in_temp,buf,copysize);
  294. rest -= copysize;
  295. buf += copysize;
  296. if (avctx->channels==1) {
  297. adx_decode(samples,c->dec_temp,c->prev);
  298. samples += 32;
  299. } else {
  300. adx_decode_stereo(samples,c->dec_temp,c->prev);
  301. samples += 32*2;
  302. }
  303. }
  304. //
  305. if (avctx->channels==1) {
  306. while(rest>=18) {
  307. adx_decode(samples,buf,c->prev);
  308. rest-=18;
  309. buf+=18;
  310. samples+=32;
  311. }
  312. } else {
  313. while(rest>=18*2) {
  314. adx_decode_stereo(samples,buf,c->prev);
  315. rest-=18*2;
  316. buf+=18*2;
  317. samples+=32*2;
  318. }
  319. }
  320. //
  321. c->in_temp = rest;
  322. if (rest) {
  323. memcpy(c->dec_temp,buf,rest);
  324. buf+=rest;
  325. }
  326. *data_size = (uint8_t*)samples - (uint8_t*)data;
  327. // printf("%d:%d ",buf-buf0,*data_size); fflush(stdout);
  328. return buf-buf0;
  329. }
  330. #ifdef CONFIG_ENCODERS
  331. AVCodec adx_adpcm_encoder = {
  332. "adx_adpcm",
  333. CODEC_TYPE_AUDIO,
  334. CODEC_ID_ADPCM_ADX,
  335. sizeof(ADXContext),
  336. adx_encode_init,
  337. adx_encode_frame,
  338. adx_encode_close,
  339. NULL,
  340. };
  341. #endif //CONFIG_ENCODERS
  342. AVCodec adx_adpcm_decoder = {
  343. "adx_adpcm",
  344. CODEC_TYPE_AUDIO,
  345. CODEC_ID_ADPCM_ADX,
  346. sizeof(ADXContext),
  347. adx_decode_init,
  348. NULL,
  349. NULL,
  350. adx_decode_frame,
  351. };