[cvs] / xvidcore / src / bitstream / mbcoding.c Repository:
ViewVC logotype

Diff of /xvidcore/src/bitstream/mbcoding.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.4, Fri Mar 15 09:20:03 2002 UTC revision 1.26, Sun Sep 8 14:43:04 2002 UTC
# Line 1  Line 1 
1    /*****************************************************************************
2     *
3     *  XVID MPEG-4 VIDEO CODEC
4     *  - Vector Length Coding tables -
5     *
6     *  Copyright(C) 2002 Michael Militzer <isibaar@xvid.org>
7     *
8     *
9     *  This program is an implementation of a part of one or more MPEG-4
10     *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending
11     *  to use this software module in hardware or software products are
12     *  advised that its use may infringe existing patents or copyrights, and
13     *  any such use would be at such party's own risk.  The original
14     *  developer of this software module and his/her company, and subsequent
15     *  editors and their companies, will have no liability for use of this
16     *  software or modifications or derivatives thereof.
17     *
18     *  This program is free software; you can redistribute it and/or modify
19     *  it under the terms of the GNU General Public License as published by
20     *  the Free Software Foundation; either version 2 of the License, or
21     *  (at your option) any later version.
22     *
23     *  This program is distributed in the hope that it will be useful,
24     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
25     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26     *  GNU General Public License for more details.
27     *
28     *  You should have received a copy of the GNU General Public License
29     *  along with this program; if not, write to the Free Software
30     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
31     *
32     * $Id$
33     *
34     ****************************************************************************/
35    
36    #include <stdlib.h>
37  #include "../portab.h"  #include "../portab.h"
38  #include "bitstream.h"  #include "bitstream.h"
39  #include "zigzag.h"  #include "zigzag.h"
40  #include "vlc_codes.h"  #include "vlc_codes.h"
41    #include "mbcoding.h"
42    
43  #include "../utils/mbfunctions.h"  #include "../utils/mbfunctions.h"
44    
 #include <stdlib.h> /* malloc, free */  
   
 #define ESCAPE 7167  
45  #define ABS(X) (((X)>0)?(X):-(X))  #define ABS(X) (((X)>0)?(X):-(X))
46  #define CLIP(X,A) (X > A) ? (A) : (X)  #define CLIP(X,A) (X > A) ? (A) : (X)
47    
48  static VLC *DCT3D[2];  /*****************************************************************************
49     * Local data
50     ****************************************************************************/
51    
52    static VLC intra_table[524032];
53    static VLC inter_table[524032];
54    
55    static VLC DCT3Dintra[4096];
56    static VLC DCT3Dinter[4096];
57    
58    /*****************************************************************************
59     * Functions
60     ****************************************************************************/
61    
62  VLC *intra_table, *inter_table;  void
63  static short clip_table[4096];  init_vlc_tables(void)
   
 void create_vlc_tables(void)  
