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

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

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

revision 100, Thu Apr 4 13:58:18 2002 UTC revision 335, Wed Jul 24 19:34:14 2002 UTC
# Line 1  Line 1 
1     /******************************************************************************
2      *                                                                            *
3      *  This file is part of XviD, a free MPEG-4 video encoder/decoder            *
4      *                                                                            *
5      *  XviD is an implementation of a part of one or more MPEG-4 Video tools     *
6      *  as specified in ISO/IEC 14496-2 standard.  Those intending to use this    *
7      *  software module in hardware or software products are advised that its     *
8      *  use may infringe existing patents or copyrights, and any such use         *
9      *  would be at such party's own risk.  The original developer of this        *
10      *  software module and his/her company, and subsequent editors and their     *
11      *  companies, will have no liability for use of this software or             *
12      *  modifications or derivatives thereof.                                     *
13      *                                                                            *
14      *  XviD is free software; you can redistribute it and/or modify it           *
15      *  under the terms of the GNU General Public License as published by         *
16      *  the Free Software Foundation; either version 2 of the License, or         *
17      *  (at your option) any later version.                                       *
18      *                                                                            *
19      *  XviD is distributed in the hope that it will be useful, but               *
20      *  WITHOUT ANY WARRANTY; without even the implied warranty of                *
21      *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
22      *  GNU General Public License for more details.                              *
23      *                                                                            *
24      *  You should have received a copy of the GNU General Public License         *
25      *  along with this program; if not, write to the Free Software               *
26      *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA  *
27      *                                                                            *
28      ******************************************************************************/
29    
30     /******************************************************************************
31      *                                                                            *
32      *  mbcoding.c                                                                *
33      *                                                                            *
34      *  Copyright (C) 2002 - Michael Militzer <isibaar@xvid.org>                  *
35      *                                                                            *
36      *  For more information visit the XviD homepage: http://www.xvid.org         *
37      *                                                                            *
38      ******************************************************************************/
39    
40     /******************************************************************************
41      *                                                                                                                                                        *
42      *  Revision history:                                                         *
43      *                                                                            *
44      *  28.06.2002 added check_resync_marker()                                    *
45      *  14.04.2002 bframe encoding                                                                                            *
46      *  08.03.2002 initial version; isibaar                                                           *
47      *                                                                                                                                                        *
48      ******************************************************************************/
49    
50    
51    
52  #include <stdlib.h>  #include <stdlib.h>
53  #include "../portab.h"  #include "../portab.h"
54  #include "bitstream.h"  #include "bitstream.h"
# Line 7  Line 58 
58    
59  #include "../utils/mbfunctions.h"  #include "../utils/mbfunctions.h"
60    
 #define ESCAPE 7167  
61  #define ABS(X) (((X)>0)?(X):-(X))  #define ABS(X) (((X)>0)?(X):-(X))
62  #define CLIP(X,A) (X > A) ? (A) : (X)  #define CLIP(X,A) (X > A) ? (A) : (X)
63    
64    VLC intra_table[524032];
65    VLC inter_table[524032];
66    
67  VLC DCT3Dintra[4096];  VLC DCT3Dintra[4096];
68  VLC DCT3Dinter[4096];  VLC DCT3Dinter[4096];
 static VLC *DCT3D[2];  
   
 VLC intra_table[65536];  
 VLC inter_table[65536];  
69    
70  static short clip_table[4096];  void
71    init_vlc_tables(void)
 void init_vlc_tables(void)  
