adpcm.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662
  1. /*
  2. * ADPCM codecs
  3. * Copyright (c) 2001-2003 The ffmpeg Project
  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 "bitstream.h"
  23. #include "bytestream.h"
  24. /**
  25. * @file adpcm.c
  26. * ADPCM codecs.
  27. * First version by Francois Revol (revol@free.fr)
  28. * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
  29. * by Mike Melanson (melanson@pcisys.net)
  30. * CD-ROM XA ADPCM codec by BERO
  31. * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
  32. * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
  33. * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
  34. * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
  35. * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
  36. * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
  37. * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
  38. *
  39. * Features and limitations:
  40. *
  41. * Reference documents:
  42. * http://www.pcisys.net/~melanson/codecs/simpleaudio.html
  43. * http://www.geocities.com/SiliconValley/8682/aud3.txt
  44. * http://openquicktime.sourceforge.net/plugins.htm
  45. * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html
  46. * http://www.cs.ucla.edu/~leec/mediabench/applications.html
  47. * SoX source code http://home.sprynet.com/~cbagwell/sox.html
  48. *
  49. * CD-ROM XA:
  50. * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html
  51. * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html
  52. * readstr http://www.geocities.co.jp/Playtown/2004/
  53. */
  54. #define BLKSIZE 1024
  55. /* step_table[] and index_table[] are from the ADPCM reference source */
  56. /* This is the index table: */
  57. static const int index_table[16] = {
  58. -1, -1, -1, -1, 2, 4, 6, 8,
  59. -1, -1, -1, -1, 2, 4, 6, 8,
  60. };
  61. /**
  62. * This is the step table. Note that many programs use slight deviations from
  63. * this table, but such deviations are negligible:
  64. */
  65. static const int step_table[89] = {
  66. 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
  67. 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
  68. 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
  69. 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
  70. 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
  71. 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
  72. 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
  73. 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
  74. 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
  75. };
  76. /* These are for MS-ADPCM */
  77. /* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */
  78. static const int AdaptationTable[] = {
  79. 230, 230, 230, 230, 307, 409, 512, 614,
  80. 768, 614, 512, 409, 307, 230, 230, 230
  81. };
  82. static const int AdaptCoeff1[] = {
  83. 256, 512, 0, 192, 240, 460, 392
  84. };
  85. static const int AdaptCoeff2[] = {
  86. 0, -256, 0, 64, 0, -208, -232
  87. };
  88. /* These are for CD-ROM XA ADPCM */
  89. static const int xa_adpcm_table[5][2] = {
  90. { 0, 0 },
  91. { 60, 0 },
  92. { 115, -52 },
  93. { 98, -55 },
  94. { 122, -60 }
  95. };
  96. static const int ea_adpcm_table[] = {
  97. 0, 240, 460, 392, 0, 0, -208, -220, 0, 1,
  98. 3, 4, 7, 8, 10, 11, 0, -1, -3, -4
  99. };
  100. static const int ct_adpcm_table[8] = {
  101. 0x00E6, 0x00E6, 0x00E6, 0x00E6,
  102. 0x0133, 0x0199, 0x0200, 0x0266
  103. };
  104. // padded to zero where table size is less then 16
  105. static const int swf_index_tables[4][16] = {
  106. /*2*/ { -1, 2 },
  107. /*3*/ { -1, -1, 2, 4 },
  108. /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
  109. /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
  110. };
  111. static const int yamaha_indexscale[] = {
  112. 230, 230, 230, 230, 307, 409, 512, 614,
  113. 230, 230, 230, 230, 307, 409, 512, 614
  114. };
  115. static const int yamaha_difflookup[] = {
  116. 1, 3, 5, 7, 9, 11, 13, 15,
  117. -1, -3, -5, -7, -9, -11, -13, -15
  118. };
  119. /* end of tables */
  120. typedef struct ADPCMChannelStatus {
  121. int predictor;
  122. short int step_index;
  123. int step;
  124. /* for encoding */
  125. int prev_sample;
  126. /* MS version */
  127. short sample1;
  128. short sample2;
  129. int coeff1;
  130. int coeff2;
  131. int idelta;
  132. } ADPCMChannelStatus;
  133. typedef struct ADPCMContext {
  134. int channel; /* for stereo MOVs, decode left, then decode right, then tell it's decoded */
  135. ADPCMChannelStatus status[6];
  136. } ADPCMContext;
  137. /* XXX: implement encoding */
  138. #ifdef CONFIG_ENCODERS
  139. static int adpcm_encode_init(AVCodecContext *avctx)
  140. {
  141. if (avctx->channels > 2)
  142. return -1; /* only stereo or mono =) */
  143. switch(avctx->codec->id) {
  144. case CODEC_ID_ADPCM_IMA_WAV:
  145. avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
  146. /* and we have 4 bytes per channel overhead */
  147. avctx->block_align = BLKSIZE;
  148. /* seems frame_size isn't taken into account... have to buffer the samples :-( */
  149. break;
  150. case CODEC_ID_ADPCM_IMA_QT:
  151. avctx->frame_size = 64;
  152. avctx->block_align = 34 * avctx->channels;
  153. break;
  154. case CODEC_ID_ADPCM_MS:
  155. avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2; /* each 16 bits sample gives one nibble */
  156. /* and we have 7 bytes per channel overhead */
  157. avctx->block_align = BLKSIZE;
  158. break;
  159. case CODEC_ID_ADPCM_YAMAHA:
  160. avctx->frame_size = BLKSIZE * avctx->channels;
  161. avctx->block_align = BLKSIZE;
  162. break;
  163. case CODEC_ID_ADPCM_SWF:
  164. if (avctx->sample_rate != 11025 &&
  165. avctx->sample_rate != 22050 &&
  166. avctx->sample_rate != 44100) {
  167. av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, 22050 or 44100\n");
  168. return -1;
  169. }
  170. avctx->frame_size = 512 * (avctx->sample_rate / 11025);
  171. break;
  172. default:
  173. return -1;
  174. break;
  175. }
  176. avctx->coded_frame= avcodec_alloc_frame();
  177. avctx->coded_frame->key_frame= 1;
  178. return 0;
  179. }
  180. static int adpcm_encode_close(AVCodecContext *avctx)
  181. {
  182. av_freep(&avctx->coded_frame);
  183. return 0;
  184. }
  185. static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
  186. {
  187. int delta = sample - c->prev_sample;
  188. int nibble = FFMIN(7, abs(delta)*4/step_table[c->step_index]) + (delta<0)*8;
  189. c->prev_sample += ((step_table[c->step_index] * yamaha_difflookup[nibble]) / 8);
  190. c->prev_sample = av_clip_int16(c->prev_sample);
  191. c->step_index = av_clip(c->step_index + index_table[nibble], 0, 88);
  192. return nibble;
  193. }
  194. static inline unsigned char adpcm_ms_compress_sample(ADPCMChannelStatus *c, short sample)
  195. {
  196. int predictor, nibble, bias;
  197. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
  198. nibble= sample - predictor;
  199. if(nibble>=0) bias= c->idelta/2;
  200. else bias=-c->idelta/2;
  201. nibble= (nibble + bias) / c->idelta;
  202. nibble= av_clip(nibble, -8, 7)&0x0F;
  203. predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  204. c->sample2 = c->sample1;
  205. c->sample1 = av_clip_int16(predictor);
  206. c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
  207. if (c->idelta < 16) c->idelta = 16;
  208. return nibble;
  209. }
  210. static inline unsigned char adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, short sample)
  211. {
  212. int nibble, delta;
  213. if(!c->step) {
  214. c->predictor = 0;
  215. c->step = 127;
  216. }
  217. delta = sample - c->predictor;
  218. nibble = FFMIN(7, abs(delta)*4/c->step) + (delta<0)*8;
  219. c->predictor += ((c->step * yamaha_difflookup[nibble]) / 8);
  220. c->predictor = av_clip_int16(c->predictor);
  221. c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
  222. c->step = av_clip(c->step, 127, 24567);
  223. return nibble;
  224. }
  225. typedef struct TrellisPath {
  226. int nibble;
  227. int prev;
  228. } TrellisPath;
  229. typedef struct TrellisNode {
  230. uint32_t ssd;
  231. int path;
  232. int sample1;
  233. int sample2;
  234. int step;
  235. } TrellisNode;
  236. static void adpcm_compress_trellis(AVCodecContext *avctx, const short *samples,
  237. uint8_t *dst, ADPCMChannelStatus *c, int n)
  238. {
  239. #define FREEZE_INTERVAL 128
  240. //FIXME 6% faster if frontier is a compile-time constant
  241. const int frontier = 1 << avctx->trellis;
  242. const int stride = avctx->channels;
  243. const int version = avctx->codec->id;
  244. const int max_paths = frontier*FREEZE_INTERVAL;
  245. TrellisPath paths[max_paths], *p;
  246. TrellisNode node_buf[2][frontier];
  247. TrellisNode *nodep_buf[2][frontier];
  248. TrellisNode **nodes = nodep_buf[0]; // nodes[] is always sorted by .ssd
  249. TrellisNode **nodes_next = nodep_buf[1];
  250. int pathn = 0, froze = -1, i, j, k;
  251. assert(!(max_paths&(max_paths-1)));
  252. memset(nodep_buf, 0, sizeof(nodep_buf));
  253. nodes[0] = &node_buf[1][0];
  254. nodes[0]->ssd = 0;
  255. nodes[0]->path = 0;
  256. nodes[0]->step = c->step_index;
  257. nodes[0]->sample1 = c->sample1;
  258. nodes[0]->sample2 = c->sample2;
  259. if((version == CODEC_ID_ADPCM_IMA_WAV) || (version == CODEC_ID_ADPCM_IMA_QT) || (version == CODEC_ID_ADPCM_SWF))
  260. nodes[0]->sample1 = c->prev_sample;
  261. if(version == CODEC_ID_ADPCM_MS)
  262. nodes[0]->step = c->idelta;
  263. if(version == CODEC_ID_ADPCM_YAMAHA) {
  264. if(c->step == 0) {
  265. nodes[0]->step = 127;
  266. nodes[0]->sample1 = 0;
  267. } else {
  268. nodes[0]->step = c->step;
  269. nodes[0]->sample1 = c->predictor;
  270. }
  271. }
  272. for(i=0; i<n; i++) {
  273. TrellisNode *t = node_buf[i&1];
  274. TrellisNode **u;
  275. int sample = samples[i*stride];
  276. memset(nodes_next, 0, frontier*sizeof(TrellisNode*));
  277. for(j=0; j<frontier && nodes[j]; j++) {
  278. // higher j have higher ssd already, so they're unlikely to use a suboptimal next sample too
  279. const int range = (j < frontier/2) ? 1 : 0;
  280. const int step = nodes[j]->step;
  281. int nidx;
  282. if(version == CODEC_ID_ADPCM_MS) {
  283. const int predictor = ((nodes[j]->sample1 * c->coeff1) + (nodes[j]->sample2 * c->coeff2)) / 256;
  284. const int div = (sample - predictor) / step;
  285. const int nmin = av_clip(div-range, -8, 6);
  286. const int nmax = av_clip(div+range, -7, 7);
  287. for(nidx=nmin; nidx<=nmax; nidx++) {
  288. const int nibble = nidx & 0xf;
  289. int dec_sample = predictor + nidx * step;
  290. #define STORE_NODE(NAME, STEP_INDEX)\
  291. int d;\
  292. uint32_t ssd;\
  293. dec_sample = av_clip_int16(dec_sample);\
  294. d = sample - dec_sample;\
  295. ssd = nodes[j]->ssd + d*d;\
  296. if(nodes_next[frontier-1] && ssd >= nodes_next[frontier-1]->ssd)\
  297. continue;\
  298. /* Collapse any two states with the same previous sample value. \
  299. * One could also distinguish states by step and by 2nd to last
  300. * sample, but the effects of that are negligible. */\
  301. for(k=0; k<frontier && nodes_next[k]; k++) {\
  302. if(dec_sample == nodes_next[k]->sample1) {\
  303. assert(ssd >= nodes_next[k]->ssd);\
  304. goto next_##NAME;\
  305. }\
  306. }\
  307. for(k=0; k<frontier; k++) {\
  308. if(!nodes_next[k] || ssd < nodes_next[k]->ssd) {\
  309. TrellisNode *u = nodes_next[frontier-1];\
  310. if(!u) {\
  311. assert(pathn < max_paths);\
  312. u = t++;\
  313. u->path = pathn++;\
  314. }\
  315. u->ssd = ssd;\
  316. u->step = STEP_INDEX;\
  317. u->sample2 = nodes[j]->sample1;\
  318. u->sample1 = dec_sample;\
  319. paths[u->path].nibble = nibble;\
  320. paths[u->path].prev = nodes[j]->path;\
  321. memmove(&nodes_next[k+1], &nodes_next[k], (frontier-k-1)*sizeof(TrellisNode*));\
  322. nodes_next[k] = u;\
  323. break;\
  324. }\
  325. }\
  326. next_##NAME:;
  327. STORE_NODE(ms, FFMAX(16, (AdaptationTable[nibble] * step) >> 8));
  328. }
  329. } else if((version == CODEC_ID_ADPCM_IMA_WAV)|| (version == CODEC_ID_ADPCM_IMA_QT)|| (version == CODEC_ID_ADPCM_SWF)) {
  330. #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
  331. const int predictor = nodes[j]->sample1;\
  332. const int div = (sample - predictor) * 4 / STEP_TABLE;\
  333. int nmin = av_clip(div-range, -7, 6);\
  334. int nmax = av_clip(div+range, -6, 7);\
  335. if(nmin<=0) nmin--; /* distinguish -0 from +0 */\
  336. if(nmax<0) nmax--;\
  337. for(nidx=nmin; nidx<=nmax; nidx++) {\
  338. const int nibble = nidx<0 ? 7-nidx : nidx;\
  339. int dec_sample = predictor + (STEP_TABLE * yamaha_difflookup[nibble]) / 8;\
  340. STORE_NODE(NAME, STEP_INDEX);\
  341. }
  342. LOOP_NODES(ima, step_table[step], av_clip(step + index_table[nibble], 0, 88));
  343. } else { //CODEC_ID_ADPCM_YAMAHA
  344. LOOP_NODES(yamaha, step, av_clip((step * yamaha_indexscale[nibble]) >> 8, 127, 24567));
  345. #undef LOOP_NODES
  346. #undef STORE_NODE
  347. }
  348. }
  349. u = nodes;
  350. nodes = nodes_next;
  351. nodes_next = u;
  352. // prevent overflow
  353. if(nodes[0]->ssd > (1<<28)) {
  354. for(j=1; j<frontier && nodes[j]; j++)
  355. nodes[j]->ssd -= nodes[0]->ssd;
  356. nodes[0]->ssd = 0;
  357. }
  358. // merge old paths to save memory
  359. if(i == froze + FREEZE_INTERVAL) {
  360. p = &paths[nodes[0]->path];
  361. for(k=i; k>froze; k--) {
  362. dst[k] = p->nibble;
  363. p = &paths[p->prev];
  364. }
  365. froze = i;
  366. pathn = 0;
  367. // other nodes might use paths that don't coincide with the frozen one.
  368. // checking which nodes do so is too slow, so just kill them all.
  369. // this also slightly improves quality, but I don't know why.
  370. memset(nodes+1, 0, (frontier-1)*sizeof(TrellisNode*));
  371. }
  372. }
  373. p = &paths[nodes[0]->path];
  374. for(i=n-1; i>froze; i--) {
  375. dst[i] = p->nibble;
  376. p = &paths[p->prev];
  377. }
  378. c->predictor = nodes[0]->sample1;
  379. c->sample1 = nodes[0]->sample1;
  380. c->sample2 = nodes[0]->sample2;
  381. c->step_index = nodes[0]->step;
  382. c->step = nodes[0]->step;
  383. c->idelta = nodes[0]->step;
  384. }
  385. static int adpcm_encode_frame(AVCodecContext *avctx,
  386. unsigned char *frame, int buf_size, void *data)
  387. {
  388. int n, i, st;
  389. short *samples;
  390. unsigned char *dst;
  391. ADPCMContext *c = avctx->priv_data;
  392. dst = frame;
  393. samples = (short *)data;
  394. st= avctx->channels == 2;
  395. /* n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
  396. switch(avctx->codec->id) {
  397. case CODEC_ID_ADPCM_IMA_WAV:
  398. n = avctx->frame_size / 8;
  399. c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
  400. /* c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
  401. bytestream_put_le16(&dst, c->status[0].prev_sample);
  402. *dst++ = (unsigned char)c->status[0].step_index;
  403. *dst++ = 0; /* unknown */
  404. samples++;
  405. if (avctx->channels == 2) {
  406. c->status[1].prev_sample = (signed short)samples[0];
  407. /* c->status[1].step_index = 0; */
  408. bytestream_put_le16(&dst, c->status[1].prev_sample);
  409. *dst++ = (unsigned char)c->status[1].step_index;
  410. *dst++ = 0;
  411. samples++;
  412. }
  413. /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
  414. if(avctx->trellis > 0) {
  415. uint8_t buf[2][n*8];
  416. adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n*8);
  417. if(avctx->channels == 2)
  418. adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n*8);
  419. for(i=0; i<n; i++) {
  420. *dst++ = buf[0][8*i+0] | (buf[0][8*i+1] << 4);
  421. *dst++ = buf[0][8*i+2] | (buf[0][8*i+3] << 4);
  422. *dst++ = buf[0][8*i+4] | (buf[0][8*i+5] << 4);
  423. *dst++ = buf[0][8*i+6] | (buf[0][8*i+7] << 4);
  424. if (avctx->channels == 2) {
  425. *dst++ = buf[1][8*i+0] | (buf[1][8*i+1] << 4);
  426. *dst++ = buf[1][8*i+2] | (buf[1][8*i+3] << 4);
  427. *dst++ = buf[1][8*i+4] | (buf[1][8*i+5] << 4);
  428. *dst++ = buf[1][8*i+6] | (buf[1][8*i+7] << 4);
  429. }
  430. }
  431. } else
  432. for (; n>0; n--) {
  433. *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]);
  434. *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4;
  435. dst++;
  436. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]);
  437. *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4;
  438. dst++;
  439. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]);
  440. *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4;
  441. dst++;
  442. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]);
  443. *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4;
  444. dst++;
  445. /* right channel */
  446. if (avctx->channels == 2) {
  447. *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]);
  448. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4;
  449. dst++;
  450. *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
  451. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
  452. dst++;
  453. *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
  454. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
  455. dst++;
  456. *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
  457. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
  458. dst++;
  459. }
  460. samples += 8 * avctx->channels;
  461. }
  462. break;
  463. case CODEC_ID_ADPCM_IMA_QT:
  464. {
  465. int ch, i;
  466. PutBitContext pb;
  467. init_put_bits(&pb, dst, buf_size*8);
  468. for(ch=0; ch<avctx->channels; ch++){
  469. put_bits(&pb, 9, (c->status[ch].prev_sample + 0x10000) >> 7);
  470. put_bits(&pb, 7, c->status[ch].step_index);
  471. if(avctx->trellis > 0) {
  472. uint8_t buf[64];
  473. adpcm_compress_trellis(avctx, samples+ch, buf, &c->status[ch], 64);
  474. for(i=0; i<64; i++)
  475. put_bits(&pb, 4, buf[i^1]);
  476. c->status[ch].prev_sample = c->status[ch].predictor & ~0x7F;
  477. } else {
  478. for (i=0; i<64; i+=2){
  479. int t1, t2;
  480. t1 = adpcm_ima_compress_sample(&c->status[ch], samples[avctx->channels*(i+0)+ch]);
  481. t2 = adpcm_ima_compress_sample(&c->status[ch], samples[avctx->channels*(i+1)+ch]);
  482. put_bits(&pb, 4, t2);
  483. put_bits(&pb, 4, t1);
  484. }
  485. c->status[ch].prev_sample &= ~0x7F;
  486. }
  487. }
  488. dst += put_bits_count(&pb)>>3;
  489. break;
  490. }
  491. case CODEC_ID_ADPCM_SWF:
  492. {
  493. int i;
  494. PutBitContext pb;
  495. init_put_bits(&pb, dst, buf_size*8);
  496. n = avctx->frame_size-1;
  497. //Store AdpcmCodeSize
  498. put_bits(&pb, 2, 2); //Set 4bits flash adpcm format
  499. //Init the encoder state
  500. for(i=0; i<avctx->channels; i++){
  501. c->status[i].step_index = av_clip(c->status[i].step_index, 0, 63); // clip step so it fits 6 bits
  502. put_bits(&pb, 16, samples[i] & 0xFFFF);
  503. put_bits(&pb, 6, c->status[i].step_index);
  504. c->status[i].prev_sample = (signed short)samples[i];
  505. }
  506. if(avctx->trellis > 0) {
  507. uint8_t buf[2][n];
  508. adpcm_compress_trellis(avctx, samples+2, buf[0], &c->status[0], n);
  509. if (avctx->channels == 2)
  510. adpcm_compress_trellis(avctx, samples+3, buf[1], &c->status[1], n);
  511. for(i=0; i<n; i++) {
  512. put_bits(&pb, 4, buf[0][i]);
  513. if (avctx->channels == 2)
  514. put_bits(&pb, 4, buf[1][i]);
  515. }
  516. } else {
  517. for (i=1; i<avctx->frame_size; i++) {
  518. put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels*i]));
  519. if (avctx->channels == 2)
  520. put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1], samples[2*i+1]));
  521. }
  522. }
  523. flush_put_bits(&pb);
  524. dst += put_bits_count(&pb)>>3;
  525. break;
  526. }
  527. case CODEC_ID_ADPCM_MS:
  528. for(i=0; i<avctx->channels; i++){
  529. int predictor=0;
  530. *dst++ = predictor;
  531. c->status[i].coeff1 = AdaptCoeff1[predictor];
  532. c->status[i].coeff2 = AdaptCoeff2[predictor];
  533. }
  534. for(i=0; i<avctx->channels; i++){
  535. if (c->status[i].idelta < 16)
  536. c->status[i].idelta = 16;
  537. bytestream_put_le16(&dst, c->status[i].idelta);
  538. }
  539. for(i=0; i<avctx->channels; i++){
  540. c->status[i].sample1= *samples++;
  541. bytestream_put_le16(&dst, c->status[i].sample1);
  542. }
  543. for(i=0; i<avctx->channels; i++){
  544. c->status[i].sample2= *samples++;
  545. bytestream_put_le16(&dst, c->status[i].sample2);
  546. }
  547. if(avctx->trellis > 0) {
  548. int n = avctx->block_align - 7*avctx->channels;
  549. uint8_t buf[2][n];
  550. if(avctx->channels == 1) {
  551. n *= 2;
  552. adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
  553. for(i=0; i<n; i+=2)
  554. *dst++ = (buf[0][i] << 4) | buf[0][i+1];
  555. } else {
  556. adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
  557. adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n);
  558. for(i=0; i<n; i++)
  559. *dst++ = (buf[0][i] << 4) | buf[1][i];
  560. }
  561. } else
  562. for(i=7*avctx->channels; i<avctx->block_align; i++) {
  563. int nibble;
  564. nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++)<<4;
  565. nibble|= adpcm_ms_compress_sample(&c->status[st], *samples++);
  566. *dst++ = nibble;
  567. }
  568. break;
  569. case CODEC_ID_ADPCM_YAMAHA:
  570. n = avctx->frame_size / 2;
  571. if(avctx->trellis > 0) {
  572. uint8_t buf[2][n*2];
  573. n *= 2;
  574. if(avctx->channels == 1) {
  575. adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
  576. for(i=0; i<n; i+=2)
  577. *dst++ = buf[0][i] | (buf[0][i+1] << 4);
  578. } else {
  579. adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
  580. adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n);
  581. for(i=0; i<n; i++)
  582. *dst++ = buf[0][i] | (buf[1][i] << 4);
  583. }
  584. } else
  585. for (; n>0; n--) {
  586. for(i = 0; i < avctx->channels; i++) {
  587. int nibble;
  588. nibble = adpcm_yamaha_compress_sample(&c->status[i], samples[i]);
  589. nibble |= adpcm_yamaha_compress_sample(&c->status[i], samples[i+avctx->channels]) << 4;
  590. *dst++ = nibble;
  591. }
  592. samples += 2 * avctx->channels;
  593. }
  594. break;
  595. default:
  596. return -1;
  597. }
  598. return dst - frame;
  599. }
  600. #endif //CONFIG_ENCODERS
  601. static av_cold int adpcm_decode_init(AVCodecContext * avctx)
  602. {
  603. ADPCMContext *c = avctx->priv_data;
  604. unsigned int max_channels = 2, channel;
  605. switch(avctx->codec->id) {
  606. case CODEC_ID_ADPCM_EA_R1:
  607. case CODEC_ID_ADPCM_EA_R2:
  608. case CODEC_ID_ADPCM_EA_R3:
  609. max_channels = 6;
  610. break;
  611. }
  612. if(avctx->channels > max_channels){
  613. return -1;
  614. }
  615. switch(avctx->codec->id) {
  616. case CODEC_ID_ADPCM_CT:
  617. c->status[0].step = c->status[1].step = 511;
  618. break;
  619. case CODEC_ID_ADPCM_IMA_WS:
  620. if (avctx->extradata && avctx->extradata_size == 2 * 4) {
  621. c->status[0].predictor = AV_RL32(avctx->extradata);
  622. c->status[1].predictor = AV_RL32(avctx->extradata + 4);
  623. }
  624. break;
  625. default:
  626. break;
  627. }
  628. return 0;
  629. }
  630. static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
  631. {
  632. int step_index;
  633. int predictor;
  634. int sign, delta, diff, step;
  635. step = step_table[c->step_index];
  636. step_index = c->step_index + index_table[(unsigned)nibble];
  637. if (step_index < 0) step_index = 0;
  638. else if (step_index > 88) step_index = 88;
  639. sign = nibble & 8;
  640. delta = nibble & 7;
  641. /* perform direct multiplication instead of series of jumps proposed by
  642. * the reference ADPCM implementation since modern CPUs can do the mults
  643. * quickly enough */
  644. diff = ((2 * delta + 1) * step) >> shift;
  645. predictor = c->predictor;
  646. if (sign) predictor -= diff;
  647. else predictor += diff;
  648. c->predictor = av_clip_int16(predictor);
  649. c->step_index = step_index;
  650. return (short)c->predictor;
  651. }
  652. static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
  653. {
  654. int predictor;
  655. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
  656. predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  657. c->sample2 = c->sample1;
  658. c->sample1 = av_clip_int16(predictor);
  659. c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
  660. if (c->idelta < 16) c->idelta = 16;
  661. return c->sample1;
  662. }
  663. static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
  664. {
  665. int sign, delta, diff;
  666. int new_step;
  667. sign = nibble & 8;
  668. delta = nibble & 7;
  669. /* perform direct multiplication instead of series of jumps proposed by
  670. * the reference ADPCM implementation since modern CPUs can do the mults
  671. * quickly enough */
  672. diff = ((2 * delta + 1) * c->step) >> 3;
  673. /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
  674. c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
  675. c->predictor = av_clip_int16(c->predictor);
  676. /* calculate new step and clamp it to range 511..32767 */
  677. new_step = (ct_adpcm_table[nibble & 7] * c->step) >> 8;
  678. c->step = av_clip(new_step, 511, 32767);
  679. return (short)c->predictor;
  680. }
  681. static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
  682. {
  683. int sign, delta, diff;
  684. sign = nibble & (1<<(size-1));
  685. delta = nibble & ((1<<(size-1))-1);
  686. diff = delta << (7 + c->step + shift);
  687. /* clamp result */
  688. c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
  689. /* calculate new step */
  690. if (delta >= (2*size - 3) && c->step < 3)
  691. c->step++;
  692. else if (delta == 0 && c->step > 0)
  693. c->step--;
  694. return (short) c->predictor;
  695. }
  696. static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
  697. {
  698. if(!c->step) {
  699. c->predictor = 0;
  700. c->step = 127;
  701. }
  702. c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;
  703. c->predictor = av_clip_int16(c->predictor);
  704. c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
  705. c->step = av_clip(c->step, 127, 24567);
  706. return c->predictor;
  707. }
  708. static void xa_decode(short *out, const unsigned char *in,
  709. ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
  710. {
  711. int i, j;
  712. int shift,filter,f0,f1;
  713. int s_1,s_2;
  714. int d,s,t;
  715. for(i=0;i<4;i++) {
  716. shift = 12 - (in[4+i*2] & 15);
  717. filter = in[4+i*2] >> 4;
  718. f0 = xa_adpcm_table[filter][0];
  719. f1 = xa_adpcm_table[filter][1];
  720. s_1 = left->sample1;
  721. s_2 = left->sample2;
  722. for(j=0;j<28;j++) {
  723. d = in[16+i+j*4];
  724. t = (signed char)(d<<4)>>4;
  725. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  726. s_2 = s_1;
  727. s_1 = av_clip_int16(s);
  728. *out = s_1;
  729. out += inc;
  730. }
  731. if (inc==2) { /* stereo */
  732. left->sample1 = s_1;
  733. left->sample2 = s_2;
  734. s_1 = right->sample1;
  735. s_2 = right->sample2;
  736. out = out + 1 - 28*2;
  737. }
  738. shift = 12 - (in[5+i*2] & 15);
  739. filter = in[5+i*2] >> 4;
  740. f0 = xa_adpcm_table[filter][0];
  741. f1 = xa_adpcm_table[filter][1];
  742. for(j=0;j<28;j++) {
  743. d = in[16+i+j*4];
  744. t = (signed char)d >> 4;
  745. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  746. s_2 = s_1;
  747. s_1 = av_clip_int16(s);
  748. *out = s_1;
  749. out += inc;
  750. }
  751. if (inc==2) { /* stereo */
  752. right->sample1 = s_1;
  753. right->sample2 = s_2;
  754. out -= 1;
  755. } else {
  756. left->sample1 = s_1;
  757. left->sample2 = s_2;
  758. }
  759. }
  760. }
  761. /* DK3 ADPCM support macro */
  762. #define DK3_GET_NEXT_NIBBLE() \
  763. if (decode_top_nibble_next) \
  764. { \
  765. nibble = last_byte >> 4; \
  766. decode_top_nibble_next = 0; \
  767. } \
  768. else \
  769. { \
  770. last_byte = *src++; \
  771. if (src >= buf + buf_size) break; \
  772. nibble = last_byte & 0x0F; \
  773. decode_top_nibble_next = 1; \
  774. }
  775. static int adpcm_decode_frame(AVCodecContext *avctx,
  776. void *data, int *data_size,
  777. const uint8_t *buf, int buf_size)
  778. {
  779. ADPCMContext *c = avctx->priv_data;
  780. ADPCMChannelStatus *cs;
  781. int n, m, channel, i;
  782. int block_predictor[2];
  783. short *samples;
  784. short *samples_end;
  785. const uint8_t *src;
  786. int st; /* stereo */
  787. /* DK3 ADPCM accounting variables */
  788. unsigned char last_byte = 0;
  789. unsigned char nibble;
  790. int decode_top_nibble_next = 0;
  791. int diff_channel;
  792. /* EA ADPCM state variables */
  793. uint32_t samples_in_chunk;
  794. int32_t previous_left_sample, previous_right_sample;
  795. int32_t current_left_sample, current_right_sample;
  796. int32_t next_left_sample, next_right_sample;
  797. int32_t coeff1l, coeff2l, coeff1r, coeff2r;
  798. uint8_t shift_left, shift_right;
  799. int count1, count2;
  800. int coeff[2][2], shift[2];//used in EA MAXIS ADPCM
  801. if (!buf_size)
  802. return 0;
  803. //should protect all 4bit ADPCM variants
  804. //8 is needed for CODEC_ID_ADPCM_IMA_WAV with 2 channels
  805. //
  806. if(*data_size/4 < buf_size + 8)
  807. return -1;
  808. samples = data;
  809. samples_end= samples + *data_size/2;
  810. *data_size= 0;
  811. src = buf;
  812. st = avctx->channels == 2 ? 1 : 0;
  813. switch(avctx->codec->id) {
  814. case CODEC_ID_ADPCM_IMA_QT:
  815. n = (buf_size - 2);/* >> 2*avctx->channels;*/
  816. channel = c->channel;
  817. cs = &(c->status[channel]);
  818. /* (pppppp) (piiiiiii) */
  819. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  820. cs->predictor = (*src++) << 8;
  821. cs->predictor |= (*src & 0x80);
  822. cs->predictor &= 0xFF80;
  823. /* sign extension */
  824. if(cs->predictor & 0x8000)
  825. cs->predictor -= 0x10000;
  826. cs->predictor = av_clip_int16(cs->predictor);
  827. cs->step_index = (*src++) & 0x7F;
  828. if (cs->step_index > 88){
  829. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  830. cs->step_index = 88;
  831. }
  832. cs->step = step_table[cs->step_index];
  833. if (st && channel)
  834. samples++;
  835. for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
  836. *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
  837. samples += avctx->channels;
  838. *samples = adpcm_ima_expand_nibble(cs, src[0] >> 4 , 3);
  839. samples += avctx->channels;
  840. src ++;
  841. }
  842. if(st) { /* handle stereo interlacing */
  843. c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
  844. if(!channel) { /* wait for the other packet before outputing anything */
  845. return src - buf;
  846. }
  847. samples--;
  848. }
  849. break;
  850. case CODEC_ID_ADPCM_IMA_WAV:
  851. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  852. buf_size = avctx->block_align;
  853. // samples_per_block= (block_align-4*chanels)*8 / (bits_per_sample * chanels) + 1;
  854. for(i=0; i<avctx->channels; i++){
  855. cs = &(c->status[i]);
  856. cs->predictor = *samples++ = (int16_t)(src[0] + (src[1]<<8));
  857. src+=2;
  858. cs->step_index = *src++;
  859. if (cs->step_index > 88){
  860. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  861. cs->step_index = 88;
  862. }
  863. if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */
  864. }
  865. while(src < buf + buf_size){
  866. for(m=0; m<4; m++){
  867. for(i=0; i<=st; i++)
  868. *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] & 0x0F, 3);
  869. for(i=0; i<=st; i++)
  870. *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] >> 4 , 3);
  871. src++;
  872. }
  873. src += 4*st;
  874. }
  875. break;
  876. case CODEC_ID_ADPCM_4XM:
  877. cs = &(c->status[0]);
  878. c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  879. if(st){
  880. c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  881. }
  882. c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  883. if(st){
  884. c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  885. }
  886. if (cs->step_index < 0) cs->step_index = 0;
  887. if (cs->step_index > 88) cs->step_index = 88;
  888. m= (buf_size - (src - buf))>>st;
  889. for(i=0; i<m; i++) {
  890. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
  891. if (st)
  892. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
  893. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
  894. if (st)
  895. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
  896. }
  897. src += m<<st;
  898. break;
  899. case CODEC_ID_ADPCM_MS:
  900. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  901. buf_size = avctx->block_align;
  902. n = buf_size - 7 * avctx->channels;
  903. if (n < 0)
  904. return -1;
  905. block_predictor[0] = av_clip(*src++, 0, 7);
  906. block_predictor[1] = 0;
  907. if (st)
  908. block_predictor[1] = av_clip(*src++, 0, 7);
  909. c->status[0].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  910. src+=2;
  911. if (st){
  912. c->status[1].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  913. src+=2;
  914. }
  915. c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
  916. c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
  917. c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
  918. c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
  919. c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  920. src+=2;
  921. if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  922. if (st) src+=2;
  923. c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  924. src+=2;
  925. if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  926. if (st) src+=2;
  927. *samples++ = c->status[0].sample1;
  928. if (st) *samples++ = c->status[1].sample1;
  929. *samples++ = c->status[0].sample2;
  930. if (st) *samples++ = c->status[1].sample2;
  931. for(;n>0;n--) {
  932. *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], src[0] >> 4 );
  933. *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
  934. src ++;
  935. }
  936. break;
  937. case CODEC_ID_ADPCM_IMA_DK4:
  938. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  939. buf_size = avctx->block_align;
  940. c->status[0].predictor = (int16_t)(src[0] | (src[1] << 8));
  941. c->status[0].step_index = src[2];
  942. src += 4;
  943. *samples++ = c->status[0].predictor;
  944. if (st) {
  945. c->status[1].predictor = (int16_t)(src[0] | (src[1] << 8));
  946. c->status[1].step_index = src[2];
  947. src += 4;
  948. *samples++ = c->status[1].predictor;
  949. }
  950. while (src < buf + buf_size) {
  951. /* take care of the top nibble (always left or mono channel) */
  952. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  953. src[0] >> 4, 3);
  954. /* take care of the bottom nibble, which is right sample for
  955. * stereo, or another mono sample */
  956. if (st)
  957. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  958. src[0] & 0x0F, 3);
  959. else
  960. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  961. src[0] & 0x0F, 3);
  962. src++;
  963. }
  964. break;
  965. case CODEC_ID_ADPCM_IMA_DK3:
  966. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  967. buf_size = avctx->block_align;
  968. if(buf_size + 16 > (samples_end - samples)*3/8)
  969. return -1;
  970. c->status[0].predictor = (int16_t)(src[10] | (src[11] << 8));
  971. c->status[1].predictor = (int16_t)(src[12] | (src[13] << 8));
  972. c->status[0].step_index = src[14];
  973. c->status[1].step_index = src[15];
  974. /* sign extend the predictors */
  975. src += 16;
  976. diff_channel = c->status[1].predictor;
  977. /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
  978. * the buffer is consumed */
  979. while (1) {
  980. /* for this algorithm, c->status[0] is the sum channel and
  981. * c->status[1] is the diff channel */
  982. /* process the first predictor of the sum channel */
  983. DK3_GET_NEXT_NIBBLE();
  984. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  985. /* process the diff channel predictor */
  986. DK3_GET_NEXT_NIBBLE();
  987. adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
  988. /* process the first pair of stereo PCM samples */
  989. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  990. *samples++ = c->status[0].predictor + c->status[1].predictor;
  991. *samples++ = c->status[0].predictor - c->status[1].predictor;
  992. /* process the second predictor of the sum channel */
  993. DK3_GET_NEXT_NIBBLE();
  994. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  995. /* process the second pair of stereo PCM samples */
  996. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  997. *samples++ = c->status[0].predictor + c->status[1].predictor;
  998. *samples++ = c->status[0].predictor - c->status[1].predictor;
  999. }
  1000. break;
  1001. case CODEC_ID_ADPCM_IMA_WS:
  1002. /* no per-block initialization; just start decoding the data */
  1003. while (src < buf + buf_size) {
  1004. if (st) {
  1005. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1006. src[0] >> 4 , 3);
  1007. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  1008. src[0] & 0x0F, 3);
  1009. } else {
  1010. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1011. src[0] >> 4 , 3);
  1012. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1013. src[0] & 0x0F, 3);
  1014. }
  1015. src++;
  1016. }
  1017. break;
  1018. case CODEC_ID_ADPCM_XA:
  1019. while (buf_size >= 128) {
  1020. xa_decode(samples, src, &c->status[0], &c->status[1],
  1021. avctx->channels);
  1022. src += 128;
  1023. samples += 28 * 8;
  1024. buf_size -= 128;
  1025. }
  1026. break;
  1027. case CODEC_ID_ADPCM_IMA_EA_EACS:
  1028. samples_in_chunk = bytestream_get_le32(&src) >> (1-st);
  1029. if (samples_in_chunk > buf_size-4-(8<<st)) {
  1030. src += buf_size - 4;
  1031. break;
  1032. }
  1033. for (i=0; i<=st; i++)
  1034. c->status[i].step_index = bytestream_get_le32(&src);
  1035. for (i=0; i<=st; i++)
  1036. c->status[i].predictor = bytestream_get_le32(&src);
  1037. for (; samples_in_chunk; samples_in_chunk--, src++) {
  1038. *samples++ = adpcm_ima_expand_nibble(&c->status[0], *src>>4, 3);
  1039. *samples++ = adpcm_ima_expand_nibble(&c->status[st], *src&0x0F, 3);
  1040. }
  1041. break;
  1042. case CODEC_ID_ADPCM_IMA_EA_SEAD:
  1043. for (; src < buf+buf_size; src++) {
  1044. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] >> 4, 6);
  1045. *samples++ = adpcm_ima_expand_nibble(&c->status[st],src[0]&0x0F, 6);
  1046. }
  1047. break;
  1048. case CODEC_ID_ADPCM_EA:
  1049. samples_in_chunk = AV_RL32(src);
  1050. if (samples_in_chunk >= ((buf_size - 12) * 2)) {
  1051. src += buf_size;
  1052. break;
  1053. }
  1054. src += 4;
  1055. current_left_sample = (int16_t)AV_RL16(src);
  1056. src += 2;
  1057. previous_left_sample = (int16_t)AV_RL16(src);
  1058. src += 2;
  1059. current_right_sample = (int16_t)AV_RL16(src);
  1060. src += 2;
  1061. previous_right_sample = (int16_t)AV_RL16(src);
  1062. src += 2;
  1063. for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
  1064. coeff1l = ea_adpcm_table[ *src >> 4 ];
  1065. coeff2l = ea_adpcm_table[(*src >> 4 ) + 4];
  1066. coeff1r = ea_adpcm_table[*src & 0x0F];
  1067. coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
  1068. src++;
  1069. shift_left = (*src >> 4 ) + 8;
  1070. shift_right = (*src & 0x0F) + 8;
  1071. src++;
  1072. for (count2 = 0; count2 < 28; count2++) {
  1073. next_left_sample = (int32_t)((*src & 0xF0) << 24) >> shift_left;
  1074. next_right_sample = (int32_t)((*src & 0x0F) << 28) >> shift_right;
  1075. src++;
  1076. next_left_sample = (next_left_sample +
  1077. (current_left_sample * coeff1l) +
  1078. (previous_left_sample * coeff2l) + 0x80) >> 8;
  1079. next_right_sample = (next_right_sample +
  1080. (current_right_sample * coeff1r) +
  1081. (previous_right_sample * coeff2r) + 0x80) >> 8;
  1082. previous_left_sample = current_left_sample;
  1083. current_left_sample = av_clip_int16(next_left_sample);
  1084. previous_right_sample = current_right_sample;
  1085. current_right_sample = av_clip_int16(next_right_sample);
  1086. *samples++ = (unsigned short)current_left_sample;
  1087. *samples++ = (unsigned short)current_right_sample;
  1088. }
  1089. }
  1090. break;
  1091. case CODEC_ID_ADPCM_EA_MAXIS_XA:
  1092. for(channel = 0; channel < avctx->channels; channel++) {
  1093. for (i=0; i<2; i++)
  1094. coeff[channel][i] = ea_adpcm_table[(*src >> 4) + 4*i];
  1095. shift[channel] = (*src & 0x0F) + 8;
  1096. src++;
  1097. }
  1098. for (count1 = 0; count1 < (buf_size - avctx->channels) / avctx->channels; count1++) {
  1099. for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
  1100. for(channel = 0; channel < avctx->channels; channel++) {
  1101. int32_t sample = (int32_t)(((*(src+channel) >> i) & 0x0F) << 0x1C) >> shift[channel];
  1102. sample = (sample +
  1103. c->status[channel].sample1 * coeff[channel][0] +
  1104. c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
  1105. c->status[channel].sample2 = c->status[channel].sample1;
  1106. c->status[channel].sample1 = av_clip_int16(sample);
  1107. *samples++ = c->status[channel].sample1;
  1108. }
  1109. }
  1110. src+=avctx->channels;
  1111. }
  1112. break;
  1113. case CODEC_ID_ADPCM_EA_R1:
  1114. case CODEC_ID_ADPCM_EA_R2:
  1115. case CODEC_ID_ADPCM_EA_R3: {
  1116. /* channel numbering
  1117. 2chan: 0=fl, 1=fr
  1118. 4chan: 0=fl, 1=rl, 2=fr, 3=rr
  1119. 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
  1120. const int big_endian = avctx->codec->id == CODEC_ID_ADPCM_EA_R3;
  1121. int32_t previous_sample, current_sample, next_sample;
  1122. int32_t coeff1, coeff2;
  1123. uint8_t shift;
  1124. unsigned int channel;
  1125. uint16_t *samplesC;
  1126. const uint8_t *srcC;
  1127. samples_in_chunk = (big_endian ? bytestream_get_be32(&src)
  1128. : bytestream_get_le32(&src)) / 28;
  1129. if (samples_in_chunk > UINT32_MAX/(28*avctx->channels) ||
  1130. 28*samples_in_chunk*avctx->channels > samples_end-samples) {
  1131. src += buf_size - 4;
  1132. break;
  1133. }
  1134. for (channel=0; channel<avctx->channels; channel++) {
  1135. srcC = src + (big_endian ? bytestream_get_be32(&src)
  1136. : bytestream_get_le32(&src))
  1137. + (avctx->channels-channel-1) * 4;
  1138. samplesC = samples + channel;
  1139. if (avctx->codec->id == CODEC_ID_ADPCM_EA_R1) {
  1140. current_sample = (int16_t)bytestream_get_le16(&srcC);
  1141. previous_sample = (int16_t)bytestream_get_le16(&srcC);
  1142. } else {
  1143. current_sample = c->status[channel].predictor;
  1144. previous_sample = c->status[channel].prev_sample;
  1145. }
  1146. for (count1=0; count1<samples_in_chunk; count1++) {
  1147. if (*srcC == 0xEE) { /* only seen in R2 and R3 */
  1148. srcC++;
  1149. current_sample = (int16_t)bytestream_get_be16(&srcC);
  1150. previous_sample = (int16_t)bytestream_get_be16(&srcC);
  1151. for (count2=0; count2<28; count2++) {
  1152. *samplesC = (int16_t)bytestream_get_be16(&srcC);
  1153. samplesC += avctx->channels;
  1154. }
  1155. } else {
  1156. coeff1 = ea_adpcm_table[ *srcC>>4 ];
  1157. coeff2 = ea_adpcm_table[(*srcC>>4) + 4];
  1158. shift = (*srcC++ & 0x0F) + 8;
  1159. for (count2=0; count2<28; count2++) {
  1160. if (count2 & 1)
  1161. next_sample = (int32_t)((*srcC++ & 0x0F) << 28) >> shift;
  1162. else
  1163. next_sample = (int32_t)((*srcC & 0xF0) << 24) >> shift;
  1164. next_sample += (current_sample * coeff1) +
  1165. (previous_sample * coeff2);
  1166. next_sample = av_clip_int16(next_sample >> 8);
  1167. previous_sample = current_sample;
  1168. current_sample = next_sample;
  1169. *samplesC = current_sample;
  1170. samplesC += avctx->channels;
  1171. }
  1172. }
  1173. }
  1174. if (avctx->codec->id != CODEC_ID_ADPCM_EA_R1) {
  1175. c->status[channel].predictor = current_sample;
  1176. c->status[channel].prev_sample = previous_sample;
  1177. }
  1178. }
  1179. src = src + buf_size - (4 + 4*avctx->channels);
  1180. samples += 28 * samples_in_chunk * avctx->channels;
  1181. break;
  1182. }
  1183. case CODEC_ID_ADPCM_EA_XAS:
  1184. if (samples_end-samples < 32*4*avctx->channels
  1185. || buf_size < (4+15)*4*avctx->channels) {
  1186. src += buf_size;
  1187. break;
  1188. }
  1189. for (channel=0; channel<avctx->channels; channel++) {
  1190. int coeff[2][4], shift[4];
  1191. short *s2, *s = &samples[channel];
  1192. for (n=0; n<4; n++, s+=32*avctx->channels) {
  1193. for (i=0; i<2; i++)
  1194. coeff[i][n] = ea_adpcm_table[(src[0]&0x0F)+4*i];
  1195. shift[n] = (src[2]&0x0F) + 8;
  1196. for (s2=s, i=0; i<2; i++, src+=2, s2+=avctx->channels)
  1197. s2[0] = (src[0]&0xF0) + (src[1]<<8);
  1198. }
  1199. for (m=2; m<32; m+=2) {
  1200. s = &samples[m*avctx->channels + channel];
  1201. for (n=0; n<4; n++, src++, s+=32*avctx->channels) {
  1202. for (s2=s, i=0; i<8; i+=4, s2+=avctx->channels) {
  1203. int level = (int32_t)((*src & (0xF0>>i)) << (24+i)) >> shift[n];
  1204. int pred = s2[-1*avctx->channels] * coeff[0][n]
  1205. + s2[-2*avctx->channels] * coeff[1][n];
  1206. s2[0] = av_clip_int16((level + pred + 0x80) >> 8);
  1207. }
  1208. }
  1209. }
  1210. }
  1211. samples += 32*4*avctx->channels;
  1212. break;
  1213. case CODEC_ID_ADPCM_IMA_AMV:
  1214. case CODEC_ID_ADPCM_IMA_SMJPEG:
  1215. c->status[0].predictor = (int16_t)bytestream_get_le16(&src);
  1216. c->status[0].step_index = bytestream_get_le16(&src);
  1217. if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
  1218. src+=4;
  1219. while (src < buf + buf_size) {
  1220. char hi, lo;
  1221. lo = *src & 0x0F;
  1222. hi = *src >> 4;
  1223. if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
  1224. FFSWAP(char, hi, lo);
  1225. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1226. lo, 3);
  1227. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1228. hi, 3);
  1229. src++;
  1230. }
  1231. break;
  1232. case CODEC_ID_ADPCM_CT:
  1233. while (src < buf + buf_size) {
  1234. if (st) {
  1235. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1236. src[0] >> 4);
  1237. *samples++ = adpcm_ct_expand_nibble(&c->status[1],
  1238. src[0] & 0x0F);
  1239. } else {
  1240. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1241. src[0] >> 4);
  1242. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1243. src[0] & 0x0F);
  1244. }
  1245. src++;
  1246. }
  1247. break;
  1248. case CODEC_ID_ADPCM_SBPRO_4:
  1249. case CODEC_ID_ADPCM_SBPRO_3:
  1250. case CODEC_ID_ADPCM_SBPRO_2:
  1251. if (!c->status[0].step_index) {
  1252. /* the first byte is a raw sample */
  1253. *samples++ = 128 * (*src++ - 0x80);
  1254. if (st)
  1255. *samples++ = 128 * (*src++ - 0x80);
  1256. c->status[0].step_index = 1;
  1257. }
  1258. if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
  1259. while (src < buf + buf_size) {
  1260. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1261. src[0] >> 4, 4, 0);
  1262. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1263. src[0] & 0x0F, 4, 0);
  1264. src++;
  1265. }
  1266. } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
  1267. while (src < buf + buf_size && samples + 2 < samples_end) {
  1268. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1269. src[0] >> 5 , 3, 0);
  1270. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1271. (src[0] >> 2) & 0x07, 3, 0);
  1272. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1273. src[0] & 0x03, 2, 0);
  1274. src++;
  1275. }
  1276. } else {
  1277. while (src < buf + buf_size && samples + 3 < samples_end) {
  1278. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1279. src[0] >> 6 , 2, 2);
  1280. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1281. (src[0] >> 4) & 0x03, 2, 2);
  1282. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1283. (src[0] >> 2) & 0x03, 2, 2);
  1284. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1285. src[0] & 0x03, 2, 2);
  1286. src++;
  1287. }
  1288. }
  1289. break;
  1290. case CODEC_ID_ADPCM_SWF:
  1291. {
  1292. GetBitContext gb;
  1293. const int *table;
  1294. int k0, signmask, nb_bits, count;
  1295. int size = buf_size*8;
  1296. init_get_bits(&gb, buf, size);
  1297. //read bits & initial values
  1298. nb_bits = get_bits(&gb, 2)+2;
  1299. //av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", nb_bits);
  1300. table = swf_index_tables[nb_bits-2];
  1301. k0 = 1 << (nb_bits-2);
  1302. signmask = 1 << (nb_bits-1);
  1303. while (get_bits_count(&gb) <= size - 22*avctx->channels) {
  1304. for (i = 0; i < avctx->channels; i++) {
  1305. *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
  1306. c->status[i].step_index = get_bits(&gb, 6);
  1307. }
  1308. for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
  1309. int i;
  1310. for (i = 0; i < avctx->channels; i++) {
  1311. // similar to IMA adpcm
  1312. int delta = get_bits(&gb, nb_bits);
  1313. int step = step_table[c->status[i].step_index];
  1314. long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
  1315. int k = k0;
  1316. do {
  1317. if (delta & k)
  1318. vpdiff += step;
  1319. step >>= 1;
  1320. k >>= 1;
  1321. } while(k);
  1322. vpdiff += step;
  1323. if (delta & signmask)
  1324. c->status[i].predictor -= vpdiff;
  1325. else
  1326. c->status[i].predictor += vpdiff;
  1327. c->status[i].step_index += table[delta & (~signmask)];
  1328. c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
  1329. c->status[i].predictor = av_clip_int16(c->status[i].predictor);
  1330. *samples++ = c->status[i].predictor;
  1331. if (samples >= samples_end) {
  1332. av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
  1333. return -1;
  1334. }
  1335. }
  1336. }
  1337. }
  1338. src += buf_size;
  1339. break;
  1340. }
  1341. case CODEC_ID_ADPCM_YAMAHA:
  1342. while (src < buf + buf_size) {
  1343. if (st) {
  1344. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1345. src[0] & 0x0F);
  1346. *samples++ = adpcm_yamaha_expand_nibble(&c->status[1],
  1347. src[0] >> 4 );
  1348. } else {
  1349. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1350. src[0] & 0x0F);
  1351. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1352. src[0] >> 4 );
  1353. }
  1354. src++;
  1355. }
  1356. break;
  1357. case CODEC_ID_ADPCM_THP:
  1358. {
  1359. int table[2][16];
  1360. unsigned int samplecnt;
  1361. int prev[2][2];
  1362. int ch;
  1363. if (buf_size < 80) {
  1364. av_log(avctx, AV_LOG_ERROR, "frame too small\n");
  1365. return -1;
  1366. }
  1367. src+=4;
  1368. samplecnt = bytestream_get_be32(&src);
  1369. for (i = 0; i < 32; i++)
  1370. table[0][i] = (int16_t)bytestream_get_be16(&src);
  1371. /* Initialize the previous sample. */
  1372. for (i = 0; i < 4; i++)
  1373. prev[0][i] = (int16_t)bytestream_get_be16(&src);
  1374. if (samplecnt >= (samples_end - samples) / (st + 1)) {
  1375. av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
  1376. return -1;
  1377. }
  1378. for (ch = 0; ch <= st; ch++) {
  1379. samples = (unsigned short *) data + ch;
  1380. /* Read in every sample for this channel. */
  1381. for (i = 0; i < samplecnt / 14; i++) {
  1382. int index = (*src >> 4) & 7;
  1383. unsigned int exp = 28 - (*src++ & 15);
  1384. int factor1 = table[ch][index * 2];
  1385. int factor2 = table[ch][index * 2 + 1];
  1386. /* Decode 14 samples. */
  1387. for (n = 0; n < 14; n++) {
  1388. int32_t sampledat;
  1389. if(n&1) sampledat= *src++ <<28;
  1390. else sampledat= (*src&0xF0)<<24;
  1391. sampledat = ((prev[ch][0]*factor1
  1392. + prev[ch][1]*factor2) >> 11) + (sampledat>>exp);
  1393. *samples = av_clip_int16(sampledat);
  1394. prev[ch][1] = prev[ch][0];
  1395. prev[ch][0] = *samples++;
  1396. /* In case of stereo, skip one sample, this sample
  1397. is for the other channel. */
  1398. samples += st;
  1399. }
  1400. }
  1401. }
  1402. /* In the previous loop, in case stereo is used, samples is
  1403. increased exactly one time too often. */
  1404. samples -= st;
  1405. break;
  1406. }
  1407. default:
  1408. return -1;
  1409. }
  1410. *data_size = (uint8_t *)samples - (uint8_t *)data;
  1411. return src - buf;
  1412. }
  1413. #ifdef CONFIG_ENCODERS
  1414. #define ADPCM_ENCODER(id,name) \
  1415. AVCodec name ## _encoder = { \
  1416. #name, \
  1417. CODEC_TYPE_AUDIO, \
  1418. id, \
  1419. sizeof(ADPCMContext), \
  1420. adpcm_encode_init, \
  1421. adpcm_encode_frame, \
  1422. adpcm_encode_close, \
  1423. NULL, \
  1424. };
  1425. #else
  1426. #define ADPCM_ENCODER(id,name)
  1427. #endif
  1428. #ifdef CONFIG_DECODERS
  1429. #define ADPCM_DECODER(id,name) \
  1430. AVCodec name ## _decoder = { \
  1431. #name, \
  1432. CODEC_TYPE_AUDIO, \
  1433. id, \
  1434. sizeof(ADPCMContext), \
  1435. adpcm_decode_init, \
  1436. NULL, \
  1437. NULL, \
  1438. adpcm_decode_frame, \
  1439. };
  1440. #else
  1441. #define ADPCM_DECODER(id,name)
  1442. #endif
  1443. #define ADPCM_CODEC(id, name) \
  1444. ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name)
  1445. ADPCM_DECODER(CODEC_ID_ADPCM_4XM, adpcm_4xm);
  1446. ADPCM_DECODER(CODEC_ID_ADPCM_CT, adpcm_ct);
  1447. ADPCM_DECODER(CODEC_ID_ADPCM_EA, adpcm_ea);
  1448. ADPCM_DECODER(CODEC_ID_ADPCM_EA_MAXIS_XA, adpcm_ea_maxis_xa);
  1449. ADPCM_DECODER(CODEC_ID_ADPCM_EA_R1, adpcm_ea_r1);
  1450. ADPCM_DECODER(CODEC_ID_ADPCM_EA_R2, adpcm_ea_r2);
  1451. ADPCM_DECODER(CODEC_ID_ADPCM_EA_R3, adpcm_ea_r3);
  1452. ADPCM_DECODER(CODEC_ID_ADPCM_EA_XAS, adpcm_ea_xas);
  1453. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_AMV, adpcm_ima_amv);
  1454. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
  1455. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
  1456. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_EACS, adpcm_ima_ea_eacs);
  1457. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_SEAD, adpcm_ima_ea_sead);
  1458. ADPCM_CODEC (CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
  1459. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg);
  1460. ADPCM_CODEC (CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
  1461. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
  1462. ADPCM_CODEC (CODEC_ID_ADPCM_MS, adpcm_ms);
  1463. ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2);
  1464. ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3);
  1465. ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4);
  1466. ADPCM_CODEC (CODEC_ID_ADPCM_SWF, adpcm_swf);
  1467. ADPCM_DECODER(CODEC_ID_ADPCM_THP, adpcm_thp);
  1468. ADPCM_DECODER(CODEC_ID_ADPCM_XA, adpcm_xa);
  1469. ADPCM_CODEC (CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha);