adpcm.c 50 KB

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