adxenc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #include "adx.h"
  23. /**
  24. * @file adx.c
  25. * SEGA CRI adx codecs.
  26. *
  27. * Reference documents:
  28. * http://ku-www.ss.titech.ac.jp/~yatsushi/adx.html
  29. * adx2wav & wav2adx http://www.geocities.co.jp/Playtown/2004/
  30. */
  31. /* 18 bytes <-> 32 samples */
  32. static void adx_encode(unsigned char *adx,const short *wav,PREV *prev)
  33. {
  34. int scale;
  35. int i;
  36. int s0,s1,s2,d;
  37. int max=0;
  38. int min=0;
  39. int data[32];
  40. s1 = prev->s1;
  41. s2 = prev->s2;
  42. for(i=0;i<32;i++) {
  43. s0 = wav[i];
  44. d = ((s0<<14) - SCALE1*s1 + SCALE2*s2)/BASEVOL;
  45. data[i]=d;
  46. if (max<d) max=d;
  47. if (min>d) min=d;
  48. s2 = s1;
  49. s1 = s0;
  50. }
  51. prev->s1 = s1;
  52. prev->s2 = s2;
  53. /* -8..+7 */
  54. if (max==0 && min==0) {
  55. memset(adx,0,18);
  56. return;
  57. }
  58. if (max/7>-min/8) scale = max/7;
  59. else scale = -min/8;
  60. if (scale==0) scale=1;
  61. AV_WB16(adx, scale);
  62. for(i=0;i<16;i++) {
  63. adx[i+2] = ((data[i*2]/scale)<<4) | ((data[i*2+1]/scale)&0xf);
  64. }
  65. }
  66. static int adx_encode_header(AVCodecContext *avctx,unsigned char *buf,size_t bufsize)
  67. {
  68. #if 0
  69. struct {
  70. uint32_t offset; /* 0x80000000 + sample start - 4 */
  71. unsigned char unknown1[3]; /* 03 12 04 */
  72. unsigned char channel; /* 1 or 2 */
  73. uint32_t freq;
  74. uint32_t size;
  75. uint32_t unknown2; /* 01 f4 03 00 */
  76. uint32_t unknown3; /* 00 00 00 00 */
  77. uint32_t unknown4; /* 00 00 00 00 */
  78. /* if loop
  79. unknown3 00 15 00 01
  80. unknown4 00 00 00 01
  81. long loop_start_sample;
  82. long loop_start_byte;
  83. long loop_end_sample;
  84. long loop_end_byte;
  85. long
  86. */
  87. } adxhdr; /* big endian */
  88. /* offset-6 "(c)CRI" */
  89. #endif
  90. AV_WB32(buf+0x00,0x80000000|0x20);
  91. AV_WB32(buf+0x04,0x03120400|avctx->channels);
  92. AV_WB32(buf+0x08,avctx->sample_rate);
  93. AV_WB32(buf+0x0c,0); /* FIXME: set after */
  94. AV_WB32(buf+0x10,0x01040300);
  95. AV_WB32(buf+0x14,0x00000000);
  96. AV_WB32(buf+0x18,0x00000000);
  97. memcpy(buf+0x1c,"\0\0(c)CRI",8);
  98. return 0x20+4;
  99. }
  100. static av_cold int adx_encode_init(AVCodecContext *avctx)
  101. {
  102. if (avctx->channels > 2)
  103. return -1; /* only stereo or mono =) */
  104. avctx->frame_size = 32;
  105. avctx->coded_frame= avcodec_alloc_frame();
  106. avctx->coded_frame->key_frame= 1;
  107. // avctx->bit_rate = avctx->sample_rate*avctx->channels*18*8/32;
  108. av_log(avctx, AV_LOG_DEBUG, "adx encode init\n");
  109. return 0;
  110. }
  111. static av_cold int adx_encode_close(AVCodecContext *avctx)
  112. {
  113. av_freep(&avctx->coded_frame);
  114. return 0;
  115. }
  116. static int adx_encode_frame(AVCodecContext *avctx,
  117. uint8_t *frame, int buf_size, void *data)
  118. {
  119. ADXContext *c = avctx->priv_data;
  120. const short *samples = data;
  121. unsigned char *dst = frame;
  122. int rest = avctx->frame_size;
  123. /*
  124. input data size =
  125. ffmpeg.c: do_audio_out()
  126. frame_bytes = enc->frame_size * 2 * enc->channels;
  127. */
  128. // printf("sz=%d ",buf_size); fflush(stdout);
  129. if (!c->header_parsed) {
  130. int hdrsize = adx_encode_header(avctx,dst,buf_size);
  131. dst+=hdrsize;
  132. c->header_parsed = 1;
  133. }
  134. if (avctx->channels==1) {
  135. while(rest>=32) {
  136. adx_encode(dst,samples,c->prev);
  137. dst+=18;
  138. samples+=32;
  139. rest-=32;
  140. }
  141. } else {
  142. while(rest>=32*2) {
  143. short tmpbuf[32*2];
  144. int i;
  145. for(i=0;i<32;i++) {
  146. tmpbuf[i] = samples[i*2];
  147. tmpbuf[i+32] = samples[i*2+1];
  148. }
  149. adx_encode(dst,tmpbuf,c->prev);
  150. adx_encode(dst+18,tmpbuf+32,c->prev+1);
  151. dst+=18*2;
  152. samples+=32*2;
  153. rest-=32*2;
  154. }
  155. }
  156. return dst-frame;
  157. }
  158. AVCodec adpcm_adx_encoder = {
  159. "adpcm_adx",
  160. CODEC_TYPE_AUDIO,
  161. CODEC_ID_ADPCM_ADX,
  162. sizeof(ADXContext),
  163. adx_encode_init,
  164. adx_encode_frame,
  165. adx_encode_close,
  166. NULL,
  167. .long_name = "SEGA CRI ADX",
  168. };