64  {  {
65    
66          int32_t k, l, i, intra, last;          int32_t k, l, i, intra, last;
67          VLC *vlc[2];          VLC *vlc[2];
68          VLC **coeff_ptr;          VLC **coeff_ptr;
69          VLC *vlc1, *vlc2;          VLC *vlc1, *vlc2;
70    
         VLC *DCT3Dintra;  
         VLC *DCT3Dinter;  
   
         DCT3Dintra = (VLC *) malloc(sizeof(VLC) * 4096);  
         DCT3Dinter = (VLC *) malloc(sizeof(VLC) * 4096);  
   
71          vlc1 = DCT3Dintra;          vlc1 = DCT3Dintra;
72          vlc2 = DCT3Dinter;          vlc2 = DCT3Dinter;
73    
74          vlc[0] = intra_table = (VLC *) malloc(128 * 511 * sizeof(VLC));          vlc[0] = intra_table;
75          vlc[1] = inter_table = (VLC *) malloc(128 * 511 * sizeof(VLC));          vlc[1] = inter_table;
   
         // initialize the clipping table  
         for(i = -2048; i < 2048; i++) {  
                 clip_table[i + 2048] = i;  
                 if(i < -255)  
                         clip_table[i + 2048] = -255;  
                 if(i > 255)  
                         clip_table[i + 2048] = 255;  
         }  
76    
77          // generate intra/inter vlc lookup table          /*
78             * Generate encoding vlc lookup tables
79             * the lookup table idea is taken from the excellent fame project
80             * by Vivien Chapellier
81             */
82          for(i = 0; i < 4; i++) {          for(i = 0; i < 4; i++) {
83                  intra = i % 2;                  intra = i % 2;
84                  last = i >> 1;                  last = i / 2;
85    
86                  coeff_ptr = coeff_vlc[last + (intra << 1)];                  coeff_ptr = coeff_vlc[last + 2 * intra];
87    
88                  for(k = -255; k < 256; k++) { // level                  for (k = -2047; k < 2048; k++) {        // level
89                          char *max_level_ptr = max_level[last + (intra << 1)];                          int8_t *max_level_ptr = max_level[last + 2 * intra];
90                          char *max_run_ptr = max_run[last + (intra << 1)];                          int8_t *max_run_ptr = max_run[last + 2 * intra];
91    
92                          for(l = 0; l < 64; l++) { // run                          for(l = 0; l < 64; l++) { // run
93                                  int32_t level = k, run = l;                                  int32_t level = k;
94                                    ptr_t run = l;
95    
96                                  if(abs(level) <= max_level_ptr[run] && run <= max_run_ptr[abs(level)]) {                                  if ((abs(level) <= max_level_ptr[run]) && (run <= (uint32_t) max_run_ptr[abs(level)])) {        // level < max_level and run < max_run
97    
                                         if(level > 0) {  
                                                 vlc[intra]->code = (coeff_ptr[run][level - 1].code) << 1;  
                                                 vlc[intra]->len = coeff_ptr[run][level - 1].len + 1;  
                                         }  
                                         else if(level < 0) {  
                                                 vlc[intra]->code = ((coeff_ptr[run][-level - 1].code) << 1) + 1;  
                                                 vlc[intra]->len = coeff_ptr[run][-level - 1].len + 1;  
                                         }  
                                         else {  
98                                                  vlc[intra]->code = 0;                                                  vlc[intra]->code = 0;
99                                                  vlc[intra]->len = 0;                                                  vlc[intra]->len = 0;
100                                          }                                          goto loop_end;
101                                  } else {                                  } else {
102                                          if(level > 0)                                          if (level > 0)  // correct level
103                                                  level -= max_level_ptr[run];                                                  level -= max_level_ptr[run];
104                                          else                                          else
105                                                  level += max_level_ptr[run];                                                  level += max_level_ptr[run];
106    
107                                          if(abs(level) <= max_level_ptr[run] &&                                          if ((abs(level) <= max_level_ptr[run]) &&
108                                                  run <= max_run_ptr[abs(level)]) {                                                  (run <= (uint32_t) max_run_ptr[abs(level)])) {
109    
                                                 if(level > 0) {  
                                                         vlc[intra]->code = (0x06 << (coeff_ptr[run][level - 1].len + 1)) |  
                                                                 (coeff_ptr[run][level - 1].code << 1);  
                                                         vlc[intra]->len = (coeff_ptr[run][level - 1].len + 1) + 8;  
                                                 }  
                                                 else if(level < 0) {  
                                                         vlc[intra]->code = (0x06 << (coeff_ptr[run][-level - 1].len + 1)) |  
                                                                 ((coeff_ptr[run][-level - 1].code << 1) + 1);  
                                                         vlc[intra]->len = (coeff_ptr[run][-level - 1].len + 1) + 8;  
                                                 }  
                                                 else {  
110                                                          vlc[intra]->code = 0x06;                                                          vlc[intra]->code = 0x06;
111                                                          vlc[intra]->len = 8;                                                          vlc[intra]->len = 8;
112                                                    goto loop_end;
113                                                  }                                                  }
114                                          } else {  
115                                                  if(level > 0)                                          if (level > 0)  // still here?
116                                                          level += max_level_ptr[run];                                                  level += max_level_ptr[run];    // restore level
117                                                  else                                                  else
118                                                          level -= max_level_ptr[run];                                                          level -= max_level_ptr[run];
119                                                  DEBUG1("1) run:", run);  
120                                                  run -= max_run_ptr[abs(level)] + 1;                                          run -= max_run_ptr[abs(level)] + 1;     // and change run
121                                                  DEBUG1("2) run:", run);  
122                                            if ((abs(level) <= max_level_ptr[run]) &&
123                                                  if(abs(level) <= max_level_ptr[run] &&                                                  (run <= (uint32_t) max_run_ptr[abs(level)])) {
124                                                          run <= max_run_ptr[abs(level)]) {  
   
                                                         if(level > 0) {  
                                                                 vlc[intra]->code = (0x0e << (coeff_ptr[run][level - 1].len + 1)) |  
                                                                         (coeff_ptr[run][level - 1].code << 1);  
                                                                 vlc[intra]->len = (coeff_ptr[run][level - 1].len + 1) + 9;  
                                                         }  
                                                         else if(level < 0) {  
                                                                 vlc[intra]->code = (0x0e << (coeff_ptr[run][-level - 1].len + 1)) |  
                                                                         ((coeff_ptr[run][-level - 1].code << 1) + 1);  
                                                                 vlc[intra]->len = (coeff_ptr[run][-level - 1].len + 1) + 9;  
                                                         }  
                                                         else {  
125                                                                  vlc[intra]->code = 0x0e;                                                                  vlc[intra]->code = 0x0e;
126                                                                  vlc[intra]->len = 9;                                                                  vlc[intra]->len = 9;
127                                                    goto loop_end;
128                                                          }                                                          }
                                                 } else {  
                                                         if(level != 0)  
129                                                                  run += max_run_ptr[abs(level)] + 1;                                                                  run += max_run_ptr[abs(level)] + 1;
130                                                          else                                  }
                                                                 run++;  
   
                                                         DEBUG1("3) run:", run);  
131    
132                                                          vlc[intra]->code = (uint32_t) ((0x1e + last) << 20) |                                  vlc[intra]->code =
133                                                                                  (l << 14) | (1 << 13) | ((k & 0xfff) << 1) | 1;                                          (uint32_t) ((l << 14) | (0x1e + last) << 20) | (1 << 13) |
134                                            ((k & 0xfff) << 1) | 1;
135    
136                                                          vlc[intra]->len = 30;                                                          vlc[intra]->len = 30;
137                                    vlc[intra]++;
138                                    continue;
139    
140                              loop_end:
141                                    if (level != 0) {
142                                            vlc[intra]->code =
143                                                    (vlc[intra]->
144                                                     code << (coeff_ptr[run][abs(level) - 1].len +
145                                                                      1)) | (coeff_ptr[run][abs(level) -
146                                                                                                                    1].code << 1);
147                                            vlc[intra]->len =
148                                                    (coeff_ptr[run][abs(level) - 1].len + 1) +
149                                                    vlc[intra]->len;
150    
151                                            if (level < 0)
152                                                    vlc[intra]->code += 1;
153                                                  }                                                  }
154                                          }  
                                 }  
155                                  vlc[intra]++;                                  vlc[intra]++;
156                          }                          }
157                  }                  }
158          }          }
         intra_table += 64*255; // center vlc tables  
         inter_table += 64*255; // center vlc tables  
