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.

400 lines
9.3KB

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