adpcm.c 62 KB

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