1 /********************************************************************
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. *
8 * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 *
9 * by the Xiph.Org Foundation http://www.xiph.org/ *
11 ********************************************************************
14 last mod: $Id: tokenize.c 16503 2009-08-22 18:14:02Z giles $
16 ********************************************************************/
23 static int oc_make_eob_token(int _run_count){
24 if(_run_count<4)return OC_DCT_EOB1_TOKEN+_run_count-1;
27 cat=OC_ILOGNZ_32(_run_count)-3;
29 return OC_DCT_REPEAT_RUN0_TOKEN+cat;
33 static int oc_make_eob_token_full(int _run_count,int *_eb){
36 return OC_DCT_EOB1_TOKEN+_run_count-1;
40 cat=OC_ILOGNZ_32(_run_count)-3;
42 *_eb=_run_count-OC_BYTE_TABLE32(4,8,16,0,cat);
43 return OC_DCT_REPEAT_RUN0_TOKEN+cat;
47 /*Returns the number of blocks ended by an EOB token.*/
48 static int oc_decode_eob_token(int _token,int _eb){
49 return (0x20820C41U>>_token*5&0x1F)+_eb;
52 /*TODO: This is now only used during DCT tokenization, and never for runs; it
53 should be simplified.*/
54 static int oc_make_dct_token_full(int _zzi,int _zzj,int _val,int *_eb){
64 /*Implement a minor restriction on stack 1 so that we know during DC fixups
65 that extending a dctrun token from stack 1 will never overflow.*/
67 if(_val<2&&zero_run<17+adj){
69 token=OC_DCT_RUN_CAT1A+zero_run-1;
73 token=OC_DCT_RUN_CAT1B;
74 eb=zero_run-6+(neg<<2);
77 token=OC_DCT_RUN_CAT1C;
78 eb=zero_run-10+(neg<<3);
81 else if(_val<4&&zero_run<3+adj){
83 token=OC_DCT_RUN_CAT2A;
87 token=OC_DCT_RUN_CAT2B;
88 eb=zero_run-2+(_val-2<<1)+(neg<<2);
92 if(zero_run<9)token=OC_DCT_SHORT_ZRL_TOKEN;
93 else token=OC_DCT_ZRL_TOKEN;
98 token=OC_ONE_TOKEN+(_val-1<<1)+neg;
102 token=OC_DCT_VAL_CAT2+_val-3;
106 token=OC_DCT_VAL_CAT3;
110 token=OC_DCT_VAL_CAT4;
114 token=OC_DCT_VAL_CAT5;
118 token=OC_DCT_VAL_CAT6;
122 token=OC_DCT_VAL_CAT7;
126 token=OC_DCT_VAL_CAT8;
133 /*Token logging to allow a few fragments of efficient rollback.
134 Late SKIP analysis is tied up in the tokenization process, so we need to be
135 able to undo a fragment's tokens on a whim.*/
137 static const unsigned char OC_ZZI_HUFF_OFFSET[64]={
138 0,16,16,16,16,16,32,32,
139 32,32,32,32,32,32,32,48,
140 48,48,48,48,48,48,48,48,
141 48,48,48,48,64,64,64,64,
142 64,64,64,64,64,64,64,64,
143 64,64,64,64,64,64,64,64,
144 64,64,64,64,64,64,64,64
147 static int oc_token_bits(oc_enc_ctx *_enc,int _huffi,int _zzi,int _token){
148 return _enc->huff_codes[_huffi+OC_ZZI_HUFF_OFFSET[_zzi]][_token].nbits
149 +OC_DCT_TOKEN_EXTRA_BITS[_token];
152 static void oc_enc_tokenlog_checkpoint(oc_enc_ctx *_enc,
153 oc_token_checkpoint *_cp,int _pli,int _zzi){
156 _cp->eob_run=_enc->eob_run[_pli][_zzi];
157 _cp->ndct_tokens=_enc->ndct_tokens[_pli][_zzi];
160 void oc_enc_tokenlog_rollback(oc_enc_ctx *_enc,
161 const oc_token_checkpoint *_stack,int _n){
168 _enc->eob_run[pli][zzi]=_stack[i].eob_run;
169 _enc->ndct_tokens[pli][zzi]=_stack[i].ndct_tokens;
173 static void oc_enc_token_log(oc_enc_ctx *_enc,
174 int _pli,int _zzi,int _token,int _eb){
176 ti=_enc->ndct_tokens[_pli][_zzi]++;
177 _enc->dct_tokens[_pli][_zzi][ti]=(unsigned char)_token;
178 _enc->extra_bits[_pli][_zzi][ti]=(ogg_uint16_t)_eb;
181 static void oc_enc_eob_log(oc_enc_ctx *_enc,
182 int _pli,int _zzi,int _run_count){
185 token=oc_make_eob_token_full(_run_count,&eb);
186 oc_enc_token_log(_enc,_pli,_zzi,token,eb);
190 void oc_enc_tokenize_start(oc_enc_ctx *_enc){
191 memset(_enc->ndct_tokens,0,sizeof(_enc->ndct_tokens));
192 memset(_enc->eob_run,0,sizeof(_enc->eob_run));
193 memset(_enc->dct_token_offs,0,sizeof(_enc->dct_token_offs));
194 memset(_enc->dc_pred_last,0,sizeof(_enc->dc_pred_last));
197 typedef struct oc_quant_token oc_quant_token;
199 /*A single node in the Viterbi trellis.
200 We maintain up to 2 of these per coefficient:
201 - A token to code if the value is zero (EOB, zero run, or combo token).
202 - A token to code if the value is not zero (DCT value token).*/
203 struct oc_quant_token{
212 /*Tokenizes the AC coefficients, possibly adjusting the quantization, and then
213 dequantizes and de-zig-zags the result.
214 The DC coefficient is not preserved; it should be restored by the caller.*/
215 int oc_enc_tokenize_ac(oc_enc_ctx *_enc,int _pli,ptrdiff_t _fragi,
216 ogg_int16_t *_qdct,const ogg_uint16_t *_dequant,const ogg_int16_t *_dct,
217 int _zzi,oc_token_checkpoint **_stack,int _acmin){
218 oc_token_checkpoint *stack;
221 ogg_int64_t best_flags;
222 ogg_uint32_t d2_accum[64];
223 oc_quant_token tokens[64][2];
224 ogg_uint16_t *eob_run;
225 const unsigned char *dct_fzig_zag;
237 huffi=_enc->huff_idxs[_enc->state.frame_type][1][_pli+1>>1];
238 eob_run=_enc->eob_run[_pli];
239 memset(tokens[0],0,sizeof(tokens[0]));
240 best_flags=nzflags=0;
244 for(zzi=OC_MINI(_zzi,63);zzi>0;zzi--){
246 ogg_uint32_t best_cost;
247 int best_bits=best_bits;
248 int best_next=best_next;
249 int best_token=best_token;
263 c=_dct[OC_FZIG_ZAG[zzi]];
268 /*The hard case: try a zero run.*/
270 /*Skip runs that are already quantized to zeros.
271 If we considered each zero coefficient in turn, we might
272 theoretically find a better way to partition long zero runs (e.g.,
273 a run of > 17 zeros followed by a 1 might be better coded as a short
274 zero run followed by a combo token, rather than the longer zero
275 token followed by a 1 value token), but zeros are so common that
276 this becomes very computationally expensive (quadratic instead of
277 linear in the number of coefficients), for a marginal gain.*/
278 while(zzi>1&&!_qdct[zzi-1])zzi--;
279 /*The distortion of coefficients originally quantized to zero is
280 treated as zero (since we'll never quantize them to anything else).*/
290 sum_d2=d2+d2_accum[zzj];
291 d2_accum[zzi]=sum_d2;
292 flush_bits=eob>0?oc_token_bits(_enc,huffi,zzi,oc_make_eob_token(eob)):0;
293 /*We reserve 1 spot for combo run tokens that start in the 1st AC stack
294 to ensure they can be extended to include the DC coefficient if
295 necessary; this greatly simplifies stack-rewriting later on.*/
296 dc_reserve=zzi+62>>6;
297 best_cost=0xFFFFFFFF;
305 next=tokens[zzj][1].next;
308 /*Try a pure zero run to this point.*/
310 token=OC_DCT_SHORT_ZRL_TOKEN+cat;
311 bits=flush_bits+oc_token_bits(_enc,huffi,zzi,token);
312 d2=sum_d2-d2_accum[zzj];
313 cost=d2+lambda*bits+tokens[zzj][1].cost;
315 best_next=(zzj<<1)+1;
319 best_bits=bits+tokens[zzj][1].bits;
322 if(nzeros<16+dc_reserve){
327 /*Try a +/- 1 combo token.*/
329 token=OC_DCT_RUN_CAT1A+nzeros-1;
334 token=OC_DCT_RUN_CAT1B+cat;
335 eb=(-val_s<<cat+2)+nzeros-6-(cat<<2);
337 e=(_dct[OC_FZIG_ZAG[zzj]]+val_s^val_s)-_dequant[zzj];
338 d2=e*(ogg_int32_t)e+sum_d2-d2_accum[zzj];
339 bits=flush_bits+oc_token_bits(_enc,huffi,zzi,token);
340 cost=d2+lambda*bits+tokens[zzk][tk].cost;
346 best_bits=bits+tokens[zzk][tk].bits;
347 best_qc=1+val_s^val_s;
350 if(nzeros<2+dc_reserve&&2<=val&&val<=4){
351 /*Try a +/- 2/3 combo token.*/
353 token=OC_DCT_RUN_CAT2A+cat;
354 bits=flush_bits+oc_token_bits(_enc,huffi,zzi,token);
355 val=2+((val+val_s^val_s)>2);
356 e=(_dct[OC_FZIG_ZAG[zzj]]+val_s^val_s)-_dequant[zzj]*val;
357 d2=e*(ogg_int32_t)e+sum_d2-d2_accum[zzj];
358 cost=d2+lambda*bits+tokens[zzk][tk].cost;
361 best_bits=bits+tokens[zzk][tk].bits;
364 best_eb=(-val_s<<1+cat)+(val-2<<cat)+(nzeros-1>>1);
365 best_qc=val+val_s^val_s;
369 /*zzj can't be coded as a zero, so stop trying to extend the run.*/
370 if(!(zflags>>zzj&1))break;
372 /*We could try to consider _all_ potentially non-zero coefficients, but
373 if we already found a bunch of them not worth coding, it's fairly
374 unlikely they would now be worth coding from this position; skipping
375 them saves a lot of work.*/
376 zzj=(tokens[zzj][0].next>>1)-(tokens[zzj][0].qc!=0)&63;
378 /*We made it all the way to the end of the block; try an EOB token.*/
380 bits=oc_token_bits(_enc,huffi,zzi,oc_make_eob_token(eob+1))
383 else bits=oc_token_bits(_enc,huffi,zzi,OC_DCT_EOB1_TOKEN);
384 cost=sum_d2+bits*lambda;
385 /*If the best route so far is still a pure zero run to the end of the
386 block, force coding it as an EOB.
387 Even if it's not optimal for this block, it has a good chance of
388 getting combined with an EOB token from subsequent blocks, saving
390 if(cost<=best_cost||best_token<=OC_DCT_ZRL_TOKEN&&zzi+best_eb==63){
392 /*This token is just a marker; in reality we may not emit any
393 tokens, but update eob_run[] instead.*/
394 best_token=OC_DCT_EOB1_TOKEN;
404 tokens[zzi][0].next=(unsigned char)best_next;
405 tokens[zzi][0].token=(signed char)best_token;
406 tokens[zzi][0].eb=(ogg_int16_t)best_eb;
407 tokens[zzi][0].cost=best_cost;
408 tokens[zzi][0].bits=best_bits;
409 tokens[zzi][0].qc=best_qc;
410 zflags|=(ogg_int64_t)1<<zzi;
413 if(zzi<_acmin)lambda=0;
416 token=OC_ONE_TOKEN-s;
417 bits=flush_bits+oc_token_bits(_enc,huffi,zzi,token);
419 tj=best_flags>>zzj&1;
421 tokens[zzi][1].next=(unsigned char)next;
422 tokens[zzi][1].token=(signed char)token;
424 tokens[zzi][1].cost=d2+lambda*bits+tokens[zzj][tj].cost;
425 tokens[zzi][1].bits=bits+tokens[zzj][tj].bits;
426 tokens[zzi][1].qc=1+s^s;
427 nzflags|=(ogg_int64_t)1<<zzi;
429 (ogg_int64_t)(tokens[zzi][1].cost<tokens[zzi][0].cost)<<zzi;
434 if(zzi<_acmin)lambda=0;
437 /*No zero run can extend past this point.*/
439 flush_bits=eob>0?oc_token_bits(_enc,huffi,zzi,oc_make_eob_token(eob)):0;
443 best_token=OC_TWO_TOKEN-s;
444 best_bits=flush_bits+oc_token_bits(_enc,huffi,zzi,best_token);
445 best_cost=d2+lambda*best_bits;
448 token=OC_ONE_TOKEN-s;
449 bits=flush_bits+oc_token_bits(_enc,huffi,zzi,token);
462 best_token=OC_DCT_VAL_CAT2;
464 best_bits=flush_bits+oc_token_bits(_enc,huffi,zzi,best_token);
465 best_cost=d2+lambda*best_bits;
468 token=OC_TWO_TOKEN-s;
469 bits=flush_bits+oc_token_bits(_enc,huffi,zzi,token);
482 best_token=OC_DCT_VAL_CAT2+qc-3;
484 best_bits=flush_bits+oc_token_bits(_enc,huffi,zzi,best_token);
485 best_cost=d2+lambda*best_bits;
489 bits=flush_bits+oc_token_bits(_enc,huffi,zzi,token);
501 best_token=OC_DCT_VAL_CAT3;
502 best_eb=(-s<<1)+qc-7;
503 best_bits=flush_bits+oc_token_bits(_enc,huffi,zzi,best_token);
504 best_cost=d2+lambda*best_bits;
507 token=OC_DCT_VAL_CAT2+3;
508 bits=flush_bits+oc_token_bits(_enc,huffi,zzi,token);
521 best_token=OC_DCT_VAL_CAT4;
522 best_eb=(-s<<2)+qc-9;
523 best_bits=flush_bits+oc_token_bits(_enc,huffi,zzi,best_token);
524 best_cost=d2+lambda*best_bits;
528 bits=flush_bits+oc_token_bits(_enc,huffi,zzi,token);
541 best_token=OC_DCT_VAL_CAT5;
542 best_eb=(-s<<3)+qc-13;
543 best_bits=flush_bits+oc_token_bits(_enc,huffi,zzi,best_token);
544 best_cost=d2+lambda*best_bits;
548 bits=flush_bits+oc_token_bits(_enc,huffi,zzi,token);
561 best_token=OC_DCT_VAL_CAT6;
562 best_eb=(-s<<4)+qc-21;
563 best_bits=flush_bits+oc_token_bits(_enc,huffi,zzi,best_token);
564 best_cost=d2+lambda*best_bits;
568 bits=flush_bits+oc_token_bits(_enc,huffi,zzi,token);
581 best_token=OC_DCT_VAL_CAT7;
582 best_eb=(-s<<5)+qc-37;
583 best_bits=flush_bits+oc_token_bits(_enc,huffi,zzi,best_token);
584 best_cost=d2+lambda*best_bits;
588 bits=flush_bits+oc_token_bits(_enc,huffi,zzi,token);
601 best_token=OC_DCT_VAL_CAT8;
602 best_eb=(-s<<9)+qc-69;
603 best_bits=flush_bits+oc_token_bits(_enc,huffi,zzi,best_token);
604 best_cost=d2+lambda*best_bits;
608 bits=flush_bits+oc_token_bits(_enc,huffi,zzi,token);
619 tj=best_flags>>zzj&1;
621 tokens[zzi][1].next=(unsigned char)next;
622 tokens[zzi][1].token=(signed char)best_token;
623 tokens[zzi][1].eb=best_eb;
624 tokens[zzi][1].cost=best_cost+tokens[zzj][tj].cost;
625 tokens[zzi][1].bits=best_bits+tokens[zzj][tj].bits;
626 tokens[zzi][1].qc=qc+s^s;
627 nzflags|=(ogg_int64_t)1<<zzi;
628 best_flags|=(ogg_int64_t)1<<zzi;
632 /*Emit the tokens from the best path through the trellis.*/
634 /*We blow away the first entry here so that things vectorize better.
635 The DC coefficient is not actually stored in the array yet.*/
636 for(zzi=0;zzi<64;zzi++)_qdct[zzi]=0;
637 dct_fzig_zag=_enc->state.opt_data.dct_fzig_zag;
640 bits=tokens[zzi][ti].bits;
642 oc_enc_tokenlog_checkpoint(_enc,stack++,_pli,zzi);
644 if(tokens[zzi][ti].token<OC_NDCT_EOB_TOKEN_MAX){
646 oc_enc_eob_log(_enc,_pli,zzi,eob);
650 /*We don't include the actual EOB cost for this block in the return value.
651 It will be paid for by the fragment that terminates the EOB run.*/
652 bits-=tokens[zzi][ti].bits;
656 /*Emit pending EOB run if any.*/
658 oc_enc_eob_log(_enc,_pli,zzi,eob);
661 oc_enc_token_log(_enc,_pli,zzi,tokens[zzi][ti].token,tokens[zzi][ti].eb);
662 next=tokens[zzi][ti].next;
663 qc=tokens[zzi][ti].qc;
665 /*TODO: It may be worth saving the dequantized coefficient in the trellis
666 above; we had to compute it to measure the error anyway.*/
667 _qdct[dct_fzig_zag[zzj]]=(ogg_int16_t)(qc*(int)_dequant[zzj]);
676 void oc_enc_pred_dc_frag_rows(oc_enc_ctx *_enc,
677 int _pli,int _fragy0,int _frag_yend){
678 const oc_fragment_plane *fplane;
679 const oc_fragment *frags;
680 ogg_int16_t *frag_dc;
686 fplane=_enc->state.fplanes+_pli;
687 frags=_enc->state.frags;
688 frag_dc=_enc->frag_dc;
689 pred_last=_enc->dc_pred_last[_pli];
690 nhfrags=fplane->nhfrags;
691 fragi=fplane->froffset+_fragy0*nhfrags;
692 for(fragy=_fragy0;fragy<_frag_yend;fragy++){
694 /*For the first row, all of the cases reduce to just using the previous
695 predictor for the same reference frame.*/
696 for(fragx=0;fragx<nhfrags;fragx++,fragi++){
697 if(frags[fragi].coded){
699 ref=OC_FRAME_FOR_MODE(frags[fragi].mb_mode);
700 frag_dc[fragi]=(ogg_int16_t)(frags[fragi].dc-pred_last[ref]);
701 pred_last[ref]=frags[fragi].dc;
706 const oc_fragment *u_frags;
710 u_frags=frags-nhfrags;
713 u_ref=u_frags[fragi].coded?OC_FRAME_FOR_MODE(u_frags[fragi].mb_mode):-1;
714 for(fragx=0;fragx<nhfrags;fragx++,fragi++){
716 if(fragx+1>=nhfrags)ur_ref=-1;
718 ur_ref=u_frags[fragi+1].coded?
719 OC_FRAME_FOR_MODE(u_frags[fragi+1].mb_mode):-1;
721 if(frags[fragi].coded){
724 ref=OC_FRAME_FOR_MODE(frags[fragi].mb_mode);
725 /*We break out a separate case based on which of our neighbors use
726 the same reference frames.
727 This is somewhat faster than trying to make a generic case which
728 handles all of them, since it reduces lots of poorly predicted
729 jumps to one switch statement, and also lets a number of the
730 multiplications be optimized out by strength reduction.*/
731 switch((l_ref==ref)|(ul_ref==ref)<<1|
732 (u_ref==ref)<<2|(ur_ref==ref)<<3){
733 default:pred=pred_last[ref];break;
735 case 3:pred=frags[fragi-1].dc;break;
736 case 2:pred=u_frags[fragi-1].dc;break;
739 case 12:pred=u_frags[fragi].dc;break;
740 case 5:pred=(frags[fragi-1].dc+u_frags[fragi].dc)/2;break;
741 case 8:pred=u_frags[fragi+1].dc;break;
745 pred=(75*frags[fragi-1].dc+53*u_frags[fragi+1].dc)/128;
747 case 10:pred=(u_frags[fragi-1].dc+u_frags[fragi+1].dc)/2;break;
749 pred=(3*(u_frags[fragi-1].dc+u_frags[fragi+1].dc)
750 +10*u_frags[fragi].dc)/16;
757 p0=frags[fragi-1].dc;
758 p1=u_frags[fragi-1].dc;
759 p2=u_frags[fragi].dc;
760 pred=(29*(p0+p2)-26*p1)/32;
761 if(abs(pred-p2)>128)pred=p2;
762 else if(abs(pred-p0)>128)pred=p0;
763 else if(abs(pred-p1)>128)pred=p1;
766 frag_dc[fragi]=(ogg_int16_t)(frags[fragi].dc-pred);
767 pred_last[ref]=frags[fragi].dc;
778 void oc_enc_tokenize_dc_frag_list(oc_enc_ctx *_enc,int _pli,
779 const ptrdiff_t *_coded_fragis,ptrdiff_t _ncoded_fragis,
780 int _prev_ndct_tokens1,int _prev_eob_run1){
781 const ogg_int16_t *frag_dc;
783 unsigned char *dct_tokens0;
784 unsigned char *dct_tokens1;
785 ogg_uint16_t *extra_bits0;
786 ogg_uint16_t *extra_bits1;
797 /*Return immediately if there are no coded fragments; otherwise we'd flush
798 any trailing EOB run into the AC 1 list and never read it back out.*/
799 if(_ncoded_fragis<=0)return;
800 frag_dc=_enc->frag_dc;
801 dct_tokens0=_enc->dct_tokens[_pli][0];
802 dct_tokens1=_enc->dct_tokens[_pli][1];
803 extra_bits0=_enc->extra_bits[_pli][0];
804 extra_bits1=_enc->extra_bits[_pli][1];
805 ti0=_enc->ndct_tokens[_pli][0];
806 ti1w=ti1r=_prev_ndct_tokens1;
807 eob_run0=_enc->eob_run[_pli][0];
808 /*Flush any trailing EOB run for the 1st AC coefficient.
809 This is needed to allow us to track tokens to the end of the list.*/
810 eob_run1=_enc->eob_run[_pli][1];
811 if(eob_run1>0)oc_enc_eob_log(_enc,_pli,1,eob_run1);
812 /*If there was an active EOB run at the start of the 1st AC stack, read it
814 if(_prev_eob_run1>0){
815 token1=dct_tokens1[ti1r];
816 eb1=extra_bits1[ti1r];
818 eob_run1=oc_decode_eob_token(token1,eb1);
819 /*Consume the portion of the run that came before these fragments.*/
820 neobs1=eob_run1-_prev_eob_run1;
822 else eob_run1=neobs1=0;
823 for(fragii=0;fragii<_ncoded_fragis;fragii++){
825 /*All tokens in the 1st AC coefficient stack are regenerated as the DC
826 coefficients are produced.
827 This can be done in-place; stack 1 cannot get larger.*/
829 /*There's no active EOB run in stack 1; read the next token.*/
830 token1=dct_tokens1[ti1r];
831 eb1=extra_bits1[ti1r];
833 if(token1<OC_NDCT_EOB_TOKEN_MAX){
834 neobs1=oc_decode_eob_token(token1,eb1);
835 /*It's an EOB run; add it to the current (inactive) one.
836 Because we may have moved entries to stack 0, we may have an
837 opportunity to merge two EOB runs in stack 1.*/
841 val=frag_dc[_coded_fragis[fragii]];
843 /*There was a non-zero DC value, so there's no alteration to stack 1
844 for this fragment; just code the stack 0 token.*/
845 /*Flush any pending EOB run.*/
847 token=oc_make_eob_token_full(eob_run0,&eb);
848 dct_tokens0[ti0]=(unsigned char)token;
849 extra_bits0[ti0]=(ogg_uint16_t)eb;
853 token=oc_make_dct_token_full(0,0,val,&eb);
854 dct_tokens0[ti0]=(unsigned char)token;
855 extra_bits0[ti0]=(ogg_uint16_t)eb;
859 /*Zero DC value; that means the entry in stack 1 might need to be coded
861 This requires a stack 1 fixup.*/
863 /*We're in the middle of an active EOB run in stack 1.
864 Move it to stack 0.*/
865 if(++eob_run0>=4095){
866 token=oc_make_eob_token_full(eob_run0,&eb);
867 dct_tokens0[ti0]=(unsigned char)token;
868 extra_bits0[ti0]=(ogg_uint16_t)eb;
875 /*No active EOB run in stack 1, so we can't extend one in stack 0.
876 Flush it if we've got it.*/
878 token=oc_make_eob_token_full(eob_run0,&eb);
879 dct_tokens0[ti0]=(unsigned char)token;
880 extra_bits0[ti0]=(ogg_uint16_t)eb;
884 /*Stack 1 token is one of: a pure zero run token, a single
885 coefficient token, or a zero run/coefficient combo token.
886 A zero run token is expanded and moved to token stack 0, and the
887 stack 1 entry dropped.
888 A single coefficient value may be transformed into combo token that
889 is moved to stack 0, or if it cannot be combined, it is left alone
890 and a single length-1 zero run is emitted in stack 0.
891 A combo token is extended and moved to stack 0.
892 During AC coding, we restrict the run lengths on combo tokens for
893 stack 1 to guarantee we can extend them.*/
895 case OC_DCT_SHORT_ZRL_TOKEN:{
897 dct_tokens0[ti0]=OC_DCT_SHORT_ZRL_TOKEN;
898 extra_bits0[ti0]=(ogg_uint16_t)(eb1+1);
900 /*Don't write the AC coefficient back out.*/
905 case OC_DCT_ZRL_TOKEN:{
906 dct_tokens0[ti0]=OC_DCT_ZRL_TOKEN;
907 extra_bits0[ti0]=(ogg_uint16_t)(eb1+1);
909 /*Don't write the AC coefficient back out.*/
912 case OC_MINUS_ONE_TOKEN:{
913 dct_tokens0[ti0]=OC_DCT_RUN_CAT1A;
914 extra_bits0[ti0]=(ogg_uint16_t)(token1-OC_ONE_TOKEN);
916 /*Don't write the AC coefficient back out.*/
919 case OC_MINUS_TWO_TOKEN:{
920 dct_tokens0[ti0]=OC_DCT_RUN_CAT2A;
921 extra_bits0[ti0]=(ogg_uint16_t)(token1-OC_TWO_TOKEN<<1);
923 /*Don't write the AC coefficient back out.*/
925 case OC_DCT_VAL_CAT2:{
926 dct_tokens0[ti0]=OC_DCT_RUN_CAT2A;
927 extra_bits0[ti0]=(ogg_uint16_t)((eb1<<1)+1);
929 /*Don't write the AC coefficient back out.*/
931 case OC_DCT_RUN_CAT1A:
932 case OC_DCT_RUN_CAT1A+1:
933 case OC_DCT_RUN_CAT1A+2:
934 case OC_DCT_RUN_CAT1A+3:{
935 dct_tokens0[ti0]=(unsigned char)(token1+1);
936 extra_bits0[ti0]=(ogg_uint16_t)eb1;
938 /*Don't write the AC coefficient back out.*/
940 case OC_DCT_RUN_CAT1A+4:{
941 dct_tokens0[ti0]=OC_DCT_RUN_CAT1B;
942 extra_bits0[ti0]=(ogg_uint16_t)(eb1<<2);
944 /*Don't write the AC coefficient back out.*/
946 case OC_DCT_RUN_CAT1B:{
948 dct_tokens0[ti0]=OC_DCT_RUN_CAT1B;
949 extra_bits0[ti0]=(ogg_uint16_t)(eb1+1);
951 /*Don't write the AC coefficient back out.*/
957 case OC_DCT_RUN_CAT1C:{
958 dct_tokens0[ti0]=OC_DCT_RUN_CAT1C;
959 extra_bits0[ti0]=(ogg_uint16_t)(eb1+1);
961 /*Don't write the AC coefficient back out.*/
963 case OC_DCT_RUN_CAT2A:{
967 case OC_DCT_RUN_CAT2B:{
968 dct_tokens0[ti0]=OC_DCT_RUN_CAT2B;
969 extra_bits0[ti0]=(ogg_uint16_t)(eb1+1);
971 /*Don't write the AC coefficient back out.*/
974 /*We can't merge tokens, write a short zero run and keep going.*/
975 dct_tokens0[ti0]=OC_DCT_SHORT_ZRL_TOKEN;
981 /*Flush any (inactive) EOB run.*/
983 token=oc_make_eob_token_full(eob_run1,&eb);
984 dct_tokens1[ti1w]=(unsigned char)token;
985 extra_bits1[ti1w]=(ogg_uint16_t)eb;
989 /*There's no active EOB run, so log the current token.*/
990 dct_tokens1[ti1w]=(unsigned char)token1;
991 extra_bits1[ti1w]=(ogg_uint16_t)eb1;
995 /*Otherwise consume one EOB from the current run.*/
997 /*If we have more than 4095 EOBs outstanding in stack1, flush the run.*/
998 if(eob_run1-neobs1>=4095){
999 token=oc_make_eob_token_full(4095,&eb);
1000 dct_tokens1[ti1w]=(unsigned char)token;
1001 extra_bits1[ti1w]=(ogg_uint16_t)eb;
1007 /*Save the current state.*/
1008 _enc->ndct_tokens[_pli][0]=ti0;
1009 _enc->ndct_tokens[_pli][1]=ti1w;
1010 _enc->eob_run[_pli][0]=eob_run0;
1011 _enc->eob_run[_pli][1]=eob_run1;
1014 /*Final EOB run welding.*/
1015 void oc_enc_tokenize_finish(oc_enc_ctx *_enc){
1018 /*Emit final EOB runs.*/
1019 for(pli=0;pli<3;pli++)for(zzi=0;zzi<64;zzi++){
1021 eob_run=_enc->eob_run[pli][zzi];
1022 if(eob_run>0)oc_enc_eob_log(_enc,pli,zzi,eob_run);
1024 /*Merge the final EOB run of one token list with the start of the next, if
1026 for(zzi=0;zzi<64;zzi++)for(pli=0;pli<3;pli++){
1037 /*Make sure this coefficient has tokens at all.*/
1038 if(_enc->ndct_tokens[pli][zzi]<=0)continue;
1039 /*Ensure the first token is an EOB run.*/
1040 old_tok2=_enc->dct_tokens[pli][zzi][0];
1041 if(old_tok2>=OC_NDCT_EOB_TOKEN_MAX)continue;
1042 /*Search for a previous coefficient that has any tokens at all.*/
1043 old_tok1=OC_NDCT_EOB_TOKEN_MAX;
1044 for(zzj=zzi,plj=pli;zzj>=0;zzj--){
1046 ti=_enc->ndct_tokens[plj][zzj]-1;
1047 if(ti>=_enc->dct_token_offs[plj][zzj]){
1048 old_tok1=_enc->dct_tokens[plj][zzj][ti];
1055 /*Ensure its last token was an EOB run.*/
1056 if(old_tok1>=OC_NDCT_EOB_TOKEN_MAX)continue;
1057 /*Pull off the associated extra bits, if any, and decode the runs.*/
1058 old_eb1=_enc->extra_bits[plj][zzj][ti];
1059 old_eb2=_enc->extra_bits[pli][zzi][0];
1060 run_count=oc_decode_eob_token(old_tok1,old_eb1)
1061 +oc_decode_eob_token(old_tok2,old_eb2);
1062 /*We can't possibly combine these into one run.
1063 It might be possible to split them more optimally, but we'll just leave
1065 if(run_count>=4096)continue;
1066 /*We CAN combine them into one run.*/
1067 new_tok=oc_make_eob_token_full(run_count,&new_eb);
1068 _enc->dct_tokens[plj][zzj][ti]=(unsigned char)new_tok;
1069 _enc->extra_bits[plj][zzj][ti]=(ogg_uint16_t)new_eb;
1070 _enc->dct_token_offs[pli][zzi]++;