72  {  {
73    
74          int32_t k, l, i, intra, last;          int32_t k, l, i, intra, last;
# Line 34  Line 82 
82          vlc[0] = intra_table;          vlc[0] = intra_table;
83          vlc[1] = inter_table;          vlc[1] = inter_table;
84    
         // 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;  
         }  
   
85          // generate encoding vlc lookup tables          // generate encoding vlc lookup tables
86            // the lookup table idea is taken from the excellent fame project by Vivien Chapellier
87          for(i = 0; i < 4; i++) {          for(i = 0; i < 4; i++) {
88                  intra = i % 2;                  intra = i % 2;
89                  last = i / 2;                  last = i / 2;
90    
91                  coeff_ptr = coeff_vlc[last + 2 * intra];                  coeff_ptr = coeff_vlc[last + 2 * intra];
92    
93                  for(k = -255; k < 256; k++) { // level                  for (k = -2047; k < 2048; k++) {        // level
94                          int8_t *max_level_ptr = max_level[last + 2 * intra];                          int8_t *max_level_ptr = max_level[last + 2 * intra];
95                          int8_t *max_run_ptr = max_run[last + 2 * intra];                          int8_t *max_run_ptr = max_run[last + 2 * intra];
96    
97                          for(l = 0; l < 64; l++) { // run                          for(l = 0; l < 64; l++) { // run
98                                  int32_t level = k;                                  int32_t level = k;
99                                  uint32_t run = l;                                  ptr_t run = l;
100    
101                                  if((abs(level) <= max_level_ptr[run]) &&                                  if ((abs(level) <= max_level_ptr[run]) && (run <= (uint32_t) max_run_ptr[abs(level)])) {        // level < max_level and run < max_run
                                    (run <= max_run_ptr[abs(level)])) { // level < max_level and run < max_run  
102    
103                                                  vlc[intra]->code = 0;                                                  vlc[intra]->code = 0;
104                                                  vlc[intra]->len = 0;                                                  vlc[intra]->len = 0;
105                                                  goto loop_end;                                                  goto loop_end;
106                                  }                                  } else {
                                 else {  
107                                          if(level > 0)                                        // correct level                                          if(level > 0)                                        // correct level
108                                                  level -= max_level_ptr[run];                                                  level -= max_level_ptr[run];
109                                          else                                          else
110                                                  level += max_level_ptr[run];                                                  level += max_level_ptr[run];
111    
112                                          if((abs(level) <= max_level_ptr[run]) &&                                          if((abs(level) <= max_level_ptr[run]) &&
113                                             (run <= max_run_ptr[abs(level)])) {                                                  (run <= (uint32_t) max_run_ptr[abs(level)])) {
114    
115                                                  vlc[intra]->code = 0x06;                                                  vlc[intra]->code = 0x06;
116                                                  vlc[intra]->len = 8;                                                  vlc[intra]->len = 8;
# Line 87  Line 125 
125                                          run -= max_run_ptr[abs(level)] + 1; // and change run                                          run -= max_run_ptr[abs(level)] + 1; // and change run
126    
127                                          if((abs(level) <= max_level_ptr[run]) &&                                          if((abs(level) <= max_level_ptr[run]) &&
128                                             (run <= max_run_ptr[abs(level)])) {                                                  (run <= (uint32_t) max_run_ptr[abs(level)])) {
129    
130                                                  vlc[intra]->code = 0x0e;                                                  vlc[intra]->code = 0x0e;
131                                                  vlc[intra]->len = 9;                                                  vlc[intra]->len = 9;
# Line 96  Line 134 
134                                          run += max_run_ptr[abs(level)] + 1;                                          run += max_run_ptr[abs(level)] + 1;
135                                  }                                  }
136    
137                                  vlc[intra]->code = (uint32_t) ((l << 14) | (0x1e + last) << 20) |                                  vlc[intra]->code =
138                                                                                            (1 << 13) | ((k & 0xfff) << 1) | 1;                                          (uint32_t) ((l << 14) | (0x1e + last) << 20) | (1 << 13) |
139                                            ((k & 0xfff) << 1) | 1;
140    
141                                  vlc[intra]->len = 30;                                  vlc[intra]->len = 30;
142                                  vlc[intra]++;                                  vlc[intra]++;
# Line 105  Line 144 
144    
145  loop_end:  loop_end:
146                                  if(level != 0) {                                  if(level != 0) {
147                                          vlc[intra]->code = (vlc[intra]->code << (coeff_ptr[run][abs(level) - 1].len + 1)) |                                          vlc[intra]->code =
148                                                                             (coeff_ptr[run][abs(level) - 1].code << 1);                                                  (vlc[intra]->
149                                          vlc[intra]->len = (coeff_ptr[run][abs(level) - 1].len + 1) + vlc[intra]->len;                                                   code << (coeff_ptr[run][abs(level) - 1].len +
150                                                                      1)) | (coeff_ptr[run][abs(level) -
151                                                                                                                    1].code << 1);
152                                            vlc[intra]->len =
153                                                    (coeff_ptr[run][abs(level) - 1].len + 1) +
154                                                    vlc[intra]->len;
155    
156                                          if(level < 0)                                          if(level < 0)
157                                                  vlc[intra]->code += 1;                                                  vlc[intra]->code += 1;
# Line 122  Line 166 
166                  if(i >= 512) {                  if(i >= 512) {
167                          *vlc1 = DCT3Dtab3[(i >> 5) - 16];                          *vlc1 = DCT3Dtab3[(i >> 5) - 16];
168                          *vlc2 = DCT3Dtab0[(i >> 5) - 16];                          *vlc2 = DCT3Dtab0[(i >> 5) - 16];
169                  }                  } else if (i >= 128) {
                 else if(i >= 128) {  
170                          *vlc1 = DCT3Dtab4[(i >> 2) - 32];                          *vlc1 = DCT3Dtab4[(i >> 2) - 32];
171                          *vlc2 = DCT3Dtab1[(i >> 2) - 32];                          *vlc2 = DCT3Dtab1[(i >> 2) - 32];
172                  }                  } else if (i >= 8) {
                 else if(i >= 8) {  
173                          *vlc1 = DCT3Dtab5[i - 8];                          *vlc1 = DCT3Dtab5[i - 8];
174                          *vlc2 = DCT3Dtab2[i - 8];                          *vlc2 = DCT3Dtab2[i - 8];
175                  }                  } else {
                 else {  
176                          *vlc1 = ERRtab[i];                          *vlc1 = ERRtab[i];
177                          *vlc2 = ERRtab[i];                          *vlc2 = ERRtab[i];
178                  }                  }
# Line 144  Line 185 
185    
186  }  }
187    
188  static __inline void CodeVector(Bitstream *bs,  static __inline void
189                                  int16_t value,  CodeVector(Bitstream * bs,
190                                  int16_t f_code,                     int32_t value,
191                       int32_t f_code,
192                                  Statistics *pStat)                                  Statistics *pStat)
193  {  {
194    
# Line 163  Line 205 
205          pStat->iMvCount++;          pStat->iMvCount++;
206    
207          if (value == 0) {          if (value == 0) {
208                  BitstreamPutBits(bs, mb_motion_table[32].code, mb_motion_table[32].len);                  BitstreamPutBits(bs, mb_motion_table[32].code,
209                                                     mb_motion_table[32].len);
210          } else {          } else {
211                  uint16_t length, code, mv_res, sign;                  uint16_t length, code, mv_res, sign;
212    
# Line 188  Line 231 
231                          code = -code;                          code = -code;
232    
233                  code += 32;                  code += 32;
234                  BitstreamPutBits(bs, mb_motion_table[code].code, mb_motion_table[code].len);                  BitstreamPutBits(bs, mb_motion_table[code].code,
235                                                     mb_motion_table[code].len);
236    
237                  if(f_code)                  if(f_code)
238                          BitstreamPutBits(bs, mv_res, f_code);                          BitstreamPutBits(bs, mv_res, f_code);
# Line 197  Line 241 
241  }  }
242    
243    
244  static __inline void CodeCoeff(Bitstream *bs,  static __inline void
245                                 int16_t qcoeff[64],  CodeCoeff(Bitstream * bs,
246                      const int16_t qcoeff[64],
247                                 VLC *table,                                 VLC *table,
248                                 const uint16_t *zigzag,                                 const uint16_t *zigzag,
249                                 uint16_t intra)                                 uint16_t intra)
# Line 209  Line 254 
254          VLC *vlc;          VLC *vlc;
255    
256          j = intra;          j = intra;
257          last = 1 + intra;          last = intra;
258    
259          while((v = qcoeff[zigzag[j++]]) == 0);          while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)
260                    j++;
261    
262          do {          do {
263                    vlc = table + 64 * 2047 + (v << 6) + j - last;
264                    last = ++j;
265    
266                  // count zeroes                  // count zeroes
267                  vlc = table + 64*255 + (clip_table[2048+v] << 6) + j - last;                  while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)
268                  last = j + 1;                          j++;
                 while(j < 64 && (v = qcoeff[zigzag[j++]]) == 0);  
269    
270                  // write code                  // write code
271                  if(j != 64) {                  if(j != 64) {
272                          BitstreamPutBits(bs, vlc->code, vlc->len);                          BitstreamPutBits(bs, vlc->code, vlc->len);
273                  } else {                  } else {
274                          vlc += 64*511;                          vlc += 64 * 4095;
275                          BitstreamPutBits(bs, vlc->code, vlc->len);                          BitstreamPutBits(bs, vlc->code, vlc->len);
276                          break;                          break;
277                  }                  }
# Line 232  Line 280 
280  }  }
281    
282    
283  static void CodeBlockIntra(const MBParam * pParam,  static void
284    CodeBlockIntra(const FRAMEINFO * frame,
285                             const MACROBLOCK *pMB,                             const MACROBLOCK *pMB,
286                             int16_t qcoeff[6*64],                             int16_t qcoeff[6*64],
287                             Bitstream * bs,                             Bitstream * bs,
# Line 244  Line 293 
293          cbpy = pMB->cbp >> 2;          cbpy = pMB->cbp >> 2;
294    
295          // write mcbpc          // write mcbpc
296          if(pParam->coding_type == I_VOP) {          if (frame->coding_type == I_VOP) {
297                  mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2);                  mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2);
298                  BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, mcbpc_intra_tab[mcbpc].len);                  BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code,
299          }                                                   mcbpc_intra_tab[mcbpc].len);
300          else {          } else {
301                  mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);                  mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);
302                  BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len);                  BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,
303                                                     mcbpc_inter_tab[mcbpc].len);
304          }          }
305    
306          // ac prediction flag          // ac prediction flag
# Line 267  Line 317 
317                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, pMB->dquant, 2);
318    
319          // write interlacing          // write interlacing
320          if (pParam->global_flags & XVID_INTERLACING)          if (frame->global_flags & XVID_INTERLACING) {
         {  
321                  BitstreamPutBit(bs, pMB->field_dct);                  BitstreamPutBit(bs, pMB->field_dct);
322          }          }
   
323          // code block coeffs          // code block coeffs
324          for(i = 0; i < 6; i++)          for (i = 0; i < 6; i++) {
         {  
325                  if(i < 4)                  if(i < 4)
326                          BitstreamPutBits(bs,                          BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code,
                                          dcy_tab[qcoeff[i*64 + 0] + 255].code,  
327                                           dcy_tab[qcoeff[i*64 + 0] + 255].len);                                           dcy_tab[qcoeff[i*64 + 0] + 255].len);
328                  else                  else
329                          BitstreamPutBits(bs,                          BitstreamPutBits(bs, dcc_tab[qcoeff[i * 64 + 0] + 255].code,
                                          dcc_tab[qcoeff[i*64 + 0] + 255].code,  
330                                           dcc_tab[qcoeff[i*64 + 0] + 255].len);                                           dcc_tab[qcoeff[i*64 + 0] + 255].len);
331    
332                  if(pMB->cbp & (1 << (5 - i)))                  if (pMB->cbp & (1 << (5 - i))) {
                 {  
333                          bits = BitstreamPos(bs);                          bits = BitstreamPos(bs);
334    
335                          CodeCoeff(bs,                          CodeCoeff(bs, &qcoeff[i * 64], intra_table,
336                                    &qcoeff[i*64],                                            scan_tables[pMB->acpred_directions[i]], 1);
                                   intra_table,  
                                   scan_tables[pMB->acpred_directions[i]],  
                                   1);  
337    
338                          bits = BitstreamPos(bs) - bits;                          bits = BitstreamPos(bs) - bits;
339                          pStat->iTextBits += bits;                          pStat->iTextBits += bits;
# Line 302  Line 343 
343  }  }
344    
345    
346  static void CodeBlockInter(const MBParam * pParam,  static void
347    CodeBlockInter(const FRAMEINFO * frame,
348                             const MACROBLOCK *pMB,                             const MACROBLOCK *pMB,
349                             int16_t qcoeff[6*64],                             int16_t qcoeff[6*64],
350                             Bitstream * bs,                             Bitstream * bs,
# Line 316  Line 358 
358          cbpy = 15 - (pMB->cbp >> 2);          cbpy = 15 - (pMB->cbp >> 2);
359    
360          // write mcbpc          // write mcbpc
361          BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len);          BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,
362                                             mcbpc_inter_tab[mcbpc].len);
363    
364          // write cbpy          // write cbpy
365          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);
# Line 326  Line 369 
369                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, pMB->dquant, 2);
370    
371          // interlacing          // interlacing
372          if (pParam->global_flags & XVID_INTERLACING)          if (frame->global_flags & XVID_INTERLACING) {
         {  
373                  BitstreamPutBit(bs, pMB->field_dct);                  BitstreamPutBit(bs, pMB->field_dct);
374                  DEBUG1("codep: field_dct: ", pMB->field_dct);                  DEBUG1("codep: field_dct: ", pMB->field_dct);
375    
376                  // if inter block, write field ME flag                  // if inter block, write field ME flag
377                  if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)                  if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
                 {  
378                          BitstreamPutBit(bs, pMB->field_pred);                          BitstreamPutBit(bs, pMB->field_pred);
379                          DEBUG1("codep: field_pred: ", pMB->field_pred);                          DEBUG1("codep: field_pred: ", pMB->field_pred);
380    
381                          // write field prediction references                          // write field prediction references
382                          if (pMB->field_pred)                          if (pMB->field_pred) {
                         {  
383                                  BitstreamPutBit(bs, pMB->field_for_top);                                  BitstreamPutBit(bs, pMB->field_for_top);
384                                  BitstreamPutBit(bs, pMB->field_for_bot);                                  BitstreamPutBit(bs, pMB->field_for_bot);
385                          }                          }
386                  }                  }
387          }          }
   
