adpcm.c 65 KB

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