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.

399 lines
9.2KB

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