159    
160          for(i = 0; i < 4096; i++) {          for(i = 0; i < 4096; i++) {
161                  if(i >= 512) {                  if(i >= 512) {
162                          *vlc1 = DCT3Dtab3[(i >> 5) - 16];                          *vlc1 = DCT3Dtab3[(i >> 5) - 16];
163                          *vlc2 = DCT3Dtab0[(i >> 5) - 16];                          *vlc2 = DCT3Dtab0[(i >> 5) - 16];
164                  }                  } else if (i >= 128) {
                 else if(i >= 128) {  
165                          *vlc1 = DCT3Dtab4[(i >> 2) - 32];                          *vlc1 = DCT3Dtab4[(i >> 2) - 32];
166                          *vlc2 = DCT3Dtab1[(i >> 2) - 32];                          *vlc2 = DCT3Dtab1[(i >> 2) - 32];
167                  }                  } else if (i >= 8) {
                 else if(i >= 8) {  
168                          *vlc1 = DCT3Dtab5[i - 8];                          *vlc1 = DCT3Dtab5[i - 8];
169                          *vlc2 = DCT3Dtab2[i - 8];                          *vlc2 = DCT3Dtab2[i - 8];
170                  }                  } else {
                 else {  
171                          *vlc1 = ERRtab[i];                          *vlc1 = ERRtab[i];
172                          *vlc2 = ERRtab[i];                          *vlc2 = ERRtab[i];
173                  }                  }
# Line 169  Line 180 
180    
181  }  }
182    
183  void destroy_vlc_tables(void) {  static __inline void
184    CodeVector(Bitstream * bs,
185          if(intra_table != NULL && inter_table != NULL) {                     int32_t value,
186                  intra_table -= 64*255; // uncenter vlc tables                     int32_t f_code,
187                  inter_table -= 64*255; // uncenter vlc tables                     Statistics * pStat)
   
                 free(intra_table);  
                 free(inter_table);  
         }  
   
         if(DCT3D[0] != NULL && DCT3D[1] != NULL) {  
                 free(DCT3D[0]);  
                 free(DCT3D[1]);  
         }  
   
 }  
   
 static __inline void CodeVector(Bitstream *bs, int16_t value, int16_t f_code, Statistics *pStat)  
188  {  {
189    
190          const int scale_factor = 1 << (f_code - 1);          const int scale_factor = 1 << (f_code - 1);
191          const int cmp = scale_factor << 5;          const int cmp = scale_factor << 5;
192    
# Line 200  Line 199 
199      pStat->iMvSum += value * value;      pStat->iMvSum += value * value;
200      pStat->iMvCount++;      pStat->iMvCount++;
201    
202          if (value == 0)          if (value == 0) {
203                  BitstreamPutBits(bs, mb_motion_table[32].code, mb_motion_table[32].len);                  BitstreamPutBits(bs, mb_motion_table[32].code,
204      else {                                                   mb_motion_table[32].len);
205            } else {
206                  uint16_t length, code, mv_res, sign;                  uint16_t length, code, mv_res, sign;
207    
208                  length = 16 << f_code;                  length = 16 << f_code;
# Line 226  Line 226 
226                          code = -code;                          code = -code;
227    
228                  code += 32;                  code += 32;
229                  BitstreamPutBits(bs, mb_motion_table[code].code, mb_motion_table[code].len);                  BitstreamPutBits(bs, mb_motion_table[code].code,
230                                                     mb_motion_table[code].len);
231    
232                  if(f_code)                  if(f_code)
233                          BitstreamPutBits(bs, mv_res, f_code);                          BitstreamPutBits(bs, mv_res, f_code);
234    }    }
235    
236  }  }
237    
238    
239  static __inline void CodeCoeff(Bitstream *bs, int16_t qcoeff[64], VLC *table,  static __inline void
240                                                             const uint16_t *zigzag, uint16_t intra) {  CodeCoeff(Bitstream * bs,
241                      const int16_t qcoeff[64],
242                      VLC * table,
243                      const uint16_t * zigzag,
244                      uint16_t intra)
245    {
246    
247          uint32_t j, last;          uint32_t j, last;
248          short v;          short v;
249          VLC *vlc;          VLC *vlc;
250    
251          j = intra;          j = intra;
252          last = 1 + intra;          last = intra;
253    
254          while((v = qcoeff[zigzag[j++]]) == 0);          while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)
255                    j++;
256    
257          do {          do {
258                    vlc = table + 64 * 2047 + (v << 6) + j - last;
259                    last = ++j;
260    
261                  // count zeroes                  // count zeroes
262                  vlc = table + (clip_table[2048+v] << 6) + j - last;                  while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)
263                  last = j + 1;                          j++;
                 while(j < 64 && (v = qcoeff[zigzag[j++]]) == 0);  
264    
265                  // write code                  // write code
266                  if(j != 64) {                  if(j != 64) {
267                          BitstreamPutBits(bs, vlc->code, vlc->len);                          BitstreamPutBits(bs, vlc->code, vlc->len);
268                  } else {                  } else {
269                          vlc += 64*511;                          vlc += 64 * 4095;
270                          BitstreamPutBits(bs, vlc->code, vlc->len);                          BitstreamPutBits(bs, vlc->code, vlc->len);
271                          break;                          break;
272                  }                  }
273          } while(1);          } while(1);
274    
275  }  }
276    
277    
278  static void CodeBlockIntra(const MBParam * pParam, const MACROBLOCK *pMB,  static void
279                                                                    int16_t qcoeff[][64], Bitstream * bs, Statistics * pStat)  CodeBlockIntra(const FRAMEINFO * frame,
280                               const MACROBLOCK * pMB,
281                               int16_t qcoeff[6 * 64],
282                               Bitstream * bs,
283                               Statistics * pStat)
284  {  {
285    
286          uint32_t i, mcbpc, cbpy, bits;          uint32_t i, mcbpc, cbpy, bits;
287    
288          cbpy = pMB->cbp >> 2;          cbpy = pMB->cbp >> 2;
289    
290      // write mcbpc      // write mcbpc
291          if(pParam->coding_type == I_VOP) {          if (frame->coding_type == I_VOP) {
292              mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2);              mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2);
293                  BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, mcbpc_intra_tab[mcbpc].len);                  BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code,
294          }                                                   mcbpc_intra_tab[mcbpc].len);
295          else {          } else {
296              mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);              mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);
297                  BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len);                  BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,
298                                                     mcbpc_inter_tab[mcbpc].len);
299          }          }
300    
301          // ac prediction flag          // ac prediction flag
# Line 293  Line 311 
311      if(pMB->mode == MODE_INTRA_Q)      if(pMB->mode == MODE_INTRA_Q)
312                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, pMB->dquant, 2);
313    
314            // write interlacing
315            if (frame->global_flags & XVID_INTERLACING) {
316                    BitstreamPutBit(bs, pMB->field_dct);
317            }
318          // code block coeffs          // code block coeffs
319          for(i = 0; i < 6; i++)          for (i = 0; i < 6; i++) {
         {  
320                  if(i < 4)                  if(i < 4)
321                          BitstreamPutBits(bs, dcy_tab[qcoeff[i][0] + 255].code,                          BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code,
322                                                           dcy_tab[qcoeff[i][0] + 255].len);                                                           dcy_tab[qcoeff[i * 64 + 0] + 255].len);
323                  else                  else
324                          BitstreamPutBits(bs, dcc_tab[qcoeff[i][0] + 255].code,                          BitstreamPutBits(bs, dcc_tab[qcoeff[i * 64 + 0] + 255].code,
325                                           dcc_tab[qcoeff[i][0] + 255].len);                                                           dcc_tab[qcoeff[i * 64 + 0] + 255].len);
326    
327                  if(pMB->cbp & (1 << (5 - i)))                  if (pMB->cbp & (1 << (5 - i))) {
                 {  
328                          bits = BitstreamPos(bs);                          bits = BitstreamPos(bs);
329    
330                          CodeCoeff(bs, qcoeff[i], intra_table, scan_tables[pMB->acpred_directions[i]], 1);                          CodeCoeff(bs, &qcoeff[i * 64], intra_table,
331                                              scan_tables[pMB->acpred_directions[i]], 1);
332    
333                          bits = BitstreamPos(bs) - bits;                          bits = BitstreamPos(bs) - bits;
334                          pStat->iTextBits += bits;                          pStat->iTextBits += bits;
335                  }                  }
336          }          }
337    
338  }  }
339    
340    
341  static void CodeBlockInter(const MBParam * pParam, const MACROBLOCK *pMB,  static void
342                                                                    int16_t qcoeff[][64], Bitstream * bs, Statistics * pStat)  CodeBlockInter(const FRAMEINFO * frame,
343                               const MACROBLOCK * pMB,
344                               int16_t qcoeff[6 * 64],
345                               Bitstream * bs,
346                               Statistics * pStat)
347  {  {
348    
349          int32_t i;          int32_t i;
350          uint32_t bits, mcbpc, cbpy;          uint32_t bits, mcbpc, cbpy;
351    
# Line 326  Line 353 
353          cbpy = 15 - (pMB->cbp >> 2);          cbpy = 15 - (pMB->cbp >> 2);
354    
355          // write mcbpc          // write mcbpc
356      BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len);          BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,
357                                             mcbpc_inter_tab[mcbpc].len);
358    
359          // write cbpy          // write cbpy
360          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);
# Line 335  Line 363 
363      if(pMB->mode == MODE_INTER_Q)      if(pMB->mode == MODE_INTER_Q)
364                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, pMB->dquant, 2);
365    
366            // interlacing
367            if (frame->global_flags & XVID_INTERLACING) {
368                    if (pMB->cbp) {
369                            BitstreamPutBit(bs, pMB->field_dct);
370                            DEBUG1("codep: field_dct: ", pMB->field_dct);
371                    }
372    
373                    // if inter block, write field ME flag
374                    if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
375                            BitstreamPutBit(bs, pMB->field_pred);
376                            DEBUG1("codep: field_pred: ", pMB->field_pred);
377    
378                            // write field prediction references
379                            if (pMB->field_pred) {
380                                    BitstreamPutBit(bs, pMB->field_for_top);
381                                    BitstreamPutBit(bs, pMB->field_for_bot);
382                            }
383                    }
384            }
385          // code motion vector(s)          // code motion vector(s)
386          for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++)          for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) {
387          {                  CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat);
388                  CodeVector(bs, pMB->pmvs[i].x, pParam->fixed_code, pStat);                  CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat);
                 CodeVector(bs, pMB->pmvs[i].y, pParam->fixed_code, pStat);  
