adpcm.c 51 KB

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