tokenize.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  5. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  6. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 *
  9. * by the Xiph.Org Foundation http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function:
  13. last mod: $Id: tokenize.c 16503 2009-08-22 18:14:02Z giles $
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "encint.h"
  18. static int oc_make_eob_token(int _run_count){
  19. if(_run_count<4)return OC_DCT_EOB1_TOKEN+_run_count-1;
  20. else{
  21. int cat;
  22. cat=OC_ILOGNZ_32(_run_count)-3;
  23. cat=OC_MINI(cat,3);
  24. return OC_DCT_REPEAT_RUN0_TOKEN+cat;
  25. }
  26. }
  27. static int oc_make_eob_token_full(int _run_count,int *_eb){
  28. if(_run_count<4){
  29. *_eb=0;
  30. return OC_DCT_EOB1_TOKEN+_run_count-1;
  31. }
  32. else{
  33. int cat;
  34. cat=OC_ILOGNZ_32(_run_count)-3;
  35. cat=OC_MINI(cat,3);
  36. *_eb=_run_count-OC_BYTE_TABLE32(4,8,16,0,cat);
  37. return OC_DCT_REPEAT_RUN0_TOKEN+cat;
  38. }
  39. }
  40. /*Returns the number of blocks ended by an EOB token.*/
  41. static int oc_decode_eob_token(int _token,int _eb){
  42. return (0x20820C41U>>_token*5&0x1F)+_eb;
  43. }
  44. /*TODO: This is now only used during DCT tokenization, and never for runs; it
  45. should be simplified.*/
  46. static int oc_make_dct_token_full(int _zzi,int _zzj,int _val,int *_eb){
  47. int neg;
  48. int zero_run;
  49. int token;
  50. int eb;
  51. neg=_val<0;
  52. _val=abs(_val);
  53. zero_run=_zzj-_zzi;
  54. if(zero_run>0){
  55. int adj;
  56. /*Implement a minor restriction on stack 1 so that we know during DC fixups
  57. that extending a dctrun token from stack 1 will never overflow.*/
  58. adj=_zzi!=1;
  59. if(_val<2&&zero_run<17+adj){
  60. if(zero_run<6){
  61. token=OC_DCT_RUN_CAT1A+zero_run-1;
  62. eb=neg;
  63. }
  64. else if(zero_run<10){
  65. token=OC_DCT_RUN_CAT1B;
  66. eb=zero_run-6+(neg<<2);
  67. }
  68. else{
  69. token=OC_DCT_RUN_CAT1C;
  70. eb=zero_run-10+(neg<<3);
  71. }
  72. }
  73. else if(_val<4&&zero_run<3+adj){
  74. if(zero_run<2){
  75. token=OC_DCT_RUN_CAT2A;
  76. eb=_val-2+(neg<<1);
  77. }
  78. else{
  79. token=OC_DCT_RUN_CAT2B;
  80. eb=zero_run-2+(_val-2<<1)+(neg<<2);
  81. }
  82. }
  83. else{
  84. if(zero_run<9)token=OC_DCT_SHORT_ZRL_TOKEN;
  85. else token=OC_DCT_ZRL_TOKEN;
  86. eb=zero_run-1;
  87. }
  88. }
  89. else if(_val<3){
  90. token=OC_ONE_TOKEN+(_val-1<<1)+neg;
  91. eb=0;
  92. }
  93. else if(_val<7){
  94. token=OC_DCT_VAL_CAT2+_val-3;
  95. eb=neg;
  96. }
  97. else if(_val<9){
  98. token=OC_DCT_VAL_CAT3;
  99. eb=_val-7+(neg<<1);
  100. }
  101. else if(_val<13){
  102. token=OC_DCT_VAL_CAT4;
  103. eb=_val-9+(neg<<2);
  104. }
  105. else if(_val<21){
  106. token=OC_DCT_VAL_CAT5;
  107. eb=_val-13+(neg<<3);
  108. }
  109. else if(_val<37){
  110. token=OC_DCT_VAL_CAT6;
  111. eb=_val-21+(neg<<4);
  112. }
  113. else if(_val<69){
  114. token=OC_DCT_VAL_CAT7;
  115. eb=_val-37+(neg<<5);
  116. }
  117. else{
  118. token=OC_DCT_VAL_CAT8;
  119. eb=_val-69+(neg<<9);
  120. }
  121. *_eb=eb;
  122. return token;
  123. }
  124. /*Token logging to allow a few fragments of efficient rollback.
  125. Late SKIP analysis is tied up in the tokenization process, so we need to be
  126. able to undo a fragment's tokens on a whim.*/
  127. static const unsigned char OC_ZZI_HUFF_OFFSET[64]={
  128. 0,16,16,16,16,16,32,32,
  129. 32,32,32,32,32,32,32,48,
  130. 48,48,48,48,48,48,48,48,
  131. 48,48,48,48,64,64,64,64,
  132. 64,64,64,64,64,64,64,64,
  133. 64,64,64,64,64,64,64,64,
  134. 64,64,64,64,64,64,64,64
  135. };
  136. static int oc_token_bits(oc_enc_ctx *_enc,int _huffi,int _zzi,int _token){
  137. return _enc->huff_codes[_huffi+OC_ZZI_HUFF_OFFSET[_zzi]][_token].nbits
  138. +OC_DCT_TOKEN_EXTRA_BITS[_token];
  139. }
  140. static void oc_enc_tokenlog_checkpoint(oc_enc_ctx *_enc,
  141. oc_token_checkpoint *_cp,int _pli,int _zzi){
  142. _cp->pli=_pli;
  143. _cp->zzi=_zzi;
  144. _cp->eob_run=_enc->eob_run[_pli][_zzi];
  145. _cp->ndct_tokens=_enc->ndct_tokens[_pli][_zzi];
  146. }
  147. void oc_enc_tokenlog_rollback(oc_enc_ctx *_enc,
  148. const oc_token_checkpoint *_stack,int _n){
  149. int i;
  150. for(i=_n;i-->0;){
  151. int pli;
  152. int zzi;
  153. pli=_stack[i].pli;
  154. zzi=_stack[i].zzi;
  155. _enc->eob_run[pli][zzi]=_stack[i].eob_run;
  156. _enc->ndct_tokens[pli][zzi]=_stack[i].ndct_tokens;
  157. }
  158. }
  159. static void oc_enc_token_log(oc_enc_ctx *_enc,
  160. int _pli,int _zzi,int _token,int _eb){
  161. ptrdiff_t ti;
  162. ti=_enc->ndct_tokens[_pli][_zzi]++;
  163. _enc->dct_tokens[_pli][_zzi][ti]=(unsigned char)_token;
  164. _enc->extra_bits[_pli][_zzi][ti]=(ogg_uint16_t)_eb;
  165. }
  166. static void oc_enc_eob_log(oc_enc_ctx *_enc,
  167. int _pli,int _zzi,int _run_count){
  168. int token;
  169. int eb;
  170. token=oc_make_eob_token_full(_run_count,&eb);
  171. oc_enc_token_log(_enc,_pli,_zzi,token,eb);
  172. }
  173. void oc_enc_tokenize_start(oc_enc_ctx *_enc){
  174. memset(_enc->ndct_tokens,0,sizeof(_enc->ndct_tokens));
  175. memset(_enc->eob_run,0,sizeof(_enc->eob_run));
  176. memset(_enc->dct_token_offs,0,sizeof(_enc->dct_token_offs));
  177. memset(_enc->dc_pred_last,0,sizeof(_enc->dc_pred_last));
  178. }
  179. typedef struct oc_quant_token oc_quant_token;
  180. /*A single node in the Viterbi trellis.
  181. We maintain up to 2 of these per coefficient:
  182. - A token to code if the value is zero (EOB, zero run, or combo token).
  183. - A token to code if the value is not zero (DCT value token).*/
  184. struct oc_quant_token{
  185. unsigned char next;
  186. signed char token;
  187. ogg_int16_t eb;
  188. ogg_uint32_t cost;
  189. int bits;
  190. int qc;
  191. };
  192. /*Tokenizes the AC coefficients, possibly adjusting the quantization, and then
  193. dequantizes and de-zig-zags the result.
  194. The DC coefficient is not preserved; it should be restored by the caller.*/
  195. int oc_enc_tokenize_ac(oc_enc_ctx *_enc,int _pli,ptrdiff_t _fragi,
  196. ogg_int16_t *_qdct,const ogg_uint16_t *_dequant,const ogg_int16_t *_dct,
  197. int _zzi,oc_token_checkpoint **_stack,int _acmin){
  198. oc_token_checkpoint *stack;
  199. ogg_int64_t zflags;
  200. ogg_int64_t nzflags;
  201. ogg_int64_t best_flags;
  202. ogg_uint32_t d2_accum[64];
  203. oc_quant_token tokens[64][2];
  204. ogg_uint16_t *eob_run;
  205. const unsigned char *dct_fzig_zag;
  206. ogg_uint32_t cost;
  207. int bits;
  208. int eob;
  209. int token;
  210. int eb;
  211. int next;
  212. int huffi;
  213. int zzi;
  214. int ti;
  215. int zzj;
  216. int qc;
  217. huffi=_enc->huff_idxs[_enc->state.frame_type][1][_pli+1>>1];
  218. eob_run=_enc->eob_run[_pli];
  219. memset(tokens[0],0,sizeof(tokens[0]));
  220. best_flags=nzflags=0;
  221. zflags=1;
  222. d2_accum[0]=0;
  223. zzj=64;
  224. for(zzi=OC_MINI(_zzi,63);zzi>0;zzi--){
  225. ogg_int32_t lambda;
  226. ogg_uint32_t best_cost;
  227. int best_bits=best_bits;
  228. int best_next=best_next;
  229. int best_token=best_token;
  230. int best_eb=best_eb;
  231. int best_qc=best_qc;
  232. int flush_bits;
  233. ogg_uint32_t d2;
  234. int dq;
  235. int e;
  236. int c;
  237. int s;
  238. int tj;
  239. lambda=_enc->lambda;
  240. qc=_qdct[zzi];
  241. s=-(qc<0);
  242. qc=qc+s^s;
  243. c=_dct[OC_FZIG_ZAG[zzi]];
  244. if(qc<=1){
  245. ogg_uint32_t sum_d2;
  246. int nzeros;
  247. int dc_reserve;
  248. /*The hard case: try a zero run.*/
  249. if(!qc){
  250. /*Skip runs that are already quantized to zeros.
  251. If we considered each zero coefficient in turn, we might
  252. theoretically find a better way to partition long zero runs (e.g.,
  253. a run of > 17 zeros followed by a 1 might be better coded as a short
  254. zero run followed by a combo token, rather than the longer zero
  255. token followed by a 1 value token), but zeros are so common that
  256. this becomes very computationally expensive (quadratic instead of
  257. linear in the number of coefficients), for a marginal gain.*/
  258. while(zzi>1&&!_qdct[zzi-1])zzi--;
  259. /*The distortion of coefficients originally quantized to zero is
  260. treated as zero (since we'll never quantize them to anything else).*/
  261. d2=0;
  262. }
  263. else{
  264. c=c+s^s;
  265. d2=c*(ogg_int32_t)c;
  266. }
  267. eob=eob_run[zzi];
  268. nzeros=zzj-zzi;
  269. zzj&=63;
  270. sum_d2=d2+d2_accum[zzj];
  271. d2_accum[zzi]=sum_d2;
  272. flush_bits=eob>0?oc_token_bits(_enc,huffi,zzi,oc_make_eob_token(eob)):0;
  273. /*We reserve 1 spot for combo run tokens that start in the 1st AC stack
  274. to ensure they can be extended to include the DC coefficient if
  275. necessary; this greatly simplifies stack-rewriting later on.*/
  276. dc_reserve=zzi+62>>6;
  277. best_cost=0xFFFFFFFF;
  278. for(;;){
  279. if(nzflags>>zzj&1){
  280. int cat;
  281. int val;
  282. int val_s;
  283. int zzk;
  284. int tk;
  285. next=tokens[zzj][1].next;
  286. tk=next&1;
  287. zzk=next>>1;
  288. /*Try a pure zero run to this point.*/
  289. cat=nzeros+55>>6;
  290. token=OC_DCT_SHORT_ZRL_TOKEN+cat;
  291. bits=flush_bits+oc_token_bits(_enc,huffi,zzi,token);
  292. d2=sum_d2-d2_accum[zzj];
  293. cost=d2+lambda*bits+tokens[zzj][1].cost;
  294. if(cost<=best_cost){
  295. best_next=(zzj<<1)+1;
  296. best_token=token;
  297. best_eb=nzeros-1;
  298. best_cost=cost;
  299. best_bits=bits+tokens[zzj][1].bits;
  300. best_qc=0;
  301. }
  302. if(nzeros<16+dc_reserve){
  303. val=_qdct[zzj];
  304. val_s=-(val<0);
  305. val=val+val_s^val_s;
  306. if(val<=2){
  307. /*Try a +/- 1 combo token.*/
  308. if(nzeros<6){
  309. token=OC_DCT_RUN_CAT1A+nzeros-1;
  310. eb=-val_s;
  311. }
  312. else{
  313. cat=nzeros+54>>6;
  314. token=OC_DCT_RUN_CAT1B+cat;
  315. eb=(-val_s<<cat+2)+nzeros-6-(cat<<2);
  316. }
  317. e=(_dct[OC_FZIG_ZAG[zzj]]+val_s^val_s)-_dequant[zzj];
  318. d2=e*(ogg_int32_t)e+sum_d2-d2_accum[zzj];
  319. bits=flush_bits+oc_token_bits(_enc,huffi,zzi,token);
  320. cost=d2+lambda*bits+tokens[zzk][tk].cost;
  321. if(cost<=best_cost){
  322. best_next=next;
  323. best_token=token;
  324. best_eb=eb;
  325. best_cost=cost;
  326. best_bits=bits+tokens[zzk][tk].bits;
  327. best_qc=1+val_s^val_s;
  328. }
  329. }
  330. if(nzeros<2+dc_reserve&&2<=val&&val<=4){
  331. /*Try a +/- 2/3 combo token.*/
  332. cat=nzeros>>1;
  333. token=OC_DCT_RUN_CAT2A+cat;
  334. bits=flush_bits+oc_token_bits(_enc,huffi,zzi,token);
  335. val=2+((val+val_s^val_s)>2);
  336. e=(_dct[OC_FZIG_ZAG[zzj]]+val_s^val_s)-_dequant[zzj]*val;
  337. d2=e*(ogg_int32_t)e+sum_d2-d2_accum[zzj];
  338. cost=d2+lambda*bits+tokens[zzk][tk].cost;
  339. if(cost<=best_cost){
  340. best_cost=cost;
  341. best_bits=bits+tokens[zzk][tk].bits;
  342. best_next=next;
  343. best_token=token;
  344. best_eb=(-val_s<<1+cat)+(val-2<<cat)+(nzeros-1>>1);
  345. best_qc=val+val_s^val_s;
  346. }
  347. }
  348. }
  349. /*zzj can't be coded as a zero, so stop trying to extend the run.*/
  350. if(!(zflags>>zzj&1))break;
  351. }
  352. /*We could try to consider _all_ potentially non-zero coefficients, but
  353. if we already found a bunch of them not worth coding, it's fairly
  354. unlikely they would now be worth coding from this position; skipping
  355. them saves a lot of work.*/
  356. zzj=(tokens[zzj][0].next>>1)-(tokens[zzj][0].qc!=0)&63;
  357. if(zzj==0){
  358. /*We made it all the way to the end of the block; try an EOB token.*/
  359. if(eob<4095){
  360. bits=oc_token_bits(_enc,huffi,zzi,oc_make_eob_token(eob+1))
  361. -flush_bits;
  362. }
  363. else bits=oc_token_bits(_enc,huffi,zzi,OC_DCT_EOB1_TOKEN);
  364. cost=sum_d2+bits*lambda;
  365. /*If the best route so far is still a pure zero run to the end of the
  366. block, force coding it as an EOB.
  367. Even if it's not optimal for this block, it has a good chance of
  368. getting combined with an EOB token from subsequent blocks, saving
  369. bits overall.*/
  370. if(cost<=best_cost||best_token<=OC_DCT_ZRL_TOKEN&&zzi+best_eb==63){
  371. best_next=0;
  372. /*This token is just a marker; in reality we may not emit any
  373. tokens, but update eob_run[] instead.*/
  374. best_token=OC_DCT_EOB1_TOKEN;
  375. best_eb=0;
  376. best_cost=cost;
  377. best_bits=bits;
  378. best_qc=0;
  379. }
  380. break;
  381. }
  382. nzeros=zzj-zzi;
  383. }
  384. tokens[zzi][0].next=(unsigned char)best_next;
  385. tokens[zzi][0].token=(signed char)best_token;
  386. tokens[zzi][0].eb=(ogg_int16_t)best_eb;
  387. tokens[zzi][0].cost=best_cost;
  388. tokens[zzi][0].bits=best_bits;
  389. tokens[zzi][0].qc=best_qc;
  390. zflags|=(ogg_int64_t)1<<zzi;
  391. if(qc){
  392. dq=_dequant[zzi];
  393. if(zzi<_acmin)lambda=0;
  394. e=dq-c;
  395. d2=e*(ogg_int32_t)e;
  396. token=OC_ONE_TOKEN-s;
  397. bits=flush_bits+oc_token_bits(_enc,huffi,zzi,token);
  398. zzj=zzi+1&63;
  399. tj=best_flags>>zzj&1;
  400. next=(zzj<<1)+tj;
  401. tokens[zzi][1].next=(unsigned char)next;
  402. tokens[zzi][1].token=(signed char)token;
  403. tokens[zzi][1].eb=0;
  404. tokens[zzi][1].cost=d2+lambda*bits+tokens[zzj][tj].cost;
  405. tokens[zzi][1].bits=bits+tokens[zzj][tj].bits;
  406. tokens[zzi][1].qc=1+s^s;
  407. nzflags|=(ogg_int64_t)1<<zzi;
  408. best_flags|=
  409. (ogg_int64_t)(tokens[zzi][1].cost<tokens[zzi][0].cost)<<zzi;
  410. }
  411. }
  412. else{
  413. eob=eob_run[zzi];
  414. if(zzi<_acmin)lambda=0;
  415. c=c+s^s;
  416. dq=_dequant[zzi];
  417. /*No zero run can extend past this point.*/
  418. d2_accum[zzi]=0;
  419. flush_bits=eob>0?oc_token_bits(_enc,huffi,zzi,oc_make_eob_token(eob)):0;
  420. if(qc<=2){
  421. e=2*dq-c;
  422. d2=e*(ogg_int32_t)e;
  423. best_token=OC_TWO_TOKEN-s;
  424. best_bits=flush_bits+oc_token_bits(_enc,huffi,zzi,best_token);
  425. best_cost=d2+lambda*best_bits;
  426. e-=dq;
  427. d2=e*(ogg_int32_t)e;
  428. token=OC_ONE_TOKEN-s;
  429. bits=flush_bits+oc_token_bits(_enc,huffi,zzi,token);
  430. cost=d2+lambda*bits;
  431. if(cost<=best_cost){
  432. best_token=token;
  433. best_bits=bits;
  434. best_cost=cost;
  435. qc--;
  436. }
  437. best_eb=0;
  438. }
  439. else if(qc<=3){
  440. e=3*dq-c;
  441. d2=e*(ogg_int32_t)e;
  442. best_token=OC_DCT_VAL_CAT2;
  443. best_eb=-s;
  444. best_bits=flush_bits+oc_token_bits(_enc,huffi,zzi,best_token);
  445. best_cost=d2+lambda*best_bits;
  446. e-=dq;
  447. d2=e*(ogg_int32_t)e;
  448. token=OC_TWO_TOKEN-s;
  449. bits=flush_bits+oc_token_bits(_enc,huffi,zzi,token);
  450. cost=d2+lambda*bits;
  451. if(cost<=best_cost){
  452. best_token=token;
  453. best_eb=0;
  454. best_bits=bits;
  455. best_cost=cost;
  456. qc--;
  457. }
  458. }
  459. else if(qc<=6){
  460. e=qc*dq-c;
  461. d2=e*(ogg_int32_t)e;
  462. best_token=OC_DCT_VAL_CAT2+qc-3;
  463. best_eb=-s;
  464. best_bits=flush_bits+oc_token_bits(_enc,huffi,zzi,best_token);
  465. best_cost=d2+lambda*best_bits;
  466. e-=dq;
  467. d2=e*(ogg_int32_t)e;
  468. token=best_token-1;
  469. bits=flush_bits+oc_token_bits(_enc,huffi,zzi,token);
  470. cost=d2+lambda*bits;
  471. if(cost<=best_cost){
  472. best_token=token;
  473. best_bits=bits;
  474. best_cost=cost;
  475. qc--;
  476. }
  477. }
  478. else if(qc<=8){
  479. e=qc*dq-c;
  480. d2=e*(ogg_int32_t)e;
  481. best_token=OC_DCT_VAL_CAT3;
  482. best_eb=(-s<<1)+qc-7;
  483. best_bits=flush_bits+oc_token_bits(_enc,huffi,zzi,best_token);
  484. best_cost=d2+lambda*best_bits;
  485. e=6*dq-c;
  486. d2=e*(ogg_int32_t)e;
  487. token=OC_DCT_VAL_CAT2+3;
  488. bits=flush_bits+oc_token_bits(_enc,huffi,zzi,token);
  489. cost=d2+lambda*bits;
  490. if(cost<=best_cost){
  491. best_token=token;
  492. best_eb=-s;
  493. best_bits=bits;
  494. best_cost=cost;
  495. qc=6;
  496. }
  497. }
  498. else if(qc<=12){
  499. e=qc*dq-c;
  500. d2=e*(ogg_int32_t)e;
  501. best_token=OC_DCT_VAL_CAT4;
  502. best_eb=(-s<<2)+qc-9;
  503. best_bits=flush_bits+oc_token_bits(_enc,huffi,zzi,best_token);
  504. best_cost=d2+lambda*best_bits;
  505. e=8*dq-c;
  506. d2=e*(ogg_int32_t)e;
  507. token=best_token-1;
  508. bits=flush_bits+oc_token_bits(_enc,huffi,zzi,token);
  509. cost=d2+lambda*bits;
  510. if(cost<=best_cost){
  511. best_token=token;
  512. best_eb=(-s<<1)+1;
  513. best_bits=bits;
  514. best_cost=cost;
  515. qc=8;
  516. }
  517. }
  518. else if(qc<=20){
  519. e=qc*dq-c;
  520. d2=e*(ogg_int32_t)e;
  521. best_token=OC_DCT_VAL_CAT5;
  522. best_eb=(-s<<3)+qc-13;
  523. best_bits=flush_bits+oc_token_bits(_enc,huffi,zzi,best_token);
  524. best_cost=d2+lambda*best_bits;
  525. e=12*dq-c;
  526. d2=e*(ogg_int32_t)e;
  527. token=best_token-1;
  528. bits=flush_bits+oc_token_bits(_enc,huffi,zzi,token);
  529. cost=d2+lambda*bits;
  530. if(cost<=best_cost){
  531. best_token=token;
  532. best_eb=(-s<<2)+3;
  533. best_bits=bits;
  534. best_cost=cost;
  535. qc=12;
  536. }
  537. }
  538. else if(qc<=36){
  539. e=qc*dq-c;
  540. d2=e*(ogg_int32_t)e;
  541. best_token=OC_DCT_VAL_CAT6;
  542. best_eb=(-s<<4)+qc-21;
  543. best_bits=flush_bits+oc_token_bits(_enc,huffi,zzi,best_token);
  544. best_cost=d2+lambda*best_bits;
  545. e=20*dq-c;
  546. d2=e*(ogg_int32_t)e;
  547. token=best_token-1;
  548. bits=flush_bits+oc_token_bits(_enc,huffi,zzi,token);
  549. cost=d2+lambda*bits;
  550. if(cost<=best_cost){
  551. best_token=token;
  552. best_eb=(-s<<3)+7;
  553. best_bits=bits;
  554. best_cost=cost;
  555. qc=20;
  556. }
  557. }
  558. else if(qc<=68){
  559. e=qc*dq-c;
  560. d2=e*(ogg_int32_t)e;
  561. best_token=OC_DCT_VAL_CAT7;
  562. best_eb=(-s<<5)+qc-37;
  563. best_bits=flush_bits+oc_token_bits(_enc,huffi,zzi,best_token);
  564. best_cost=d2+lambda*best_bits;
  565. e=36*dq-c;
  566. d2=e*(ogg_int32_t)e;
  567. token=best_token-1;
  568. bits=flush_bits+oc_token_bits(_enc,huffi,zzi,token);
  569. cost=d2+lambda*bits;
  570. if(cost<best_cost){
  571. best_token=token;
  572. best_eb=(-s<<4)+15;
  573. best_bits=bits;
  574. best_cost=cost;
  575. qc=36;
  576. }
  577. }
  578. else{
  579. e=qc*dq-c;
  580. d2=e*(ogg_int32_t)e;
  581. best_token=OC_DCT_VAL_CAT8;
  582. best_eb=(-s<<9)+qc-69;
  583. best_bits=flush_bits+oc_token_bits(_enc,huffi,zzi,best_token);
  584. best_cost=d2+lambda*best_bits;
  585. e=68*dq-c;
  586. d2=e*(ogg_int32_t)e;
  587. token=best_token-1;
  588. bits=flush_bits+oc_token_bits(_enc,huffi,zzi,token);
  589. cost=d2+lambda*bits;
  590. if(cost<best_cost){
  591. best_token=token;
  592. best_eb=(-s<<5)+31;
  593. best_bits=bits;
  594. best_cost=cost;
  595. qc=68;
  596. }
  597. }
  598. zzj=zzi+1&63;
  599. tj=best_flags>>zzj&1;
  600. next=(zzj<<1)+tj;
  601. tokens[zzi][1].next=(unsigned char)next;
  602. tokens[zzi][1].token=(signed char)best_token;
  603. tokens[zzi][1].eb=best_eb;
  604. tokens[zzi][1].cost=best_cost+tokens[zzj][tj].cost;
  605. tokens[zzi][1].bits=best_bits+tokens[zzj][tj].bits;
  606. tokens[zzi][1].qc=qc+s^s;
  607. nzflags|=(ogg_int64_t)1<<zzi;
  608. best_flags|=(ogg_int64_t)1<<zzi;
  609. }
  610. zzj=zzi;
  611. }
  612. /*Emit the tokens from the best path through the trellis.*/
  613. stack=*_stack;
  614. /*We blow away the first entry here so that things vectorize better.
  615. The DC coefficient is not actually stored in the array yet.*/
  616. for(zzi=0;zzi<64;zzi++)_qdct[zzi]=0;
  617. dct_fzig_zag=_enc->state.opt_data.dct_fzig_zag;
  618. zzi=1;
  619. ti=best_flags>>1&1;
  620. bits=tokens[zzi][ti].bits;
  621. do{
  622. oc_enc_tokenlog_checkpoint(_enc,stack++,_pli,zzi);
  623. eob=eob_run[zzi];
  624. if(tokens[zzi][ti].token<OC_NDCT_EOB_TOKEN_MAX){
  625. if(++eob>=4095){
  626. oc_enc_eob_log(_enc,_pli,zzi,eob);
  627. eob=0;
  628. }
  629. eob_run[zzi]=eob;
  630. /*We don't include the actual EOB cost for this block in the return value.
  631. It will be paid for by the fragment that terminates the EOB run.*/
  632. bits-=tokens[zzi][ti].bits;
  633. zzi=_zzi;
  634. break;
  635. }
  636. /*Emit pending EOB run if any.*/
  637. if(eob>0){
  638. oc_enc_eob_log(_enc,_pli,zzi,eob);
  639. eob_run[zzi]=0;
  640. }
  641. oc_enc_token_log(_enc,_pli,zzi,tokens[zzi][ti].token,tokens[zzi][ti].eb);
  642. next=tokens[zzi][ti].next;
  643. qc=tokens[zzi][ti].qc;
  644. zzj=(next>>1)-1&63;
  645. /*TODO: It may be worth saving the dequantized coefficient in the trellis
  646. above; we had to compute it to measure the error anyway.*/
  647. _qdct[dct_fzig_zag[zzj]]=(ogg_int16_t)(qc*(int)_dequant[zzj]);
  648. zzi=next>>1;
  649. ti=next&1;
  650. }
  651. while(zzi);
  652. *_stack=stack;
  653. return bits;
  654. }
  655. void oc_enc_pred_dc_frag_rows(oc_enc_ctx *_enc,
  656. int _pli,int _fragy0,int _frag_yend){
  657. const oc_fragment_plane *fplane;
  658. const oc_fragment *frags;
  659. ogg_int16_t *frag_dc;
  660. ptrdiff_t fragi;
  661. int *pred_last;
  662. int nhfrags;
  663. int fragx;
  664. int fragy;
  665. fplane=_enc->state.fplanes+_pli;
  666. frags=_enc->state.frags;
  667. frag_dc=_enc->frag_dc;
  668. pred_last=_enc->dc_pred_last[_pli];
  669. nhfrags=fplane->nhfrags;
  670. fragi=fplane->froffset+_fragy0*nhfrags;
  671. for(fragy=_fragy0;fragy<_frag_yend;fragy++){
  672. if(fragy==0){
  673. /*For the first row, all of the cases reduce to just using the previous
  674. predictor for the same reference frame.*/
  675. for(fragx=0;fragx<nhfrags;fragx++,fragi++){
  676. if(frags[fragi].coded){
  677. int ref;
  678. ref=OC_FRAME_FOR_MODE(frags[fragi].mb_mode);
  679. frag_dc[fragi]=(ogg_int16_t)(frags[fragi].dc-pred_last[ref]);
  680. pred_last[ref]=frags[fragi].dc;
  681. }
  682. }
  683. }
  684. else{
  685. const oc_fragment *u_frags;
  686. int l_ref;
  687. int ul_ref;
  688. int u_ref;
  689. u_frags=frags-nhfrags;
  690. l_ref=-1;
  691. ul_ref=-1;
  692. u_ref=u_frags[fragi].coded?OC_FRAME_FOR_MODE(u_frags[fragi].mb_mode):-1;
  693. for(fragx=0;fragx<nhfrags;fragx++,fragi++){
  694. int ur_ref;
  695. if(fragx+1>=nhfrags)ur_ref=-1;
  696. else{
  697. ur_ref=u_frags[fragi+1].coded?
  698. OC_FRAME_FOR_MODE(u_frags[fragi+1].mb_mode):-1;
  699. }
  700. if(frags[fragi].coded){
  701. int pred;
  702. int ref;
  703. ref=OC_FRAME_FOR_MODE(frags[fragi].mb_mode);
  704. /*We break out a separate case based on which of our neighbors use
  705. the same reference frames.
  706. This is somewhat faster than trying to make a generic case which
  707. handles all of them, since it reduces lots of poorly predicted
  708. jumps to one switch statement, and also lets a number of the
  709. multiplications be optimized out by strength reduction.*/
  710. switch((l_ref==ref)|(ul_ref==ref)<<1|
  711. (u_ref==ref)<<2|(ur_ref==ref)<<3){
  712. default:pred=pred_last[ref];break;
  713. case 1:
  714. case 3:pred=frags[fragi-1].dc;break;
  715. case 2:pred=u_frags[fragi-1].dc;break;
  716. case 4:
  717. case 6:
  718. case 12:pred=u_frags[fragi].dc;break;
  719. case 5:pred=(frags[fragi-1].dc+u_frags[fragi].dc)/2;break;
  720. case 8:pred=u_frags[fragi+1].dc;break;
  721. case 9:
  722. case 11:
  723. case 13:{
  724. pred=(75*frags[fragi-1].dc+53*u_frags[fragi+1].dc)/128;
  725. }break;
  726. case 10:pred=(u_frags[fragi-1].dc+u_frags[fragi+1].dc)/2;break;
  727. case 14:{
  728. pred=(3*(u_frags[fragi-1].dc+u_frags[fragi+1].dc)
  729. +10*u_frags[fragi].dc)/16;
  730. }break;
  731. case 7:
  732. case 15:{
  733. int p0;
  734. int p1;
  735. int p2;
  736. p0=frags[fragi-1].dc;
  737. p1=u_frags[fragi-1].dc;
  738. p2=u_frags[fragi].dc;
  739. pred=(29*(p0+p2)-26*p1)/32;
  740. if(abs(pred-p2)>128)pred=p2;
  741. else if(abs(pred-p0)>128)pred=p0;
  742. else if(abs(pred-p1)>128)pred=p1;
  743. }break;
  744. }
  745. frag_dc[fragi]=(ogg_int16_t)(frags[fragi].dc-pred);
  746. pred_last[ref]=frags[fragi].dc;
  747. l_ref=ref;
  748. }
  749. else l_ref=-1;
  750. ul_ref=u_ref;
  751. u_ref=ur_ref;
  752. }
  753. }
  754. }
  755. }
  756. void oc_enc_tokenize_dc_frag_list(oc_enc_ctx *_enc,int _pli,
  757. const ptrdiff_t *_coded_fragis,ptrdiff_t _ncoded_fragis,
  758. int _prev_ndct_tokens1,int _prev_eob_run1){
  759. const ogg_int16_t *frag_dc;
  760. ptrdiff_t fragii;
  761. unsigned char *dct_tokens0;
  762. unsigned char *dct_tokens1;
  763. ogg_uint16_t *extra_bits0;
  764. ogg_uint16_t *extra_bits1;
  765. ptrdiff_t ti0;
  766. ptrdiff_t ti1r;
  767. ptrdiff_t ti1w;
  768. int eob_run0;
  769. int eob_run1;
  770. int neobs1;
  771. int token;
  772. int eb;
  773. int token1=token1;
  774. int eb1=eb1;
  775. /*Return immediately if there are no coded fragments; otherwise we'd flush
  776. any trailing EOB run into the AC 1 list and never read it back out.*/
  777. if(_ncoded_fragis<=0)return;
  778. frag_dc=_enc->frag_dc;
  779. dct_tokens0=_enc->dct_tokens[_pli][0];
  780. dct_tokens1=_enc->dct_tokens[_pli][1];
  781. extra_bits0=_enc->extra_bits[_pli][0];
  782. extra_bits1=_enc->extra_bits[_pli][1];
  783. ti0=_enc->ndct_tokens[_pli][0];
  784. ti1w=ti1r=_prev_ndct_tokens1;
  785. eob_run0=_enc->eob_run[_pli][0];
  786. /*Flush any trailing EOB run for the 1st AC coefficient.
  787. This is needed to allow us to track tokens to the end of the list.*/
  788. eob_run1=_enc->eob_run[_pli][1];
  789. if(eob_run1>0)oc_enc_eob_log(_enc,_pli,1,eob_run1);
  790. /*If there was an active EOB run at the start of the 1st AC stack, read it
  791. in and decode it.*/
  792. if(_prev_eob_run1>0){
  793. token1=dct_tokens1[ti1r];
  794. eb1=extra_bits1[ti1r];
  795. ti1r++;
  796. eob_run1=oc_decode_eob_token(token1,eb1);
  797. /*Consume the portion of the run that came before these fragments.*/
  798. neobs1=eob_run1-_prev_eob_run1;
  799. }
  800. else eob_run1=neobs1=0;
  801. for(fragii=0;fragii<_ncoded_fragis;fragii++){
  802. int val;
  803. /*All tokens in the 1st AC coefficient stack are regenerated as the DC
  804. coefficients are produced.
  805. This can be done in-place; stack 1 cannot get larger.*/
  806. if(!neobs1){
  807. /*There's no active EOB run in stack 1; read the next token.*/
  808. token1=dct_tokens1[ti1r];
  809. eb1=extra_bits1[ti1r];
  810. ti1r++;
  811. if(token1<OC_NDCT_EOB_TOKEN_MAX){
  812. neobs1=oc_decode_eob_token(token1,eb1);
  813. /*It's an EOB run; add it to the current (inactive) one.
  814. Because we may have moved entries to stack 0, we may have an
  815. opportunity to merge two EOB runs in stack 1.*/
  816. eob_run1+=neobs1;
  817. }
  818. }
  819. val=frag_dc[_coded_fragis[fragii]];
  820. if(val){
  821. /*There was a non-zero DC value, so there's no alteration to stack 1
  822. for this fragment; just code the stack 0 token.*/
  823. /*Flush any pending EOB run.*/
  824. if(eob_run0>0){
  825. token=oc_make_eob_token_full(eob_run0,&eb);
  826. dct_tokens0[ti0]=(unsigned char)token;
  827. extra_bits0[ti0]=(ogg_uint16_t)eb;
  828. ti0++;
  829. eob_run0=0;
  830. }
  831. token=oc_make_dct_token_full(0,0,val,&eb);
  832. dct_tokens0[ti0]=(unsigned char)token;
  833. extra_bits0[ti0]=(ogg_uint16_t)eb;
  834. ti0++;
  835. }
  836. else{
  837. /*Zero DC value; that means the entry in stack 1 might need to be coded
  838. from stack 0.
  839. This requires a stack 1 fixup.*/
  840. if(neobs1>0){
  841. /*We're in the middle of an active EOB run in stack 1.
  842. Move it to stack 0.*/
  843. if(++eob_run0>=4095){
  844. token=oc_make_eob_token_full(eob_run0,&eb);
  845. dct_tokens0[ti0]=(unsigned char)token;
  846. extra_bits0[ti0]=(ogg_uint16_t)eb;
  847. ti0++;
  848. eob_run0=0;
  849. }
  850. eob_run1--;
  851. }
  852. else{
  853. /*No active EOB run in stack 1, so we can't extend one in stack 0.
  854. Flush it if we've got it.*/
  855. if(eob_run0>0){
  856. token=oc_make_eob_token_full(eob_run0,&eb);
  857. dct_tokens0[ti0]=(unsigned char)token;
  858. extra_bits0[ti0]=(ogg_uint16_t)eb;
  859. ti0++;
  860. eob_run0=0;
  861. }
  862. /*Stack 1 token is one of: a pure zero run token, a single
  863. coefficient token, or a zero run/coefficient combo token.
  864. A zero run token is expanded and moved to token stack 0, and the
  865. stack 1 entry dropped.
  866. A single coefficient value may be transformed into combo token that
  867. is moved to stack 0, or if it cannot be combined, it is left alone
  868. and a single length-1 zero run is emitted in stack 0.
  869. A combo token is extended and moved to stack 0.
  870. During AC coding, we restrict the run lengths on combo tokens for
  871. stack 1 to guarantee we can extend them.*/
  872. switch(token1){
  873. case OC_DCT_SHORT_ZRL_TOKEN:{
  874. if(eb1<7){
  875. dct_tokens0[ti0]=OC_DCT_SHORT_ZRL_TOKEN;
  876. extra_bits0[ti0]=(ogg_uint16_t)(eb1+1);
  877. ti0++;
  878. /*Don't write the AC coefficient back out.*/
  879. continue;
  880. }
  881. /*Fall through.*/
  882. }
  883. case OC_DCT_ZRL_TOKEN:{
  884. dct_tokens0[ti0]=OC_DCT_ZRL_TOKEN;
  885. extra_bits0[ti0]=(ogg_uint16_t)(eb1+1);
  886. ti0++;
  887. /*Don't write the AC coefficient back out.*/
  888. }continue;
  889. case OC_ONE_TOKEN:
  890. case OC_MINUS_ONE_TOKEN:{
  891. dct_tokens0[ti0]=OC_DCT_RUN_CAT1A;
  892. extra_bits0[ti0]=(ogg_uint16_t)(token1-OC_ONE_TOKEN);
  893. ti0++;
  894. /*Don't write the AC coefficient back out.*/
  895. }continue;
  896. case OC_TWO_TOKEN:
  897. case OC_MINUS_TWO_TOKEN:{
  898. dct_tokens0[ti0]=OC_DCT_RUN_CAT2A;
  899. extra_bits0[ti0]=(ogg_uint16_t)(token1-OC_TWO_TOKEN<<1);
  900. ti0++;
  901. /*Don't write the AC coefficient back out.*/
  902. }continue;
  903. case OC_DCT_VAL_CAT2:{
  904. dct_tokens0[ti0]=OC_DCT_RUN_CAT2A;
  905. extra_bits0[ti0]=(ogg_uint16_t)((eb1<<1)+1);
  906. ti0++;
  907. /*Don't write the AC coefficient back out.*/
  908. }continue;
  909. case OC_DCT_RUN_CAT1A:
  910. case OC_DCT_RUN_CAT1A+1:
  911. case OC_DCT_RUN_CAT1A+2:
  912. case OC_DCT_RUN_CAT1A+3:{
  913. dct_tokens0[ti0]=(unsigned char)(token1+1);
  914. extra_bits0[ti0]=(ogg_uint16_t)eb1;
  915. ti0++;
  916. /*Don't write the AC coefficient back out.*/
  917. }continue;
  918. case OC_DCT_RUN_CAT1A+4:{
  919. dct_tokens0[ti0]=OC_DCT_RUN_CAT1B;
  920. extra_bits0[ti0]=(ogg_uint16_t)(eb1<<2);
  921. ti0++;
  922. /*Don't write the AC coefficient back out.*/
  923. }continue;
  924. case OC_DCT_RUN_CAT1B:{
  925. if((eb1&3)<3){
  926. dct_tokens0[ti0]=OC_DCT_RUN_CAT1B;
  927. extra_bits0[ti0]=(ogg_uint16_t)(eb1+1);
  928. ti0++;
  929. /*Don't write the AC coefficient back out.*/
  930. continue;
  931. }
  932. eb1=((eb1&4)<<1)-1;
  933. /*Fall through.*/
  934. }
  935. case OC_DCT_RUN_CAT1C:{
  936. dct_tokens0[ti0]=OC_DCT_RUN_CAT1C;
  937. extra_bits0[ti0]=(ogg_uint16_t)(eb1+1);
  938. ti0++;
  939. /*Don't write the AC coefficient back out.*/
  940. }continue;
  941. case OC_DCT_RUN_CAT2A:{
  942. eb1=(eb1<<1)-1;
  943. /*Fall through.*/
  944. }
  945. case OC_DCT_RUN_CAT2B:{
  946. dct_tokens0[ti0]=OC_DCT_RUN_CAT2B;
  947. extra_bits0[ti0]=(ogg_uint16_t)(eb1+1);
  948. ti0++;
  949. /*Don't write the AC coefficient back out.*/
  950. }continue;
  951. }
  952. /*We can't merge tokens, write a short zero run and keep going.*/
  953. dct_tokens0[ti0]=OC_DCT_SHORT_ZRL_TOKEN;
  954. extra_bits0[ti0]=0;
  955. ti0++;
  956. }
  957. }
  958. if(!neobs1){
  959. /*Flush any (inactive) EOB run.*/
  960. if(eob_run1>0){
  961. token=oc_make_eob_token_full(eob_run1,&eb);
  962. dct_tokens1[ti1w]=(unsigned char)token;
  963. extra_bits1[ti1w]=(ogg_uint16_t)eb;
  964. ti1w++;
  965. eob_run1=0;
  966. }
  967. /*There's no active EOB run, so log the current token.*/
  968. dct_tokens1[ti1w]=(unsigned char)token1;
  969. extra_bits1[ti1w]=(ogg_uint16_t)eb1;
  970. ti1w++;
  971. }
  972. else{
  973. /*Otherwise consume one EOB from the current run.*/
  974. neobs1--;
  975. /*If we have more than 4095 EOBs outstanding in stack1, flush the run.*/
  976. if(eob_run1-neobs1>=4095){
  977. token=oc_make_eob_token_full(4095,&eb);
  978. dct_tokens1[ti1w]=(unsigned char)token;
  979. extra_bits1[ti1w]=(ogg_uint16_t)eb;
  980. ti1w++;
  981. eob_run1-=4095;
  982. }
  983. }
  984. }
  985. /*Save the current state.*/
  986. _enc->ndct_tokens[_pli][0]=ti0;
  987. _enc->ndct_tokens[_pli][1]=ti1w;
  988. _enc->eob_run[_pli][0]=eob_run0;
  989. _enc->eob_run[_pli][1]=eob_run1;
  990. }
  991. /*Final EOB run welding.*/
  992. void oc_enc_tokenize_finish(oc_enc_ctx *_enc){
  993. int pli;
  994. int zzi;
  995. /*Emit final EOB runs.*/
  996. for(pli=0;pli<3;pli++)for(zzi=0;zzi<64;zzi++){
  997. int eob_run;
  998. eob_run=_enc->eob_run[pli][zzi];
  999. if(eob_run>0)oc_enc_eob_log(_enc,pli,zzi,eob_run);
  1000. }
  1001. /*Merge the final EOB run of one token list with the start of the next, if
  1002. possible.*/
  1003. for(zzi=0;zzi<64;zzi++)for(pli=0;pli<3;pli++){
  1004. int old_tok1;
  1005. int old_tok2;
  1006. int old_eb1;
  1007. int old_eb2;
  1008. int new_tok;
  1009. int new_eb;
  1010. int zzj;
  1011. int plj;
  1012. ptrdiff_t ti=ti;
  1013. int run_count;
  1014. /*Make sure this coefficient has tokens at all.*/
  1015. if(_enc->ndct_tokens[pli][zzi]<=0)continue;
  1016. /*Ensure the first token is an EOB run.*/
  1017. old_tok2=_enc->dct_tokens[pli][zzi][0];
  1018. if(old_tok2>=OC_NDCT_EOB_TOKEN_MAX)continue;
  1019. /*Search for a previous coefficient that has any tokens at all.*/
  1020. old_tok1=OC_NDCT_EOB_TOKEN_MAX;
  1021. for(zzj=zzi,plj=pli;zzj>=0;zzj--){
  1022. while(plj-->0){
  1023. ti=_enc->ndct_tokens[plj][zzj]-1;
  1024. if(ti>=_enc->dct_token_offs[plj][zzj]){
  1025. old_tok1=_enc->dct_tokens[plj][zzj][ti];
  1026. break;
  1027. }
  1028. }
  1029. if(plj>=0)break;
  1030. plj=3;
  1031. }
  1032. /*Ensure its last token was an EOB run.*/
  1033. if(old_tok1>=OC_NDCT_EOB_TOKEN_MAX)continue;
  1034. /*Pull off the associated extra bits, if any, and decode the runs.*/
  1035. old_eb1=_enc->extra_bits[plj][zzj][ti];
  1036. old_eb2=_enc->extra_bits[pli][zzi][0];
  1037. run_count=oc_decode_eob_token(old_tok1,old_eb1)
  1038. +oc_decode_eob_token(old_tok2,old_eb2);
  1039. /*We can't possibly combine these into one run.
  1040. It might be possible to split them more optimally, but we'll just leave
  1041. them as-is.*/
  1042. if(run_count>=4096)continue;
  1043. /*We CAN combine them into one run.*/
  1044. new_tok=oc_make_eob_token_full(run_count,&new_eb);
  1045. _enc->dct_tokens[plj][zzj][ti]=(unsigned char)new_tok;
  1046. _enc->extra_bits[plj][zzj][ti]=(ogg_uint16_t)new_eb;
  1047. _enc->dct_token_offs[pli][zzi]++;
  1048. }
  1049. }