389          }          }
390    
391          bits = BitstreamPos(bs);          bits = BitstreamPos(bs);
# Line 347  Line 393 
393          // code block coeffs          // code block coeffs
394          for(i = 0; i < 6; i++)          for(i = 0; i < 6; i++)
395                  if(pMB->cbp & (1 << (5 - i)))                  if(pMB->cbp & (1 << (5 - i)))
396                          CodeCoeff(bs, qcoeff[i], inter_table, scan_tables[0], 0);                          CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0);
397    
398          bits = BitstreamPos(bs) - bits;          bits = BitstreamPos(bs) - bits;
399          pStat->iTextBits += bits;          pStat->iTextBits += bits;
400    
401  }  }
402    
403    
404  void MBCoding(const MBParam * pParam, MACROBLOCK *pMB,  void
405                int16_t qcoeff[][64],  MBCoding(const FRAMEINFO * frame,
406                    Bitstream * bs, Statistics * pStat)                   MACROBLOCK * pMB,
407                     int16_t qcoeff[6 * 64],
408                     Bitstream * bs,
409                     Statistics * pStat)
410  {  {
         int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q);  
411    
412      if(pParam->coding_type == P_VOP) {          if (frame->coding_type == P_VOP) {
413                  if(pMB->cbp == 0 && pMB->mode == MODE_INTER &&                          BitstreamPutBit(bs, 0); // coded
414                          pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0)          }
415    
416            if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)
417                    CodeBlockIntra(frame, pMB, qcoeff, bs, pStat);
418            else
419                    CodeBlockInter(frame, pMB, qcoeff, bs, pStat);
420    
421    }
422    
423    
424    void
425    MBSkip(Bitstream * bs)
426                  {                  {
427                          BitstreamPutBit(bs, 1);         // not_coded          BitstreamPutBit(bs, 1); // not coded
428                          return;                          return;
429                  }                  }
430                  else  
431                          BitstreamPutBit(bs, 0);         // coded  
432    /***************************************************************
433     * bframe encoding start
434     ***************************************************************/
435    
436    /*
437            mbtype
438            0       1b              direct(h263)            mvdb
439            1       01b             interpolate mc+q        dbquant, mvdf, mvdb
440            2       001b    backward mc+q           dbquant, mvdb
441            3       0001b   forward mc+q            dbquant, mvdf
442    */
443    
444    void
445    put_bvop_mbtype(Bitstream * bs,
446                                    int value)
447    {
448            switch (value) {
449            case 0:
450                    BitstreamPutBit(bs, 1);
451                    return;
452    
453            case 1:
454                    BitstreamPutBit(bs, 0);
455                    BitstreamPutBit(bs, 1);
456                    return;
457    
458            case 2:
459                    BitstreamPutBit(bs, 0);
460                    BitstreamPutBit(bs, 0);
461                    BitstreamPutBit(bs, 1);
462                    return;
463    
464            case 3:
465                    BitstreamPutBit(bs, 0);
466                    BitstreamPutBit(bs, 0);
467                    BitstreamPutBit(bs, 0);
468                    BitstreamPutBit(bs, 1);
469                    return;
470    
471            default:;                                       // invalid!
472    
473          }          }
474    
         if(intra)  
                 CodeBlockIntra(pParam, pMB, qcoeff, bs, pStat);  
         else  
                 CodeBlockInter(pParam, pMB, qcoeff, bs, pStat);  
