[xiph-commits] r14270 - branches/theora-thusnelda/lib/enc

xiphmont at svn.xiph.org xiphmont at svn.xiph.org
Tue Dec 4 16:52:41 PST 2007


Author: xiphmont
Date: 2007-12-04 16:52:39 -0800 (Tue, 04 Dec 2007)
New Revision: 14270

Modified:
   branches/theora-thusnelda/lib/enc/codec_internal.h
   branches/theora-thusnelda/lib/enc/dct_decode.c
   branches/theora-thusnelda/lib/enc/dct_encode.c
   branches/theora-thusnelda/lib/enc/encode.c
   branches/theora-thusnelda/lib/enc/encoder_huffman.h
   branches/theora-thusnelda/lib/enc/frinit.c
Log:
Moving work to repository to work on another machine; this revision
does not build, does not work.

First pass of replacing multi-stage dct token marshalling.



Modified: branches/theora-thusnelda/lib/enc/codec_internal.h
===================================================================
--- branches/theora-thusnelda/lib/enc/codec_internal.h	2007-12-04 10:28:46 UTC (rev 14269)
+++ branches/theora-thusnelda/lib/enc/codec_internal.h	2007-12-05 00:52:39 UTC (rev 14270)
@@ -106,22 +106,19 @@
   ogg_int32_t   y;
 } mv_t;
 
-typedef struct fragment {
+typedef struct fragment fragment_t;
+
+struct fragment {
   int coded;
   coding_mode_t mode;
   mv_t mv;
   ogg_int16_t pred_dc;
   ogg_int16_t dct[64];
-
   unsigned char nonzero;
-  ogg_uint32_t  token_list[128];
-  unsigned char tokens_coded;
-  unsigned char coeffs_packed;
-  unsigned char tokens_packed;
-
   ogg_uint32_t buffer_index;
 
-} fragment_t;
+  fragment_t *next;
+};
 
 typedef struct macroblock {
   fragment_t *y[4]; // MV (raster) order
@@ -160,6 +157,7 @@
   unsigned char   *recon;
   unsigned char   *golden;
   unsigned char   *lastrecon;
+  ogg_uint32_t    frame_size;
 
   /* SuperBlock, MacroBLock and Fragment Information */
   fragment_t      *frag[3];
@@ -197,26 +195,28 @@
   /*********************************************************************/
   /* Token Buffers */
 
-  ogg_uint32_t    *OptimisedTokenListEb; /* Optimised token list extra bits */
-  unsigned char   *OptimisedTokenList;   /* Optimised token list. */
-  unsigned char   *OptimisedTokenListHi; /* Optimised token list huffman
-                                             table index */
+  fragment_t      *coded_head;
+  fragment_t      *coded_tail;
+  ogg_uint32_t     coded_y;
+  ogg_uint32_t     coded_total;
 
-  unsigned char   *OptimisedTokenListPl; /* Plane to which the token
-                                             belongs Y = 0 or UV = 1 */
-  ogg_int32_t      OptimisedTokenCount;           /* Count of Optimized tokens */
-  ogg_uint32_t     RunHuffIndex;         /* Huffman table in force at
-                                             the start of a run */
-  ogg_uint32_t     RunPlaneIndex;        /* The plane (Y=0 UV=1) to
-                                             which the first token in
-                                             an EOB run belonged. */
+  unsigned char   *dct_token_storage;
+  ogg_uint16_t    *dct_token_eb_storage;
+  unsigned char   *dct_token[64];
+  ogg_uint16_t    *dct_token_eb[64];
 
+  ogg_uint32_t     dct_token_pre[64];
+  ogg_uint32_t     dct_token_count[64];
+  ogg_uint32_t     dct_token_ycount[64];
+
+  ogg_uint32_t     dc_bits[2][DC_HUFF_CHOICES];
+  ogg_uint32_t     ac_bits[2][AC_HUFF_CHOICES];
+
+  ogg_int32_t      ModeCount[MAX_MODES]; /* Frequency count of modes */
+
   ogg_uint32_t     MVBits_0; /* count of bits used by MV coding mode 0 */
   ogg_uint32_t     MVBits_1; /* count of bits used by MV coding mode 1 */
-  ogg_int32_t      ModeCount[MAX_MODES]; /* Frequency count of modes */
 
-  int              CodedBlockIndex;
-  fragment_t     **CodedBlockList;           
 
   /********************************************************************/
   /* Setup */
@@ -282,7 +282,7 @@
 
 extern void fdct_short ( ogg_int16_t *InputData, ogg_int16_t *OutputData );
 
-extern ogg_uint32_t DPCMTokenizeBlock (CP_INSTANCE *cpi, fragment_t *fp);
+extern void DPCMTokenize (CP_INSTANCE *cpi);
 
 extern void TransformQuantizeBlock (CP_INSTANCE *cpi, fragment_t *fp) ;
 

Modified: branches/theora-thusnelda/lib/enc/dct_decode.c
===================================================================
--- branches/theora-thusnelda/lib/enc/dct_decode.c	2007-12-04 10:28:46 UTC (rev 14269)
+++ branches/theora-thusnelda/lib/enc/dct_decode.c	2007-12-05 00:52:39 UTC (rev 14270)
@@ -35,7 +35,7 @@
   }
 }
 
-static void ExpandBlock ( CP_INSTANCE *cpi, fragment_t *fp, ogg_int32_t FragmentNumber){
+static void ExpandBlock ( CP_INSTANCE *cpi, fragment_t *fp){
   int            mode = fp->mode;
   int            qi = cpi->BaseQ; // temporary 
   int            plane = (fp<cpi->frag[1] ? 0 : (fp<cpi->frag[2] ? 1 : 2));
@@ -177,25 +177,6 @@
   }
 }
 
