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.

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