475  }  }
476    
477    /*
478            dbquant
479            -2      10b
480            0       0b
481            +2      11b
482    */
483    
484    void
485    put_bvop_dbquant(Bitstream * bs,
486                                     int value)
487    {
488            switch (value) {
489            case 0:
490                    BitstreamPutBit(bs, 0);
491                    return;
492    
493            case -2:
494                    BitstreamPutBit(bs, 1);
495                    BitstreamPutBit(bs, 0);
496                    return;
497    
498            case 2:
499                    BitstreamPutBit(bs, 1);
500                    BitstreamPutBit(bs, 1);
501                    return;
502    
503            default:;                                       // invalid
504            }
505    }
506    
507    
508    
509    void
510    MBCodingBVOP(const MACROBLOCK * mb,
511                             const int16_t qcoeff[6 * 64],
512                             const int32_t fcode,
513                             const int32_t bcode,
514                             Bitstream * bs,
515                             Statistics * pStat)
516    {
517            int i;
518    
519    /*      ------------------------------------------------------------------
520                    when a block is skipped it is decoded DIRECT(0,0)
521                    hence is interpolated from forward & backward frames
522            ------------------------------------------------------------------ */
523    
524            if (mb->mode == MODE_DIRECT_NONE_MV) {
525                    BitstreamPutBit(bs, 1); // skipped
526                    return;
527            }
528    
529            BitstreamPutBit(bs, 0);         // not skipped
530    
531            if (mb->cbp == 0) {
532                    BitstreamPutBit(bs, 1); // cbp == 0
533            } else {
534                    BitstreamPutBit(bs, 0); // cbp == xxx
535            }
536    
537            put_bvop_mbtype(bs, mb->mode);
538    
539            if (mb->cbp) {
540                    BitstreamPutBits(bs, mb->cbp, 6);
541            }
542    
543            if (mb->mode != MODE_DIRECT && mb->cbp != 0) {
544                    put_bvop_dbquant(bs, 0);        // todo: mb->dquant = 0
545            }
546    
547            if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {
548                    CodeVector(bs, mb->pmvs[0].x, fcode, pStat);
549                    CodeVector(bs, mb->pmvs[0].y, fcode, pStat);
550            }
551    
552            if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) {
553                    CodeVector(bs, mb->b_pmvs[0].x, bcode, pStat);
554                    CodeVector(bs, mb->b_pmvs[0].y, bcode, pStat);
555            }
556    
557            if (mb->mode == MODE_DIRECT) {
558                    CodeVector(bs, mb->deltamv.x, 1, pStat);                /* fcode is always 1 for delta vector */
559                    CodeVector(bs, mb->deltamv.y, 1, pStat);                /* prediction is always (0,0) */
560            }
561    
562            for (i = 0; i < 6; i++) {
563                    if (mb->cbp & (1 << (5 - i))) {
564                            CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0);
565                    }
566            }
567    }
568    
569    
570    
571  /***************************************************************  /***************************************************************
572   * decoding stuff starts here                                  *   * decoding stuff starts here                                  *
573   ***************************************************************/   ***************************************************************/
574    
575  int get_mcbpc_intra(Bitstream * bs)  
576    // for IVOP addbits == 0
577    // for PVOP addbits == fcode - 1
578    // for BVOP addbits == max(fcode,bcode) - 1
579    // returns true or false
580    int
581    check_resync_marker(Bitstream * bs, int addbits)
582    {
583            uint32_t nbits;
584            uint32_t code;
585            uint32_t nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER + addbits;
586    
587            nbits = BitstreamNumBitsToByteAlign(bs);
588            code = BitstreamShowBits(bs, nbits);
589    
590            if (code == (((uint32_t)1 << (nbits - 1)) - 1))
591  {  {
592          uint32_t index;                  return BitstreamShowBitsFromByteAlign(bs, nbitsresyncmarker) == RESYNC_MARKER;
593            }
594    
595          while((index = BitstreamShowBits(bs, 9)) == 1)          return 0;
596                  BitstreamSkip(bs, 9);  }
597    
598    
599    
600    int
601    get_mcbpc_intra(Bitstream * bs)
602    {
603    
604            uint32_t index;
605    
606            index = BitstreamShowBits(bs, 9);
607          index >>= 3;          index >>= 3;
608    
609          BitstreamSkip(bs, mcbpc_intra_table[index].len);          BitstreamSkip(bs, mcbpc_intra_table[index].len);
610    
611          return mcbpc_intra_table[index].code;          return mcbpc_intra_table[index].code;
612    
613  }  }
614    
615  int get_mcbpc_inter(Bitstream * bs)  int
616    get_mcbpc_inter(Bitstream * bs)
617  {  {
618    
619          uint32_t index;          uint32_t index;
620    
621          while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1)          index = CLIP(BitstreamShowBits(bs, 9), 256);
                 BitstreamSkip(bs, 9);  
622    
623      BitstreamSkip(bs,  mcbpc_inter_table[index].len);      BitstreamSkip(bs,  mcbpc_inter_table[index].len);
624    
625          return mcbpc_inter_table[index].code;          return mcbpc_inter_table[index].code;
626    
627  }  }
628    
629  int get_cbpy(Bitstream * bs, int intra)  int
630    get_cbpy(Bitstream * bs,
631                     int intra)
632  {  {
633    
634          int cbpy;          int cbpy;
635          uint32_t index = BitstreamShowBits(bs, 6);          uint32_t index = BitstreamShowBits(bs, 6);
636    
# Line 418  Line 641 
641                  cbpy = 15 - cbpy;                  cbpy = 15 - cbpy;
642    
643          return cbpy;          return cbpy;
644    
645  }  }
646    
647  int get_mv_data(Bitstream * bs)  int
648    get_mv_data(Bitstream * bs)
649  {  {
650    
651          uint32_t index;          uint32_t index;
652    
653          if(BitstreamGetBit(bs))          if(BitstreamGetBit(bs))
# Line 429  Line 655 
655    
656          index = BitstreamShowBits(bs, 12);          index = BitstreamShowBits(bs, 12);
657    
658          if(index >= 512)          if (index >= 512) {
         {  
659                  index = (index >> 8) - 2;                  index = (index >> 8) - 2;
660                  BitstreamSkip(bs, TMNMVtab0[index].len);                  BitstreamSkip(bs, TMNMVtab0[index].len);
661                  return TMNMVtab0[index].code;                  return TMNMVtab0[index].code;
662          }          }
663    
664          if(index >= 128)          if (index >= 128) {
         {  
665                  index = (index >> 2) - 32;                  index = (index >> 2) - 32;
666                  BitstreamSkip(bs, TMNMVtab1[index].len);                  BitstreamSkip(bs, TMNMVtab1[index].len);
667                  return TMNMVtab1[index].code;                  return TMNMVtab1[index].code;
# Line 447  Line 671 
671    
672          BitstreamSkip(bs, TMNMVtab2[index].len);          BitstreamSkip(bs, TMNMVtab2[index].len);
673          return TMNMVtab2[index].code;          return TMNMVtab2[index].code;
674    
675  }  }
676    
677  int get_mv(Bitstream * bs, int fcode)  int
678    get_mv(Bitstream * bs,
679               int fcode)
680  {  {
681    
682          int data;          int data;
683          int res;          int res;
684          int mv;          int mv;
# Line 465  Line 693 
693          mv = ((ABS(data) - 1) * scale_fac) + res + 1;          mv = ((ABS(data) - 1) * scale_fac) + res + 1;
694    
695          return data < 0 ? -mv : mv;          return data < 0 ? -mv : mv;
696    
697  }  }
698    
699  int get_dc_dif(Bitstream * bs, uint32_t dc_size)  int
700    get_dc_dif(Bitstream * bs,
701                       uint32_t dc_size)
702  {  {
703    
704          int code = BitstreamGetBits(bs, dc_size);          int code = BitstreamGetBits(bs, dc_size);
705          int msb = code >> (dc_size - 1);          int msb = code >> (dc_size - 1);
706    
# Line 476  Line 708 
708                  return (-1 * (code^((1 << dc_size) - 1)));                  return (-1 * (code^((1 << dc_size) - 1)));
709    
710          return code;          return code;
711    
712  }  }
713    
714  int get_dc_size_lum(Bitstream * bs)  int
715    get_dc_size_lum(Bitstream * bs)
716  {  {
717    
718          int code, i;          int code, i;
719    
720          code = BitstreamShowBits(bs, 11);          code = BitstreamShowBits(bs, 11);
721    
722          for(i = 11; i > 3; i--) {          for(i = 11; i > 3; i--) {
# Line 493  Line 729 
729    
730          BitstreamSkip(bs, dc_lum_tab[code].len);          BitstreamSkip(bs, dc_lum_tab[code].len);
731          return dc_lum_tab[code].code;          return dc_lum_tab[code].code;
732    
733  }  }
734    
735    
736  int get_dc_size_chrom(Bitstream * bs)  int
737    get_dc_size_chrom(Bitstream * bs)
738  {  {
739    
740          uint32_t code, i;          uint32_t code, i;
741    
742          code = BitstreamShowBits(bs, 12);          code = BitstreamShowBits(bs, 12);
743    
744          for(i = 12; i > 2; i--) {          for(i = 12; i > 2; i--) {
# Line 510  Line 750 
750          }          }
751    
752          return 3 - BitstreamGetBits(bs, 2);          return 3 - BitstreamGetBits(bs, 2);
 }  
   
 int get_coeff(Bitstream * bs, int *run, int *last, int intra, int short_video_header)  
 {  
     uint32_t mode;  
     const VLC *tab;  
         int32_t level;  
   
         if(short_video_header) // inter-VLCs will be used for both intra and inter blocks  
                 intra = 0;  
   
         tab = &DCT3D[intra][BitstreamShowBits(bs, 12)];  
   
         if(tab->code == -1)  
                 goto error;  
   
         BitstreamSkip(bs, tab->len);  
   
         if(tab->code != ESCAPE) {  
                 if(!intra)  
                 {  
                         *run = (tab->code >> 4) & 255;  
                         level = tab->code & 15;  
                         *last = (tab->code >> 12) & 1;  
                 }  
             else  
                 {  
                         *run = (tab->code >> 8) & 255;  
                         level = tab->code & 255;  
                         *last = (tab->code >> 16) & 1;  
                 }  
                 return BitstreamGetBit(bs) ? -level : level;  
         }  
753    
         if(short_video_header)  
         {  
                 // escape mode 4 - H.263 type, only used if short_video_header = 1  
                 *last = BitstreamGetBit(bs);  
                 *run = BitstreamGetBits(bs, 6);  
                 level = BitstreamGetBits(bs, 8);  
   
                 if (level == 0 || level == 128)  
                         DEBUG1("Illegal LEVEL for ESCAPE mode 4:", level);  
   
                 return (level >= 128 ? -(256 - level) : level);  
754          }          }
755    
756          mode = BitstreamShowBits(bs, 2);  void
757    get_intra_block(Bitstream * bs,
758          if(mode < 3) {                                  int16_t * block,
759                  BitstreamSkip(bs, (mode == 2) ? 2 : 1);                                  int direction,
760                                    int coeff)
                 tab = &DCT3D[intra][BitstreamShowBits(bs, 12)];  
                 if (tab->code == -1)  
                         goto error;  
   
                 BitstreamSkip(bs, tab->len);  
   
                 if (!intra) {  
                         *run = (tab->code >> 4) & 255;  
                         level = tab->code & 15;  
                         *last = (tab->code >> 12) & 1;  
                 }  
                 else  
761                  {                  {
                         *run = (tab->code >> 8) & 255;  
                         level = tab->code & 255;  
                         *last = (tab->code >> 16) & 1;  
                 }  
762    
                 if(mode < 2) // first escape mode, level is offset  
                         level += max_level[*last + (!intra<<1)][*run]; // need to add back the max level  
                 else if(mode == 2)  // second escape mode, run is offset  
                         *run += max_run[*last + (!intra<<1)][level] + 1;  
   
                 return BitstreamGetBit(bs) ? -level : level;  
         }  
   
         // third escape mode - fixed length codes  
         BitstreamSkip(bs, 2);  
         *last = BitstreamGetBits(bs, 1);  
         *run = BitstreamGetBits(bs, 6);  
         BitstreamSkip(bs, 1);                           // marker  
         level = BitstreamGetBits(bs, 12);  
         BitstreamSkip(bs, 1);                           // marker  
   
         return (level & 0x800) ? (level | (-1 ^ 0xfff)) : level;  
   
 error:  
         *run = VLC_ERROR;  
         return 0;  
 }  
   
   
 void get_intra_block(Bitstream * bs, int16_t * block, int direction, int coeff)  
 {  
763          const uint16_t * scan = scan_tables[ direction ];          const uint16_t * scan = scan_tables[ direction ];
764          int level;          int level;
765          int run;          int run;
766          int last;          int last;
767    
768          do          do {
         {  
769                  level = get_coeff(bs, &run, &last, 1, 0);                  level = get_coeff(bs, &run, &last, 1, 0);
770                  if (run == -1)                  if (run == -1) {
                 {  
771                          DEBUG("fatal: invalid run");                          DEBUG("fatal: invalid run");
772                          break;                          break;
773                  }                  }
774                  coeff += run;                  coeff += run;
775                  block[ scan[coeff] ] = level;                  block[ scan[coeff] ] = level;
776                  if (level < -127 || level > 127)  
777                  {                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level);
778                    //DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32));
779    
780                    if (level < -127 || level > 127) {
781                          DEBUG1("warning: intra_overflow", level);                          DEBUG1("warning: intra_overflow", level);
782                  }                  }
783                  coeff++;                  coeff++;
784          } while (!last);          } while (!last);
785    
786  }  }
787    
788  void get_inter_block(Bitstream * bs, int16_t * block)  void
789    get_inter_block(Bitstream * bs,
790                                    int16_t * block)
791  {  {
792    
793          const uint16_t * scan = scan_tables[0];          const uint16_t * scan = scan_tables[0];
794          int p;          int p;
795          int level;          int level;
# Line 638  Line 797 
797          int last;          int last;
798    
799          p = 0;          p = 0;
800          do          do {
         {  
801                  level = get_coeff(bs, &run, &last, 0, 0);                  level = get_coeff(bs, &run, &last, 0, 0);
802                  if (run == -1)                  if (run == -1) {
                 {  
803                          DEBUG("fatal: invalid run");                          DEBUG("fatal: invalid run");
804                          break;                          break;
805                  }                  }
806                  p += run;                  p += run;
807    
808                  block[ scan[p] ] = level;                  block[ scan[p] ] = level;
809                  if (level < -127 || level > 127)  
810                  {                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level);
811                    // DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32));
812    
813                    if (level < -127 || level > 127) {
814                          DEBUG1("warning: inter_overflow", level);                          DEBUG1("warning: inter_overflow", level);
815                  }                  }
816                  p++;                  p++;
817          } while (!last);          } while (!last);
818    
819  }  }

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.26

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4