-static void CopyNotRecon( CP_INSTANCE *cpi, unsigned char * DestReconPtr,
-			  unsigned char * SrcReconPtr ) {
-  ogg_uint32_t  i,plane;
-  fragment_t *fp = cpi->frag[0];
-  
-  /* Copy over only updated blocks.*/
-  for(plane=0;plane<3;plane++){  
-    int PlaneLineStep = cpi->stride[plane];
-    for ( i = 0; i < cpi->frag_n[plane]; i++,fp++ ) {
-      if ( !fp->coded ) {
-	int pi= fp->buffer_index;
-	unsigned char *src = &SrcReconPtr[ pi ];
-	unsigned char *dst = &DestReconPtr[ pi ];
-	dsp_copy8x8 (cpi->dsp, src, dst, PlaneLineStep);
-      }
-    }
-  }
-}
-
 static void FilterHoriz__c(unsigned char * PixelPtr,
 			   ogg_int32_t LineLength,
 			   ogg_int16_t *BoundingValuePtr){
@@ -479,23 +460,15 @@
 }
 
 void ReconRefFrames (CP_INSTANCE *cpi){
-  ogg_int32_t i;
-  unsigned char *SwapReconBuffersTemp;
-
-  /* Inverse DCT and reconstitute buffer in thisframe */
-  for(i=0;i<cpi->frag_total;i++)
-    ExpandBlock( cpi, cpi->frag[0]+i, i );
-
-  /* Copy the current reconstruction back to the last frame recon buffer. */
-  if(cpi->CodedBlockIndex > (ogg_int32_t) (cpi->frag_total >> 1)){
-    SwapReconBuffersTemp = cpi->recon;
-    cpi->recon = cpi->lastrecon;
-    cpi->lastrecon = SwapReconBuffersTemp;
-    CopyNotRecon( cpi, cpi->lastrecon, cpi->recon );
-  }else{
-    CopyRecon( cpi, cpi->lastrecon, cpi->recon );
+  fragment_t *fp = cpi->coded_tail;
+  
+  while(fp){
+    ExpandBlock( cpi, fp );
+    fp = fp->next;
   }
 
+  memcpy(cpi->lastrecon,cpi->recon,sizeof(*cpi->recon)*cpi->frame_size);
+
   /* Apply a loop filter to edge pixels of updated blocks */
   LoopFilter(cpi);
 

Modified: branches/theora-thusnelda/lib/enc/dct_encode.c
===================================================================
--- branches/theora-thusnelda/lib/enc/dct_encode.c	2007-12-04 10:28:46 UTC (rev 14269)
+++ branches/theora-thusnelda/lib/enc/dct_encode.c	2007-12-05 00:52:39 UTC (rev 14270)
@@ -20,221 +20,290 @@
 #include "dsp.h"
 #include "quant_lookup.h"
 
-#include <stdio.h>
-static int ModeUsesMC[MAX_MODES] = { 0, 0, 1, 1, 1, 0, 1, 1 };
+static int acoffset[64]={
+  16,16,16,16,16,16, 32,32,
+  32,32,32,32,32,32,32, 48,
+  48,48,48,48,48,48,48,48,
+  48,48,48,48, 64,64,64,64,
+  64,64,64,64,64,64,64,64,
+  64,64,64,64,64,64,64,64,
+  64,64,64,64,64,64,64,64};
 
-static unsigned char TokenizeDctValue (ogg_int16_t DataValue,
-                                       ogg_uint32_t *TokenListPtr ){
+/* plane == 0 for Y, 1 for UV */
+static void add_token(CP_INSTANCE *cpi, int plane, int coeff, 
+		      unsigned char token, ogg_uint16_t eb,
+		      int prepend){
+  if(prepend){
+    int pre = cpi->dct_token_pre[coeff]++;
+    *(cpi->dct_token[coeff] - pre)  = token;
+    *(cpi->dct_token_eb[coeff] - pre)  = eb;
+  }else{
+    cpi->dct_token[pos][cpi->dct_token_count[pos]] = token;
+    cpi->dct_token_eb[pos][cpi->dct_token_count[pos]] = eb;
+    cpi->dct_token_count[pos]++;
+  }
+
+  if(coeff == 0){
+    /* DC */
+    for ( i = 0; i < DC_HUFF_CHOICES; i++)
+      cpi->dc_bits[plane][i] += cpi->HuffCodeLengthArray_VP3x[i][token];
+  }else{
+    /* AC */
+    int offset = acoffset[coeff];
+    for ( i = 0; i < AC_HUFF_CHOICES; i++)
+      cpc->ac_bits[plane][i] += cpi->HuffCodeLengthArray_VP3x[offset+i][token];
+  }
+
+  if(!plane)cpi->dct_token_ycount[coeff]++;
+}
+
+static void emit_eob_run(CP_INSTANCE *cpi, int plane, int pos, int run, int prepend){
+  if ( run <= 3 ) {
+    if ( run == 1 ) {
+      add_token(cpi, plane, pos, DCT_EOB_TOKEN, 0, prepend);
+    } else if ( cpi->RunLength == 2 ) {
+      add_token(cpi, plane, pos, DCT_EOB_PAIR_TOKEN, 0, prepend);
+    } else {
+      add_token(cpi, plane, pos, DCT_EOB_TRIPLE_TOKEN, 0, prepend);
+    }
+    
+  } else {
+    
+    if ( run < 8 ) {
+      add_token(cpi, plane, pos, DCT_REPEAT_RUN_TOKEN, run-4, prepend);
+    } else if ( cpi->RunLength < 16 ) {
+      add_token(cpi, plane, pos, DCT_REPEAT_RUN2_TOKEN, run-8, prepend);
+    } else if ( cpi->RunLength < 32 ) {
+      add_token(cpi, plane, pos, DCT_REPEAT_RUN3_TOKEN, run-16, prepend);
+    } else if ( cpi->RunLength < 4096) {
+      add_token(cpi, plane, pos, DCT_REPEAT_RUN4_TOKEN, run, prepend);
+    }
+  }
+}
+
+static void add_eob_run(CP_INSTANCE *cpi, int plane, int pos, 
+			int *eob_run, int *eob_pre){
+  run = eob_run[pos];
+  if(!run) return;
+  
+  /* pre-runs for a coefficient group > DC are a special case; they're
+     handled when groups are tied together at the end of tokenization */
+  if(pos > 0 !cpi->dct_token_count[pos]){
+    /* no tokens emitted yet, this is a pre-run */
+    eob_pre[pos] += run;
+  }else{
+    emit_eob_run(cpi,plane,pos,run,0);
+  }
+  
+  eob_run[pos] = 0;
+}
+
+static void TokenizeDctValue (CP_INSTANCE *cpi, 
+			      int plane, 
+			      int coeff,
+			      ogg_int16_t DataValue){
+
   int AbsDataVal = abs(DataValue);
   int neg = (DataValue<0);
-  /* Values are tokenised as category value and a number of additional
-     bits that define the position within the category.  */
 
-  if ( AbsDataVal == 0 ) return 0;
-
   if ( AbsDataVal == 1 ){
 
-    TokenListPtr[0] = (neg ? MINUS_ONE_TOKEN : ONE_TOKEN);
-    return 1;
+    add_token(cpi, plane, pos, (neg ? MINUS_ONE_TOKEN : ONE_TOKEN), 0, 0);
 
   } else if ( AbsDataVal == 2 ) {
 
-    TokenListPtr[0] = (neg ? MINUS_TWO_TOKEN : TWO_TOKEN);
-    return 1;
+    add_token(cpi, plane, pos, (neg ? MINUS_TWO_TOKEN : TWO_TOKEN), 0, 0);
 
   } else if ( AbsDataVal <= MAX_SINGLE_TOKEN_VALUE ) {
 
-    TokenListPtr[0] = LOW_VAL_TOKENS + (AbsDataVal - DCT_VAL_CAT2_MIN);
-    TokenListPtr[1] = neg;
-    return 2;
+    add_token(cpi, plane, pos, LOW_VAL_TOKENS + (AbsDataVal - DCT_VAL_CAT2_MIN), neg, 0);
 
   } else if ( AbsDataVal <= 8 ) {
 
-    /* Bit 1 determines sign, Bit 0 the value */
-    TokenListPtr[0] = DCT_VAL_CATEGORY3;
-    TokenListPtr[1] = (AbsDataVal - DCT_VAL_CAT3_MIN) + (neg << 1);
-    return 2;
+    add_token(cpi, plane, pos, DCT_VAL_CATEGORY3, (AbsDataVal - DCT_VAL_CAT3_MIN) + (neg << 1), 0);
 
   } else if ( AbsDataVal <= 12 ) {
 
-    /* Bit 2 determines sign, Bit 0-2 the value */
-    TokenListPtr[0] = DCT_VAL_CATEGORY4;
-    TokenListPtr[1] = (AbsDataVal - DCT_VAL_CAT4_MIN) + (neg << 2);
-    return 2;
+    add_token(cpi, plane, pos, DCT_VAL_CATEGORY4, (AbsDataVal - DCT_VAL_CAT4_MIN) + (neg << 2), 0);
 
   } else if ( AbsDataVal <= 20 ) {
 
-    /* Bit 3 determines sign, Bit 0-2 the value */
-    TokenListPtr[0] = DCT_VAL_CATEGORY5;
-    TokenListPtr[1] = (AbsDataVal - DCT_VAL_CAT5_MIN) + (neg << 3);
-    return 2;
+    add_token(cpi, plane, pos, DCT_VAL_CATEGORY5, (AbsDataVal - DCT_VAL_CAT5_MIN) + (neg << 3), 0);
 
   } else if ( AbsDataVal <= 36 ) {
 
-    /* Bit 4 determines sign, Bit 0-3 the value */
-    TokenListPtr[0] = DCT_VAL_CATEGORY6;
-    TokenListPtr[1] = (AbsDataVal - DCT_VAL_CAT6_MIN) + (neg << 4);
-    return 2;
+    add_token(cpi, plane, pos, DCT_VAL_CATEGORY6, (AbsDataVal - DCT_VAL_CAT6_MIN) + (neg << 4), 0);
 
   } else if ( AbsDataVal <= 68 ) {
 
-    /* Bit 5 determines sign, Bit 0-4 the value */
-    TokenListPtr[0] = DCT_VAL_CATEGORY7;
-    TokenListPtr[1] = (AbsDataVal - DCT_VAL_CAT7_MIN) + (neg << 5);
-    return 2;
+    add_token(cpi, plane, pos, DCT_VAL_CATEGORY7, (AbsDataVal - DCT_VAL_CAT7_MIN) + (neg << 5), 0);
 
   } else {
 
-    /* Bit 9 determines sign, Bit 0-8 the value */
-    TokenListPtr[0] = DCT_VAL_CATEGORY8;
-    TokenListPtr[1] = (AbsDataVal - DCT_VAL_CAT8_MIN) + (neg <<9 );
-    return 2;
+    add_token(cpi, plane, pos, DCT_VAL_CATEGORY8, (AbsDataVal - DCT_VAL_CAT8_MIN) + (neg << 9), 0);
 
   } 
 }
 
-static unsigned char TokenizeDctRunValue (unsigned char RunLength,
-                                          ogg_int16_t DataValue,
-                                          ogg_uint32_t *TokenListPtr ){
-  unsigned char tokens_added = 0;
+static void TokenizeDctRunValue (CP_INSTANCE *cpi, 
+				 int plane, 
+				 int coeff,
+				 unsigned char RunLength,
+				 ogg_int16_t DataValue){
+
   ogg_uint32_t AbsDataVal = abs( (ogg_int32_t)DataValue );
 
   /* Values are tokenised as category value and a number of additional
      bits  that define the category.  */
-  if ( DataValue == 0 ) return 0;
+
   if ( AbsDataVal == 1 ) {
-    /* Zero runs of 1-5 */
-    if ( RunLength <= 5 ) {
-      TokenListPtr[0] = DCT_RUN_CATEGORY1 + (RunLength - 1);
-      if ( DataValue > 0 )
-        TokenListPtr[1] = 0;
-      else
-        TokenListPtr[1] = 1;
-    } else if ( RunLength <= 9 ) {
-      /* Zero runs of 6-9 */
-      TokenListPtr[0] = DCT_RUN_CATEGORY1B;
-      if ( DataValue > 0 )
-        TokenListPtr[1] = (RunLength - 6);
-      else
-        TokenListPtr[1] = 0x04 + (RunLength - 6);
-    } else {
-      /* Zero runs of 10-17 */
-      TokenListPtr[0] = DCT_RUN_CATEGORY1C;
-      if ( DataValue > 0 )
-        TokenListPtr[1] = (RunLength - 10);
-      else
-        TokenListPtr[1] = 0x08 + (RunLength - 10);
-    }
-    tokens_added = 2;
+
+    if ( RunLength <= 5 ) 
+      add_token(cpi,plane,coeff, DCT_RUN_CATEGORY1 + (RunLength - 1), ((DataValue&0x8000)>>15), 0);
+    else if ( RunLength <= 9 ) 
+      add_token(cpi,plane,coeff, DCT_RUN_CATEGORY1B, RunLength - 6 + ((DataValue&0x8000)>>13), 0);
+    else 
+      add_token(cpi,plane,coeff, DCT_RUN_CATEGORY1C, RunLength - 10 + ((DataValue&0x8000)>>12), 0);
+
   } else if ( AbsDataVal <= 3 ) {
-    if ( RunLength == 1 ) {
-      TokenListPtr[0] = DCT_RUN_CATEGORY2;
 
-      /* Extra bits token bit 1 indicates sign, bit 0 indicates value */
-      if ( DataValue > 0 )
-        TokenListPtr[1] = (AbsDataVal - 2);
-      else
-        TokenListPtr[1] = (0x02) + (AbsDataVal - 2);
-      tokens_added = 2;
-    }else{
-      TokenListPtr[0] = DCT_RUN_CATEGORY2 + 1;
+    if ( RunLength == 1 ) 
+      add_token(cpi,plane,coeff, DCT_RUN_CATEGORY2, AbsDataVal - 2 + ((DataValue&0x8000)>>14), 0);
+    else
+      add_token(cpi,plane,coeff, DCT_RUN_CATEGORY2B, 
+		((DataValue&0x8000)>>13) + ((AbsDataVal-2)<<1) + RunLength - 2, 0);
 
-      /* Extra bits token. */
-      /* bit 2 indicates sign, bit 1 indicates value, bit 0 indicates
-         run length */
-      if ( DataValue > 0 )
-        TokenListPtr[1] = ((AbsDataVal - 2) << 1) + (RunLength - 2);
-      else
-        TokenListPtr[1] = (0x04) + ((AbsDataVal - 2) << 1) + (RunLength - 2);
-      tokens_added = 2;
-    }
-  } else  {
-    tokens_added = 2;  /* ERROR */
-    /*IssueWarning( "Bad Input to TokenizeDctRunValue" );*/
   }
-
-  /* Return the total number of tokens added */
-  return tokens_added;
 }
 
-static unsigned char TokenizeDctBlock (fragment_t *fp){
+static int tokenize_pass (fragment_t *fp, int pos, int plane, int *eob_run, int *eob_pre){
   ogg_int16_t DC = fp->pred_dc;
   ogg_int16_t *RawData = fp->dct;
   ogg_uint32_t *TokenListPtr = fp->token_list;
-  ogg_uint32_t i;
-  unsigned char  run_count;
-  unsigned char  token_count = 0;     /* Number of tokens crated. */
-  ogg_uint32_t AbsData;
+  ogg_int16_t val = (i ? RawData[i] : DC);
+  int zero_run;
+  int i = pos;
   
-  /* Tokenize the block */
-  for( i = 0; i < BLOCK_SIZE; i++ ){
-    ogg_int16_t val = (i ? RawData[i] : DC);
-    run_count = 0;
+  while( (i < BLOCK_SIZE) && (!val) )
+    val = RawData[++i];
 
-    /* Look for a zero run.  */
-    /* NOTE the use of & instead of && which is faster (and
-       equivalent) in this instance. */
-    /* NO, NO IT ISN'T --Monty */
-    while( (i < BLOCK_SIZE) && (!val) ){
-      run_count++;
-      i++;
-      val = RawData[i];
-    }
+  if ( i == BLOCK_SIZE ){
 
-    /* If we have reached the end of the block then code EOB */
-    if ( i == BLOCK_SIZE ){
-      TokenListPtr[token_count] = DCT_EOB_TOKEN;
-      token_count++;
+    eob_run[pos]++;
+    if(eob_run[pos] >= 4095)
+      add_eob_run(cpi,plane,pos,eob_run,eob_pre);
+    return i;
+
+  }
+  
+  fp->nonzero = i;
+  add_eob_run(cpi,plane,pos,eob_run,eob_pre); /* if any */
+  
+  zero_run = i-pos;
+  if (zero_run){
+    ogg_uint32_t absval = abs(val);
+    if ( ((absval == 1) && (zero_run <= 17)) ||
+	 ((absval <= 3) && (zero_run <= 3)) ) {
+      TokenizeDctRunValue( cpi, plane, pos, run_count, val);
     }else{
-      /* If we have a short zero run followed by a low data value code
-         the two as a composite token. */
-      if ( run_count ){
-        AbsData = abs(val);
+      if ( zero_run <= 8 )
+	add_token(cpi, plane, pos, DCT_SHORT_ZRL_TOKEN, run_count - 1);
+      else
+	add_token(cpi, pos, DCT_ZRL_TOKEN, run_count - 1);
+      TokenizeDctValue( cpi, plane, pos, val );
+    }
+  }else{
+    TokenizeDctValue( cpi, plane, pos, val );
+  }
 
-        if ( ((AbsData == 1) && (run_count <= 17)) ||
-             ((AbsData <= 3) && (run_count <= 3)) ) {
-          /* Tokenise the run and subsequent value combination value */
-          token_count += TokenizeDctRunValue( run_count, val,
-                                              &TokenListPtr[token_count] );
-        }else{
+  return i+1;
+}
 
-        /* Else if we have a long non-EOB run or a run followed by a
-           value token > MAX_RUN_VAL then code the run and token
-           seperately */
-          if ( run_count <= 8 )
-            TokenListPtr[token_count] = DCT_SHORT_ZRL_TOKEN;
-          else
-            TokenListPtr[token_count] = DCT_ZRL_TOKEN;
+void DPCMTokenize (CP_INSTANCE *cpi){
+  fragment_t *fp = cpi->coded_tail;
+  int eob_run[64];
+  int eob_pre[64];
 
-          token_count++;
-          TokenListPtr[token_count] = run_count - 1;
-          token_count++;
+  memset(eob_run, 0 sizeof(eob_run));
+  memset(eob_pre, 0 sizeof(eob_pre));
+  memset(cpi->dc_bits, 0, sizeof(cpi->dc_bits));
+  memset(cpi->ac_bits, 0, sizeof(cpi->ac_bits));
+  memset(cpi->dct_token_count, 0, sizeof(cpi->dct_token_count));
+  memset(cpi->dct_token_ycount, 0, sizeof(cpi->dct_token_ycount));
+  memset(cpi->dct_token_pre, 0, sizeof(cpi->dct_token_pre));
 
-          /* Now tokenize the value */
-          token_count += TokenizeDctValue( val, &TokenListPtr[token_count] );
-        }
-      }else{
-        /* Else there was NO zero run. */
-        /* Tokenise the value  */
-        token_count += TokenizeDctValue( val, &TokenListPtr[token_count] );
-      }
-    }
+  /* Tokenise the dct data. */
+  while(fp){
+    int coeff = 0;
+    int plane = (fp >= cpi->frag[1]);
+    
+    fp->nonzero = 0;
+    while(coeff < BLOCK_SIZE)
+      coeff = tokenize_pass (fp, coeff, plane, eob_run, eob_pre);
+
+    fp = fp->next;
   }
+    
+  /* sew together group pre- and post- EOB runs */
+  /* there are two categories of runs:
+     1) runs that end a group and may or may not extend into the next
+     2) pre runs that begin at group position 0
+     
+     Category 2 is a special case that requires potentially
+     'prepending' tokens to the group. */
+   
+  {
+    int run = 0;
+    int run_coeff = 0;
+    int run_frag = 0;
 
-  /* Return the total number of tokens (including additional bits
-     tokens) used. */
-  return token_count;
-}
+    for(i=0;i<BLOCK_SIZE;i++){
+      run += cpi->dct_eob_pre[i];
 
-ogg_uint32_t DPCMTokenizeBlock (CP_INSTANCE *cpi,
-				fragment_t *fp){
-  /* Tokenise the dct data. */
+      if(cpi->dct_token_count[i]){
 
-  int token_count = TokenizeDctBlock(fp);  
-  fp->tokens_coded = token_count;
+	/* first code the spanning/preceeding run */
+	while(run){
+	  int v = (run < 4095 ? run : 4095);
+	  int plane = (run_frag >= cpi->coded_y);
 
-  /* Return number of pixels coded (i.e. 8x8). */
-  return BLOCK_SIZE;
+	  if(run_coeff == i){
+	    /* prepend to current group */
+	    emit_eob_run(cpi, plane, run_coeff, v, 1);
+	  }else{
+	    /* append to indicated coeff group; it is possible the run
+	       spans multiple coeffs, it will always be an append as
+	       intervening groups would have a zero token count */
+	    emit_eob_run(cpi, plane, run_coeff, v, 0);
+	  }
+	  
+	  run_frag += v;
+	  run -= v;
+	  while(run_frag >= cpi->coded_total){
+	    run_coeff++;
+	    run_frag -= cpi->coded_total;
+	  }
+	}
+
+	run_frag = cpi->coded_total - cpi->dct_eob_run[i];       
+      }
+      run += cpi->dct_eob_run[i];
+
+    }
+    
+    /* tie off last run */
+    while(run){
+      int v = (run < 4095 ? run : 4095);
+      int plane = (run_frag >= cpi->coded_y);
+      emit_eob_run(cpi, plane, run_coeff, v,0);
+      run_frag += v;
+      run -= v;
+      while(run_frag >= cpi->coded_total){
+	run_coeff++;
+	run_frag -= cpi->coded_total;
+      }
+    }
+  }
 }
 
 static int AllZeroDctData( ogg_int16_t * QuantList ){
@@ -247,6 +316,8 @@
   return 1;
 }
 
+static int ModeUsesMC[MAX_MODES] = { 0, 0, 1, 1, 1, 0, 1, 1 };
+
 static void BlockUpdateDifference (CP_INSTANCE * cpi, 
 				   unsigned char *FiltPtr,
 				   ogg_int16_t *DctInputPtr, 

Modified: branches/theora-thusnelda/lib/enc/encode.c
===================================================================
--- branches/theora-thusnelda/lib/enc/encode.c	2007-12-04 10:28:46 UTC (rev 14269)
+++ branches/theora-thusnelda/lib/enc/encode.c	2007-12-05 00:52:39 UTC (rev 14270)
@@ -159,8 +159,11 @@
       for ( B=0; B<4; B++, frag++ ) {
 	fragment_t *fp = sp->f[frag];
 
-	/* Does Block lie in frame: */
 	if ( fp && fp->coded ) {
+
+	  cpi->coded_total++;
+	  if(plane == 0)
+	    cpi->coded_y++;
 	  
 	  /* transform and quantize block */
 	  TransformQuantizeBlock( cpi, fp );
@@ -169,9 +172,14 @@
 	     generated after DCT) If not then mark it and the
 	     assosciated MB as coded. */
 	  if ( fp->coded ) {
-	    /* Create linear list of coded block indices */
-	    cpi->CodedBlockList[cpi->CodedBlockIndex++] = fp;
-	    
+
+	    if(cpi->coded_head){
+	      cpi->coded_head->next = fp;
+	      cpi->coded_head = fp;
+	    }else{
+	      cpi->coded_head = cpi->coded_tail = fp;
+	    }
+
 	    /* MB is still coded */
 	    coded = 1;
 	    mode = fp->mode;
@@ -182,173 +190,117 @@
      
       /* If the MB is marked as coded and we are in the Y plane */
       /* collect coding metric */
-      if ( coded && plane == 0 ){
-	cpi->ModeCount[mode] ++;
+      if ( coded && plane == 0 ) cpi->ModeCount[mode] ++;
 
-      }
     }  
   }
   return 0;
 }
 
 static void EncodeDcTokenList (CP_INSTANCE *cpi) {
-  ogg_int32_t   i,j;
-  ogg_uint32_t  Token;
-  ogg_uint32_t  ExtraBitsToken;
-  ogg_uint32_t  HuffIndex;
-
-  ogg_uint32_t  BestDcBits;
-  ogg_uint32_t  DcHuffChoice[2];
-  ogg_uint32_t  EntropyTableBits[2][DC_HUFF_CHOICES];
-
+  int i,j;
+  int best;
+  int huff[2];
   oggpack_buffer *opb=cpi->oggbuffer;
 
-  /* Clear table data structure */
-  memset ( EntropyTableBits, 0, sizeof(ogg_uint32_t)*DC_HUFF_CHOICES*2 );
-
-  /* Analyse token list to see which is the best entropy table to use */
-  for ( i = 0; i < cpi->OptimisedTokenCount; i++ ) {
-    /* Count number of bits for each table option */
-    Token = (ogg_uint32_t)cpi->OptimisedTokenList[i];
-    for ( j = 0; j < DC_HUFF_CHOICES; j++ ){
-      EntropyTableBits[cpi->OptimisedTokenListPl[i]][j] +=
-        cpi->HuffCodeLengthArray_VP3x[DC_HUFF_OFFSET + j][Token];
+  /* Work out which table options are best for DC */
+  for(plane = 0; plane<2; plane++){
+    best = cpi->dc_bits[plane][0];
+    huff[plane] = 0;
+    for ( j = 1; j < DC_HUFF_CHOICES; j++ ) {
+      if ( cpi->dc_bits[plane][j] < best ) {
+	best = cpi->dc_bits[plane][j];
+	huff[plane] = j + DC_HUFF_OFFSET;
+      }
     }
+    oggpackB_write( opb, huff[plane] - DC_HUFF_OFFSET, DC_HUFF_CHOICE_BITS );
   }
-
-  /* Work out which table option is best for Y */
-  BestDcBits = EntropyTableBits[0][0];
-  DcHuffChoice[0] = 0;
-  for ( j = 1; j < DC_HUFF_CHOICES; j++ ) {
-    if ( EntropyTableBits[0][j] < BestDcBits ) {
-      BestDcBits = EntropyTableBits[0][j];
-      DcHuffChoice[0] = j;
-    }
+  
+  /* Encode the token list */
+  for ( i = 0; i < cpi->dct_token_count[0]; i++){
+    int token = cpi->dct_token[0][i];
+    int eb = cpi->dct_token_eb[0][i];
+    int plane = (i >= cpi->dct_token_ycount[0]);
+    oggpackB_write( opb, cpi->HuffCodeArray_VP3x[huff[plane]][token],
+                    cpi->HuffCodeLengthArray_VP3x[huff[plane]][token] );
+    
+    if ( cpi->ExtraBitLengths_VP3x[token] > 0 ) 
+      oggpackB_write( opb, eb, cpi->ExtraBitLengths_VP3x[token] );
   }
+}
 
-  /* Add the DC huffman table choice to the bitstream */
-  oggpackB_write( opb, DcHuffChoice[0], DC_HUFF_CHOICE_BITS );
+static void EncodeAcGroup(CP_INSTANCE *cpi, int group, int huff_offset, int *huffchoice){
+  int i;
+  
+  int c = 0;
+  int y = cpi->dct_token_ycount[group];
+  
+  /* pre-tokens, then post-tokens */
 
-  /* Work out which table option is best for UV */
-  BestDcBits = EntropyTableBits[1][0];
-  DcHuffChoice[1] = 0;
-  for ( j = 1; j < DC_HUFF_CHOICES; j++ ) {
-    if ( EntropyTableBits[1][j] < BestDcBits ) {
-      BestDcBits = EntropyTableBits[1][j];
-      DcHuffChoice[1] = j;
-    }
-  }
+  for(j=1; j<=cpi->dct_token_pre[group]; j++){
+    int token = *(cpi->dct_token[group]-j);
+    int eb = *(cpi->dct_token_eb[group]-j);
+    int plane = (c >= y);
+    int hi = huff_offset + huffchoice[plane];
 
-  /* Add the DC huffman table choice to the bitstream */
-  oggpackB_write( opb, DcHuffChoice[1], DC_HUFF_CHOICE_BITS );
+    oggpackB_write( opb, cpi->pb.HuffCodeArray_VP3x[hi][token],
+		    cpi->pb.HuffCodeLengthArray_VP3x[hi][token] );
 
-  /* Encode the token list */
-  for ( i = 0; i < cpi->OptimisedTokenCount; i++ ) {
+    if ( cpi->pb.ExtraBitLengths_VP3x[token] > 0 ) 
+      oggpackB_write( opb, eb,cpi->pb.ExtraBitLengths_VP3x[token] );
+    
+    c++;
+  }
 
-    /* Get the token and extra bits */
-    Token = (ogg_uint32_t)cpi->OptimisedTokenList[i];
-    ExtraBitsToken = (ogg_uint32_t)cpi->OptimisedTokenListEb[i];
+  for(j=0; j<cpi->dct_token_count[group]; j++){
+    int token = cpi->dct_token[group][j];
+    int eb = cpi->dct_token_eb[group][j];
+    int plane = (c >= y);
 
-    /* Select the huffman table */
-    if ( cpi->OptimisedTokenListPl[i] == 0)
-      HuffIndex = (ogg_uint32_t)DC_HUFF_OFFSET + (ogg_uint32_t)DcHuffChoice[0];
-    else
-      HuffIndex = (ogg_uint32_t)DC_HUFF_OFFSET + (ogg_uint32_t)DcHuffChoice[1];
+    int hi = huff_offset + huffchoice[plane];
 
-    /* Add the bits to the encode holding buffer. */
-    oggpackB_write( opb, cpi->HuffCodeArray_VP3x[HuffIndex][Token],
-                     (ogg_uint32_t)cpi->
-                     HuffCodeLengthArray_VP3x[HuffIndex][Token] );
-
-    /* If the token is followed by an extra bits token then code it */
-    if ( cpi->ExtraBitLengths_VP3x[Token] > 0 ) {
-      /* Add the bits to the encode holding buffer.  */
-      oggpackB_write( opb, ExtraBitsToken,
-                       (ogg_uint32_t)cpi->ExtraBitLengths_VP3x[Token] );
-    }
-
+    oggpackB_write( opb, cpi->pb.HuffCodeArray_VP3x[hi][token],
+		    cpi->pb.HuffCodeLengthArray_VP3x[hi][token] );
+    
+    if ( cpi->pb.ExtraBitLengths_VP3x[token] > 0 ) 
+      oggpackB_write( opb, eb,cpi->pb.ExtraBitLengths_VP3x[token] );
+    
+    c++;
   }
-
-  /* Reset the count of second order optimised tokens */
-  cpi->OptimisedTokenCount = 0;
 }
 
 static void EncodeAcTokenList (CP_INSTANCE *cpi) {
-  ogg_int32_t   i,j;
-  ogg_uint32_t  Token;
-  ogg_uint32_t  ExtraBitsToken;
-  ogg_uint32_t  HuffIndex;
-
-  ogg_uint32_t  BestAcBits;
-  ogg_uint32_t  AcHuffChoice[2];
-  ogg_uint32_t  EntropyTableBits[2][AC_HUFF_CHOICES];
-
+  int i,j;
+  int best;
+  int huff[2];
   oggpack_buffer *opb=cpi->oggbuffer;
 
-  memset ( EntropyTableBits, 0, sizeof(ogg_uint32_t)*AC_HUFF_CHOICES*2 );
-
-  /* Analyse token list to see which is the best entropy table to use */
-  for ( i = 0; i < cpi->OptimisedTokenCount; i++ ) {
-    /* Count number of bits for each table option */
-    Token = (ogg_uint32_t)cpi->OptimisedTokenList[i];
-    HuffIndex = cpi->OptimisedTokenListHi[i];
-    for ( j = 0; j < AC_HUFF_CHOICES; j++ ) {
-      EntropyTableBits[cpi->OptimisedTokenListPl[i]][j] +=
-        cpi->HuffCodeLengthArray_VP3x[HuffIndex + j][Token];
+  /* Work out which table options are best for AC */
+  for(plane = 0; plane<2; plane++){
+    best = cpi->ac_bits[plane][0];
+    huff[plane] = 0;
+    for ( j = 1; j < AC_HUFF_CHOICES; j++ ) {
+      if ( cpi->ac_bits[plane][j] < best ){
+	best = cpi->ac_bits[plane][j];
+	huff[plane] = j + AC_HUFF_OFFSET;
+      }
     }
+    oggpackB_write( opb, huff[plane] - AC_HUFF_OFFSET, AC_HUFF_CHOICE_BITS );
   }
+  
+  /* encode dct tokens, group 1 through group 63 in the four AC ranges */
+  for(i=1;i<=AC_TABLE_2_THRESH;i++)
+    EncodeAcGroup(cpi, i, 0, huff);
 
-  /* Select the best set of AC tables for Y */
-  BestAcBits = EntropyTableBits[0][0];
-  AcHuffChoice[0] = 0;
-  for ( j = 1; j < AC_HUFF_CHOICES; j++ ) {
-    if ( EntropyTableBits[0][j] < BestAcBits ) {
-      BestAcBits = EntropyTableBits[0][j];
-      AcHuffChoice[0] = j;
-    }
-  }
+  for(;i<=AC_TABLE_3_THRESH;i++)
+    EncodeAcGroup(cpi, i, AC_HUFF_CHOICES, huff);
 
-  /* Add the AC-Y huffman table choice to the bitstream */
-  oggpackB_write( opb, AcHuffChoice[0], AC_HUFF_CHOICE_BITS );
+  for(;i<=AC_TABLE_4_THRESH;i++)
+    EncodeAcGroup(cpi, i, AC_HUFF_CHOICES*2, huff);
 
-  /* Select the best set of AC tables for UV */
-  BestAcBits = EntropyTableBits[1][0];
-  AcHuffChoice[1] = 0;
-  for ( j = 1; j < AC_HUFF_CHOICES; j++ ) {
-    if ( EntropyTableBits[1][j] < BestAcBits ) {
-      BestAcBits = EntropyTableBits[1][j];
-      AcHuffChoice[1] = j;
-    }
-  }
+  for(;i<BLOCK_SIZE;i++)
+    EncodeAcGroup(cpi, i, AC_HUFF_CHOICES*3, huff);
 
-  /* Add the AC-UV huffman table choice to the bitstream */
-  oggpackB_write( opb, AcHuffChoice[1], AC_HUFF_CHOICE_BITS );
-
-  /* Encode the token list */
-  for ( i = 0; i < cpi->OptimisedTokenCount; i++ ) {
-    /* Get the token and extra bits */
-    Token = (ogg_uint32_t)cpi->OptimisedTokenList[i];
-    ExtraBitsToken = (ogg_uint32_t)cpi->OptimisedTokenListEb[i];
-
-    /* Select the huffman table */
-    HuffIndex = (ogg_uint32_t)cpi->OptimisedTokenListHi[i] +
-      AcHuffChoice[cpi->OptimisedTokenListPl[i]];
-
-    /* Add the bits to the encode holding buffer. */
-    oggpackB_write( opb, cpi->HuffCodeArray_VP3x[HuffIndex][Token],
-		    (ogg_uint32_t)cpi->
-		    HuffCodeLengthArray_VP3x[HuffIndex][Token] );
-
-    /* If the token is followed by an extra bits token then code it */
-    if ( cpi->ExtraBitLengths_VP3x[Token] > 0 ) {
-      /* Add the bits to the encode holding buffer. */
-      oggpackB_write( opb, ExtraBitsToken,
-		      (ogg_uint32_t)cpi->ExtraBitLengths_VP3x[Token] );
-    }
-  }
-
-  /* Reset the count of second order optimised tokens */
-  cpi->OptimisedTokenCount = 0;
 }
 
 static const ogg_uint32_t NoOpModeWords[8] = {0,1,2,3,4,5,6,7};
@@ -511,276 +463,42 @@
   }
 }
 
-static void PackEOBRun( CP_INSTANCE *cpi) {
-  if(cpi->RunLength == 0)
-        return;
+void EncodeData(CP_INSTANCE *cpi){
 
-  /* Note the appropriate EOB or EOB run token and any extra bits in
-     the optimised token list.  Use the huffman index assosciated with
-     the first token in the run */
+  /* reset all coding metadata  */
+  memset(cpi->ModeCount, 0, MAX_MODES*sizeof(*cpi->ModeCount));
+  cpi->coded_head = cpi->coded_tail = NULL;
+  cpi->coded_y = cpi->coded_total = 0;
 
-  /* Mark out which plane the block belonged to */
-  cpi->OptimisedTokenListPl[cpi->OptimisedTokenCount] =
-    (unsigned char)cpi->RunPlaneIndex;
+  dsp_save_fpu (cpi->dsp);
 
-  /* Note the huffman index to be used */
-  cpi->OptimisedTokenListHi[cpi->OptimisedTokenCount] =
-    (unsigned char)cpi->RunHuffIndex;
+  /* Encode and tokenise the Y, U and V components */
+  CodePlane(cpi, 0);
+  CodePlane(cpi, 1);
+  CodePlane(cpi, 2);
 
-  if ( cpi->RunLength <= 3 ) {
-    if ( cpi->RunLength == 1 ) {
-      cpi->OptimisedTokenList[cpi->OptimisedTokenCount] = DCT_EOB_TOKEN;
-    } else if ( cpi->RunLength == 2 ) {
-      cpi->OptimisedTokenList[cpi->OptimisedTokenCount] = DCT_EOB_PAIR_TOKEN;
-    } else {
-      cpi->OptimisedTokenList[cpi->OptimisedTokenCount] = DCT_EOB_TRIPLE_TOKEN;
-    }
+  if(cpi->coded_head)
+    cpi->coded_head->next = NULL; /* cap off the end of the coded block list */
+  
+  PredictDC(cpi);
+  DPCMTokenize(cpi);
 
-    cpi->RunLength = 0;
-
-  } else {
-
-    /* Choose a token appropriate to the run length. */
-    if ( cpi->RunLength < 8 ) {
-      cpi->OptimisedTokenList[cpi->OptimisedTokenCount] =
-        DCT_REPEAT_RUN_TOKEN;
-      cpi->OptimisedTokenListEb[cpi->OptimisedTokenCount] =
-        cpi->RunLength - 4;
-      cpi->RunLength = 0;
-    } else if ( cpi->RunLength < 16 ) {
-      cpi->OptimisedTokenList[cpi->OptimisedTokenCount] =
-        DCT_REPEAT_RUN2_TOKEN;
-      cpi->OptimisedTokenListEb[cpi->OptimisedTokenCount] =
-        cpi->RunLength - 8;
-      cpi->RunLength = 0;
-    } else if ( cpi->RunLength < 32 ) {
-      cpi->OptimisedTokenList[cpi->OptimisedTokenCount] =
-        DCT_REPEAT_RUN3_TOKEN;
-      cpi->OptimisedTokenListEb[cpi->OptimisedTokenCount] =
-        cpi->RunLength - 16;
-      cpi->RunLength = 0;
-    } else if ( cpi->RunLength < 4096) {
-      cpi->OptimisedTokenList[cpi->OptimisedTokenCount] =
-        DCT_REPEAT_RUN4_TOKEN;
-      cpi->OptimisedTokenListEb[cpi->OptimisedTokenCount] =
-        cpi->RunLength;
-      cpi->RunLength = 0;
-    }
-
-  }
-
-  cpi->OptimisedTokenCount++;
-  /* Reset run EOB length */
-  cpi->RunLength = 0;
-}
-
-static int TokenCoeffs( ogg_uint32_t Token,
-		 ogg_int32_t ExtraBits ){
-  if ( Token == DCT_EOB_TOKEN )
-    return BLOCK_SIZE;
-
-  /* Is the token is a combination run and value token. */
-  if ( Token >= DCT_RUN_CATEGORY1 ){
-    /* Expand the token and additional bits to a zero run length and
-       data value.  */
-    if ( Token < DCT_RUN_CATEGORY2 ) {
-      /* Decoding method depends on token */
-      if ( Token < DCT_RUN_CATEGORY1B ) {
-        /* Step on by the zero run length */
-        return(Token - DCT_RUN_CATEGORY1) + 2;
-      } else if ( Token == DCT_RUN_CATEGORY1B ) {
-        /* Bits 0-1 determines the zero run length */
-        return 7 + (ExtraBits & 0x03);
-      }else{
-        /* Bits 0-2 determines the zero run length */
-        return 11 + (ExtraBits & 0x07);
-      }
-    }else{
-      /* If token == DCT_RUN_CATEGORY2 we have a single 0 followed by
-         a value */
-      if ( Token == DCT_RUN_CATEGORY2 ){
-        /* Step on by the zero run length */
-        return  2;
-      }else{
-        /* else we have 2->3 zeros followed by a value */
-        /* Bit 0 determines the zero run length */
-        return 3 + (ExtraBits & 0x01);
-      }
-    }
-  } 
-
-  if ( Token == DCT_SHORT_ZRL_TOKEN ||  Token == DCT_ZRL_TOKEN ) 
-    /* Token is a ZRL token so step on by the appropriate number of zeros */
-    return ExtraBits + 1;
-
-  return 1;
-}
-
-static void PackToken ( CP_INSTANCE *cpi, 
-			fragment_t *fp,
-			ogg_uint32_t HuffIndex ) {
-  ogg_uint32_t Token = fp->token_list[fp->tokens_packed];
-  ogg_uint32_t ExtraBitsToken = fp->token_list[fp->tokens_packed+1];
-  ogg_uint32_t OneOrTwo;
-  ogg_uint32_t OneOrZero;
-
-  /* Update the record of what coefficient we have got up to for this
-     block and unpack the encoded token back into the quantised data
-     array. */
-  fp->coeffs_packed += TokenCoeffs ( Token, ExtraBitsToken );
-
-  /* Update record of tokens coded and where we are in this fragment. */
-  /* Is there an extra bits token */
-  OneOrTwo = 1 + ( cpi->ExtraBitLengths_VP3x[Token] > 0 );
-
-  /* Advance to the next real token. */
-  fp->tokens_packed += (unsigned char)OneOrTwo;
-
-  OneOrZero = ( fp < cpi->frag[1] );
-
-  if ( Token == DCT_EOB_TOKEN ) {
-    if ( cpi->RunLength == 0 ) {
-      cpi->RunHuffIndex = HuffIndex;
-      cpi->RunPlaneIndex = 1 -  OneOrZero;
-    }
-    cpi->RunLength++;
-
-    /* we have exceeded our longest run length  xmit an eob run token; */
-    if ( cpi->RunLength == 4095 ) PackEOBRun(cpi);
-
-  }else{
-
-    /* If we have an EOB run then code it up first */
-    if ( cpi->RunLength > 0 ) PackEOBRun( cpi);
-
-    /* Mark out which plane the block belonged to */
-    cpi->OptimisedTokenListPl[cpi->OptimisedTokenCount] =
-      (unsigned char)(1 - OneOrZero);
-
-    /* Note the token, extra bits and hufman table in the optimised
-       token list */
-    cpi->OptimisedTokenList[cpi->OptimisedTokenCount] =
-      (unsigned char)Token;
-    cpi->OptimisedTokenListEb[cpi->OptimisedTokenCount] =
-      ExtraBitsToken;
-    cpi->OptimisedTokenListHi[cpi->OptimisedTokenCount] =
-      (unsigned char)HuffIndex;
-
-    cpi->OptimisedTokenCount++;
-  }
-}
-
-static void PackCodedVideo (CP_INSTANCE *cpi) {
-  ogg_int32_t i;
   ogg_int32_t EncodedCoeffs = 1;
-  ogg_uint32_t HuffIndex; /* Index to group of tables used to code a token */
+  fragment_t *fp;
 
-  /* Reset the count of second order optimised tokens */
-  cpi->OptimisedTokenCount = 0;
-
-  /* Blank the various fragment data structures before we start. */
-  for ( i = 0; i < cpi->CodedBlockIndex; i++ ) {
-    fragment_t *fp = cpi->CodedBlockList[i];
-    fp->coeffs_packed = 0;
-    fp->tokens_packed = 0;
-  }
-
   /* The tree is not needed (implicit) for key frames */
-  if ( cpi->FrameType != KEY_FRAME ){
-    /* Pack the quad tree fragment mapping. */
+  if ( cpi->FrameType != KEY_FRAME )
     PackAndWriteDFArray( cpi );
-  }
 
   /* Mode and MV data not needed for key frames. */
   if ( cpi->FrameType != KEY_FRAME ){
-    /* Pack and code the mode list. */
     PackModes(cpi);
-    /* Pack the motion vectors */
     PackMotionVectors (cpi);
   }
 
-  /* Optimise the DC tokens */
-  for ( i = 0; i < cpi->CodedBlockIndex; i++ ) {
-    /* Get the linear index for the current fragment. */
-    fragment_t *fp = cpi->CodedBlockList[i];
-    fp->nonzero = EncodedCoeffs;
-    PackToken(cpi, fp, DC_HUFF_OFFSET );
-  }
-  
-  /* Pack any outstanding EOB tokens */
-  PackEOBRun(cpi);
-
-  /* Now output the optimised DC token list using the appropriate
-     entropy tables. */
   EncodeDcTokenList(cpi);
-
-  /* Work out the number of DC bits coded */
-
-  /* Optimise the AC tokens */
-  while ( EncodedCoeffs < 64 ) {
-    /* Huffman table adjustment based upon coefficient number. */
-    if ( EncodedCoeffs <= AC_TABLE_2_THRESH )
-      HuffIndex = AC_HUFF_OFFSET;
-    else if ( EncodedCoeffs <= AC_TABLE_3_THRESH )
-      HuffIndex = AC_HUFF_OFFSET + AC_HUFF_CHOICES;
-    else if ( EncodedCoeffs <= AC_TABLE_4_THRESH )
-      HuffIndex = AC_HUFF_OFFSET + (AC_HUFF_CHOICES * 2);
-    else
-      HuffIndex = AC_HUFF_OFFSET + (AC_HUFF_CHOICES * 3);
-
-    /* Repeatedly scan through the list of blocks. */
-    for ( i = 0; i < cpi->CodedBlockIndex; i++ ) {
-      /* Get the linear index for the current fragment. */
-      fragment_t *fp = cpi->CodedBlockList[i];
-
-      /* Should we code a token for this block on this pass. */
-      if ( fp->tokens_packed < fp->tokens_coded &&
-           fp->coeffs_packed <= EncodedCoeffs ) {
-        /* Bit pack and a token for this block */
-        fp->nonzero = EncodedCoeffs;
-        PackToken( cpi, fp, HuffIndex );
-      }
-    }
-
-    EncodedCoeffs ++;
-  }
-
-  /* Pack any outstanding EOB tokens */
-  PackEOBRun(cpi);
-
-  /* Now output the optimised AC token list using the appropriate
-     entropy tables. */
   EncodeAcTokenList(cpi);
 
-}
-
-void EncodeData(CP_INSTANCE *cpi){
-  ogg_int32_t   i;
-
-  memset(cpi->ModeCount, 0, MAX_MODES*sizeof(*cpi->ModeCount));
-  
-  /* Initialise the coded block indices variables. These allow
-     subsequent linear access to the quad tree ordered list of coded
-     blocks */
-  cpi->CodedBlockIndex = 0;
-
-  dsp_save_fpu (cpi->dsp);
-
-  /* Encode and tokenise the Y, U and V components */
-  CodePlane(cpi, 0);
-  CodePlane(cpi, 1);
-  CodePlane(cpi, 2);
-  
-  PredictDC(cpi);
-
-  /* Pack DCT tokens */
-  for ( i = 0; i < cpi->CodedBlockIndex; i++ ) 
-    DPCMTokenizeBlock ( cpi, cpi->CodedBlockList[i] );
-
-  /* Bit pack the video data data */
-  PackCodedVideo(cpi);
-
-  /* Reconstruct the reference frames */
   ReconRefFrames(cpi);
 
   dsp_restore_fpu (cpi->dsp);

Modified: branches/theora-thusnelda/lib/enc/encoder_huffman.h
===================================================================
--- branches/theora-thusnelda/lib/enc/encoder_huffman.h	2007-12-04 10:28:46 UTC (rev 14269)
+++ branches/theora-thusnelda/lib/enc/encoder_huffman.h	2007-12-05 00:52:39 UTC (rev 14270)
@@ -69,6 +69,7 @@
 #define DCT_RUN_CATEGORY1B      (DCT_RUN_CATEGORY1 + 5)
 #define DCT_RUN_CATEGORY1C      (DCT_RUN_CATEGORY1B + 1)
 #define DCT_RUN_CATEGORY2       (DCT_RUN_CATEGORY1C + 1)
+#define DCT_RUN_CATEGORY2B      (DCT_RUN_CATEGORY2 + 1)
 
 /* 32 */
 #define MAX_ENTROPY_TOKENS      (DCT_RUN_CATEGORY2 + 2)

Modified: branches/theora-thusnelda/lib/enc/frinit.c
===================================================================
--- branches/theora-thusnelda/lib/enc/frinit.c	2007-12-04 10:28:46 UTC (rev 14269)
+++ branches/theora-thusnelda/lib/enc/frinit.c	2007-12-05 00:52:39 UTC (rev 14270)
@@ -19,21 +19,6 @@
 #include "codec_internal.h"
 
 
-static void CalcPixelIndexTable(CP_INSTANCE *cpi){
-  ogg_uint32_t plane,row,col;
-  fragment_t *fp = cpi->frag[0];
-
-  for(plane=0;plane<3;plane++){
-    ogg_uint32_t offset = cpi->offset[plane];
-    for(row=0;row<cpi->frag_v[plane];row++){
-      for(col=0;col<cpi->frag_h[plane];col++,fp++){
-	fp->buffer_index = offset+col*8;
-      }
-      offset += cpi->stride[plane]*8;
-    }
-  }
-}
-
 void ClearFrameInfo(CP_INSTANCE *cpi){
 
   if(cpi->frame) _ogg_free(cpi->frame);
@@ -48,9 +33,6 @@
   if(cpi->recon) _ogg_free(cpi->recon);
   cpi->recon = 0;
 
-  if(cpi->CodedBlockList) _ogg_free(cpi->CodedBlockList);
-  cpi->CodedBlockList = 0;
-
   if(cpi->OptimisedTokenListEb ) _ogg_free(cpi->OptimisedTokenListEb);
   cpi->OptimisedTokenListEb = 0;
 
@@ -100,8 +82,6 @@
    a waste of code. */
 
 void InitFrameInfo(CP_INSTANCE *cpi){
-  int FrameSize;
-
   cpi->stride[0] = (cpi->info.width + STRIDE_EXTRA);
   cpi->stride[1] = (cpi->info.width + STRIDE_EXTRA) / 2;
   cpi->stride[2] = (cpi->info.width + STRIDE_EXTRA) / 2;
@@ -109,8 +89,8 @@
   {
     ogg_uint32_t ry_size = cpi->stride[0] * (cpi->info.height + STRIDE_EXTRA);
     ogg_uint32_t ruv_size = ry_size / 4;
-    FrameSize = ry_size + 2 * ruv_size;
 
+    cpi->frame_size = ry_size + 2 * ruv_size;
     cpi->offset[0] = (cpi->stride[0] * UMV_BORDER) + UMV_BORDER;
     cpi->offset[1] = ry_size + cpi->stride[1] * (UMV_BORDER/2) + (UMV_BORDER/2);
     cpi->offset[2] = ry_size + ruv_size + cpi->stride[2] * (UMV_BORDER/2) + (UMV_BORDER/2);
@@ -226,30 +206,38 @@
   }
 
   /* allocate frames */
-  cpi->frame = _ogg_malloc(FrameSize*sizeof(*cpi->frame));
-  cpi->lastrecon = _ogg_malloc(FrameSize*sizeof(*cpi->lastrecon));
-  cpi->golden = _ogg_malloc(FrameSize*sizeof(*cpi->golden));
-  cpi->recon = _ogg_malloc(FrameSize*sizeof(*cpi->recon));
+  cpi->frame = _ogg_malloc(cpi->frame_size*sizeof(*cpi->frame));
+  cpi->lastrecon = _ogg_malloc(cpi->frame_size*sizeof(*cpi->lastrecon));
+  cpi->golden = _ogg_malloc(cpi->frame_size*sizeof(*cpi->golden));
+  cpi->recon = _ogg_malloc(cpi->frame_size*sizeof(*cpi->recon));
 
   cpi->OptimisedTokenListEb =
-    _ogg_malloc(FrameSize*
+    _ogg_malloc(cpi->frame_size*
                 sizeof(*cpi->OptimisedTokenListEb));
   cpi->OptimisedTokenList =
-    _ogg_malloc(FrameSize*
+    _ogg_malloc(cpi->frame_size*
                 sizeof(*cpi->OptimisedTokenList));
   cpi->OptimisedTokenListHi =
-    _ogg_malloc(FrameSize*
+    _ogg_malloc(cpi->frame_size*
                 sizeof(*cpi->OptimisedTokenListHi));
   cpi->OptimisedTokenListPl =
-    _ogg_malloc(FrameSize*
+    _ogg_malloc(cpi->frame_size*
                 sizeof(*cpi->OptimisedTokenListPl));
 
-  /* misc */
-  cpi->CodedBlockList =
-    _ogg_malloc(cpi->frag_total * sizeof(*cpi->CodedBlockList));
-
   /* Re-initialise the pixel index table. */
-  CalcPixelIndexTable( cpi );
-
+  {
+    ogg_uint32_t plane,row,col;
+    fragment_t *fp = cpi->frag[0];
+    
+    for(plane=0;plane<3;plane++){
+      ogg_uint32_t offset = cpi->offset[plane];
+      for(row=0;row<cpi->frag_v[plane];row++){
+	for(col=0;col<cpi->frag_h[plane];col++,fp++){
+	  fp->buffer_index = offset+col*8;
+	}
+	offset += cpi->stride[plane]*8;
+      }
+    }
+  }
 }
 



More information about the commits mailing list