adx.c 9.5 KB

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