388          // code motion vector(s)          // code motion vector(s)
389          for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++)          for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) {
390          {                  CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat);
391                  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);  
392          }          }
393    
394          bits = BitstreamPos(bs);          bits = BitstreamPos(bs);
# Line 366  Line 404 
404  }  }
405    
406    
407  void MBCoding(const MBParam * pParam,  void
408    MBCoding(const FRAMEINFO * frame,
409                MACROBLOCK *pMB,                MACROBLOCK *pMB,
410                int16_t qcoeff[6*64],                int16_t qcoeff[6*64],
411                Bitstream * bs,                Bitstream * bs,
412                Statistics * pStat)                Statistics * pStat)
413  {  {
414    
415          int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q);          if (frame->coding_type == P_VOP) {
416                    if (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 &&
417          if(pParam->coding_type == P_VOP) {                          pMB->mvs[0].y == 0) {
418                  if(pMB->cbp == 0 && pMB->mode == MODE_INTER &&  
419                     pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0)  #ifdef _DISABLE_SKIP
420                  {  /* disable SKIP when Bframes active until some workaround for the B-SKIP problem is found */
421                            BitstreamPutBit(bs, 0); // always coded!
422    #else
423                          BitstreamPutBit(bs, 1);         // not_coded                          BitstreamPutBit(bs, 1);         // not_coded
424    
425                          return;                          return;
426                  }  #endif
427                  else                  } else
428                          BitstreamPutBit(bs, 0);         // coded                          BitstreamPutBit(bs, 0);         // coded
429          }          }
430    
431          if(intra)          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)
432                  CodeBlockIntra(pParam, pMB, qcoeff, bs, pStat);                  CodeBlockIntra(frame, pMB, qcoeff, bs, pStat);
433          else          else
434                  CodeBlockInter(pParam, pMB, qcoeff, bs, pStat);                  CodeBlockInter(frame, pMB, qcoeff, bs, pStat);
435    
436    }
437    
438    /***************************************************************
439     * bframe encoding start
440     ***************************************************************/
441    
442    /*
443            mbtype
444            0       1b              direct(h263)            mvdb
445            1       01b             interpolate mc+q        dbquant, mvdf, mvdb
446            2       001b    backward mc+q           dbquant, mvdb
447            3       0001b   forward mc+q            dbquant, mvdf
448    */
449    
450    void
451    put_bvop_mbtype(Bitstream * bs,
452                                    int value)
453    {
454            switch (value) {
455            case 0:
456                    BitstreamPutBit(bs, 1);
457                    return;
458    
459            case 1:
460                    BitstreamPutBit(bs, 0);
461                    BitstreamPutBit(bs, 1);
462                    return;
463    
464            case 2:
465                    BitstreamPutBit(bs, 0);
466                    BitstreamPutBit(bs, 0);
467                    BitstreamPutBit(bs, 1);
468                    return;
469    
470            case 3:
471                    BitstreamPutBit(bs, 0);
472                    BitstreamPutBit(bs, 0);
473                    BitstreamPutBit(bs, 0);
474                    BitstreamPutBit(bs, 1);
475                    return;
476    
477            default:;                                       // invalid!
478    
479            }
480    
481    }
482    
483    /*
484            dbquant
485            -2      10b
486            0       0b
487            +2      11b
488    */
489    
490    void
491    put_bvop_dbquant(Bitstream * bs,
492                                     int value)
493    {
494            switch (value) {
495            case 0:
496                    BitstreamPutBit(bs, 0);
497                    return;
498    
499            case -2:
500                    BitstreamPutBit(bs, 1);
501                    BitstreamPutBit(bs, 0);
502                    return;
503    
504            case 2:
505                    BitstreamPutBit(bs, 1);
506                    BitstreamPutBit(bs, 1);
507                    return;
508    
509            default:;                                       // invalid
510            }
511    }
512    
513    
514    
515    void
516    MBCodingBVOP(const MACROBLOCK * mb,
517                             const int16_t qcoeff[6 * 64],
518                             const int32_t fcode,
519                             const int32_t bcode,
520                             Bitstream * bs,
521                             Statistics * pStat)
522    {
523            int i;
524    
525    /*      ------------------------------------------------------------------
526                    when a block is skipped it is decoded DIRECT(0,0)
527                    hence is interpolated from forward & backward frames
528            ------------------------------------------------------------------ */
529    
530            if (mb->mode == MODE_DIRECT_NONE_MV) {
531                    BitstreamPutBit(bs, 1); // skipped
532                    return;
533            }
534    
535            BitstreamPutBit(bs, 0);         // not skipped
536    
537            if (mb->cbp == 0) {
538                    BitstreamPutBit(bs, 1); // cbp == 0
539            } else {
540                    BitstreamPutBit(bs, 0); // cbp == xxx
541            }
542    
543            put_bvop_mbtype(bs, mb->mode);
544    
545            if (mb->cbp) {
546                    BitstreamPutBits(bs, mb->cbp, 6);
547            }
548    
549            if (mb->mode != MODE_DIRECT && mb->cbp != 0) {
550                    put_bvop_dbquant(bs, 0);        // todo: mb->dquant = 0
551  }  }
552    
553            if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {
554                    CodeVector(bs, mb->pmvs[0].x, fcode, pStat);
555                    CodeVector(bs, mb->pmvs[0].y, fcode, pStat);
556            }
557    
558            if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) {
559                    CodeVector(bs, mb->b_pmvs[0].x, bcode, pStat);
560                    CodeVector(bs, mb->b_pmvs[0].y, bcode, pStat);
561            }
562    
563            if (mb->mode == MODE_DIRECT) {
564                    CodeVector(bs, mb->mvs[0].x, 1, pStat);         /* fcode is always 1 for delta vector */
565                    CodeVector(bs, mb->mvs[0].y, 1, pStat);         /* prediction is always (0,0) */
566            }
567    
568            for (i = 0; i < 6; i++) {
569                    if (mb->cbp & (1 << (5 - i))) {
570                            CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0);
571                    }
572            }
573    }
574    
575    
576    
577  /***************************************************************  /***************************************************************
578   * decoding stuff starts here                                  *   * decoding stuff starts here                                  *
579   ***************************************************************/   ***************************************************************/
580    
581  int get_mcbpc_intra(Bitstream * bs)  
582    // for IVOP addbits == 0
583    // for PVOP addbits == fcode - 1
584    // for BVOP addbits == max(fcode,bcode) - 1
585    // returns true or false
586    int
587    check_resync_marker(Bitstream * bs, int addbits)
588    {
589            uint32_t nbits;
590            uint32_t code;
591            uint32_t nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER + addbits;
592    
593            nbits = BitstreamNumBitsToByteAlign(bs);
594            code = BitstreamShowBits(bs, nbits);
595    
596            if (code == (((uint32_t)1 << (nbits - 1)) - 1))
597  {  {
598                    return BitstreamShowBitsFromByteAlign(bs, nbitsresyncmarker) == RESYNC_MARKER;
599            }
600    
601            return 0;
602    }
603    
         uint32_t index;  
604    
         while((index = BitstreamShowBits(bs, 9)) == 1)  
                 BitstreamSkip(bs, 9);  
605    
606    int
607    get_mcbpc_intra(Bitstream * bs)
608    {
609    
610            uint32_t index;
611    
612            index = BitstreamShowBits(bs, 9);
613          index >>= 3;          index >>= 3;
614    
615          BitstreamSkip(bs, mcbpc_intra_table[index].len);          BitstreamSkip(bs, mcbpc_intra_table[index].len);
# Line 414  Line 618 
618    
619  }  }
620    
621  int get_mcbpc_inter(Bitstream * bs)  int
622    get_mcbpc_inter(Bitstream * bs)
623  {  {
624    
625          uint32_t index;          uint32_t index;
626    
627          while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1)          index = CLIP(BitstreamShowBits(bs, 9), 256);
                 BitstreamSkip(bs, 9);  
628    
629          BitstreamSkip(bs,  mcbpc_inter_table[index].len);          BitstreamSkip(bs,  mcbpc_inter_table[index].len);
630    
# Line 428  Line 632 
632    
633  }  }
634    
635  int get_cbpy(Bitstream * bs, int intra)  int
636    get_cbpy(Bitstream * bs,
637                     int intra)
638  {  {
639    
640          int cbpy;          int cbpy;
# Line 444  Line 650 
650    
651  }  }
652    
653  int get_mv_data(Bitstream * bs)  int
654    get_mv_data(Bitstream * bs)
655  {  {
656    
657          uint32_t index;          uint32_t index;
# Line 454  Line 661 
661    
662          index = BitstreamShowBits(bs, 12);          index = BitstreamShowBits(bs, 12);
663    
664          if(index >= 512)          if (index >= 512) {
         {  
665                  index = (index >> 8) - 2;                  index = (index >> 8) - 2;
666                  BitstreamSkip(bs, TMNMVtab0[index].len);                  BitstreamSkip(bs, TMNMVtab0[index].len);
667                  return TMNMVtab0[index].code;                  return TMNMVtab0[index].code;
668          }          }
669    
670          if(index >= 128)          if (index >= 128) {
         {  
671                  index = (index >> 2) - 32;                  index = (index >> 2) - 32;
672                  BitstreamSkip(bs, TMNMVtab1[index].len);                  BitstreamSkip(bs, TMNMVtab1[index].len);
673                  return TMNMVtab1[index].code;                  return TMNMVtab1[index].code;
# Line 475  Line 680 
680    
681  }  }
682    
683  int get_mv(Bitstream * bs, int fcode)  int
684    get_mv(Bitstream * bs,
685               int fcode)
686  {  {
687    
688          int data;          int data;
# Line 495  Line 702 
702    
703  }  }
704    
705  int get_dc_dif(Bitstream * bs, uint32_t dc_size)  int
706    get_dc_dif(Bitstream * bs,
707                       uint32_t dc_size)
708  {  {
709    
710          int code = BitstreamGetBits(bs, dc_size);          int code = BitstreamGetBits(bs, dc_size);
# Line 508  Line 717 
717    
718  }  }
719    
720  int get_dc_size_lum(Bitstream * bs)  int
721    get_dc_size_lum(Bitstream * bs)
722  {  {
723    
724          int code, i;          int code, i;
725    
726          code = BitstreamShowBits(bs, 11);          code = BitstreamShowBits(bs, 11);
727    
728          for(i = 11; i > 3; i--) {          for(i = 11; i > 3; i--) {
# Line 528  Line 739 
739  }  }
740    
741    
742  int get_dc_size_chrom(Bitstream * bs)  int
743    get_dc_size_chrom(Bitstream * bs)
744  {  {
745    
746          uint32_t code, i;          uint32_t code, i;
747    
748          code = BitstreamShowBits(bs, 12);          code = BitstreamShowBits(bs, 12);
749    
750          for(i = 12; i > 2; i--) {          for(i = 12; i > 2; i--) {
# Line 546  Line 759 
759    
760  }  }
761    
762  int get_coeff(Bitstream * bs, int *run, int *last, int intra, int short_video_header)  void
763  {  get_intra_block(Bitstream * bs,
764                                    int16_t * block,
765          uint32_t mode;                                  int direction,
766          const VLC *tab;                                  int coeff)
         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;  
         }  
   
         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);  
         }  
   
         mode = BitstreamShowBits(bs, 2);  
   
         if(mode < 3) {  
                 BitstreamSkip(bs, (mode == 2) ? 2 : 1);  
   
                 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  
                 {  
                         *run = (tab->code >> 8) & 255;  
                         level = tab->code & 255;  
                         *last = (tab->code >> 16) & 1;  
                 }  
   
                 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)  
767  {  {
768    
769          const uint16_t * scan = scan_tables[ direction ];          const uint16_t * scan = scan_tables[ direction ];
# Line 648  Line 771 
771          int run;          int run;
772          int last;          int last;
773    
774          do          do {
         {  
775                  level = get_coeff(bs, &run, &last, 1, 0);                  level = get_coeff(bs, &run, &last, 1, 0);
776                  if (run == -1)                  if (run == -1) {
                 {  
777                          DEBUG("fatal: invalid run");                          DEBUG("fatal: invalid run");
778                          break;                          break;
779                  }                  }
780                  coeff += run;                  coeff += run;
781                  block[ scan[coeff] ] = level;                  block[ scan[coeff] ] = level;
782                  if (level < -127 || level > 127)  
783                  {                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level);
784                    //DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32));
785    
786                    if (level < -127 || level > 127) {
787                          DEBUG1("warning: intra_overflow", level);                          DEBUG1("warning: intra_overflow", level);
788                  }                  }
789                  coeff++;                  coeff++;
# Line 667  Line 791 
791    
792  }  }
793    
794  void get_inter_block(Bitstream * bs, int16_t * block)  void
795    get_inter_block(Bitstream * bs,
796                                    int16_t * block)
797  {  {
798    
799          const uint16_t * scan = scan_tables[0];          const uint16_t * scan = scan_tables[0];
# Line 677  Line 803 
803          int last;          int last;
804    
805          p = 0;          p = 0;
806          do          do {
         {  
807                  level = get_coeff(bs, &run, &last, 0, 0);                  level = get_coeff(bs, &run, &last, 0, 0);
808                  if (run == -1)                  if (run == -1) {
                 {  
809                          DEBUG("fatal: invalid run");                          DEBUG("fatal: invalid run");
810                          break;                          break;
811                  }                  }
812                  p += run;                  p += run;
813    
814                  block[ scan[p] ] = level;                  block[ scan[p] ] = level;
815                  if (level < -127 || level > 127)  
816                  {                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level);
817                    // DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32));
818    
819                    if (level < -127 || level > 127) {
820                          DEBUG1("warning: inter_overflow", level);                          DEBUG1("warning: inter_overflow", level);
821                  }                  }
822                  p++;                  p++;

Legend:
Removed from v.100  
changed lines
  Added in v.335

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