[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 451, 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: mbcoding.c,v 1.26 2002-09-08 14:43:04 edgomez Exp $
33     *
34     ****************************************************************************/
35    
36  #include <stdlib.h>  #include <stdlib.h>
37  #include "../portab.h"  #include "../portab.h"
38  #include "bitstream.h"  #include "bitstream.h"
# Line 7  Line 42 
42    
43  #include "../utils/mbfunctions.h"  #include "../utils/mbfunctions.h"
44    
 #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  VLC DCT3Dintra[4096];  /*****************************************************************************
49  VLC DCT3Dinter[4096];   * Local data
50  static VLC *DCT3D[2];   ****************************************************************************/
51    
52  VLC intra_table[65536];  static VLC intra_table[524032];
53  VLC inter_table[65536];  static VLC inter_table[524032];
54    
55  static short clip_table[4096];  static VLC DCT3Dintra[4096];
56    static VLC DCT3Dinter[4096];
57    
58    /*****************************************************************************
59     * Functions
60     ****************************************************************************/
61    
62  void init_vlc_tables(void)  void
63    init_vlc_tables(void)
64  {  {
65    
66          int32_t k, l, i, intra, last;          int32_t k, l, i, intra, last;
# Line 34  Line 74 
74          vlc[0] = intra_table;          vlc[0] = intra_table;
75          vlc[1] = inter_table;          vlc[1] = inter_table;
76    
77          // initialize the clipping table          /*
78          for(i = -2048; i < 2048; i++) {           * Generate encoding vlc lookup tables
79                  clip_table[i + 2048] = i;           * the lookup table idea is taken from the excellent fame project
80                  if(i < -255)           * by Vivien Chapellier
81                          clip_table[i + 2048] = -255;           */
                 if(i > 255)  
                         clip_table[i + 2048] = 255;  
         }  
   
         // generate encoding vlc lookup tables  
82          for(i = 0; i < 4; i++) {          for(i = 0; i < 4; i++) {
83                  intra = i % 2;                  intra = i % 2;
84                  last = i / 2;                  last = i / 2;
85    
86                  coeff_ptr = coeff_vlc[last + 2 * intra];                  coeff_ptr = coeff_vlc[last + 2 * intra];
87    
88                  for(k = -255; k < 256; k++) { // level                  for (k = -2047; k < 2048; k++) {        // level
89                          int8_t *max_level_ptr = max_level[last + 2 * intra];                          int8_t *max_level_ptr = max_level[last + 2 * intra];
90                          int8_t *max_run_ptr = max_run[last + 2 * intra];                          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;                                  int32_t level = k;
94                                  uint32_t run = l;                                  ptr_t run = l;
95    
96                                  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  
97    
98                                                  vlc[intra]->code = 0;                                                  vlc[intra]->code = 0;
99                                                  vlc[intra]->len = 0;                                                  vlc[intra]->len = 0;
100                                                  goto loop_end;                                                  goto loop_end;
101                                  }                                  } else {
                                 else {  
102                                          if(level > 0)                                        // correct level                                          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    
110                                                  vlc[intra]->code = 0x06;                                                  vlc[intra]->code = 0x06;
111                                                  vlc[intra]->len = 8;                                                  vlc[intra]->len = 8;
# Line 87  Line 120 
120                                          run -= max_run_ptr[abs(level)] + 1; // and change run                                          run -= max_run_ptr[abs(level)] + 1; // and change run
121    
122                                          if((abs(level) <= max_level_ptr[run]) &&                                          if((abs(level) <= max_level_ptr[run]) &&
123                                             (run <= max_run_ptr[abs(level)])) {                                                  (run <= (uint32_t) max_run_ptr[abs(level)])) {
124    
125                                                  vlc[intra]->code = 0x0e;                                                  vlc[intra]->code = 0x0e;
126                                                  vlc[intra]->len = 9;                                                  vlc[intra]->len = 9;
# Line 96  Line 129 
129                                          run += max_run_ptr[abs(level)] + 1;                                          run += max_run_ptr[abs(level)] + 1;
130                                  }                                  }
131    
132                                  vlc[intra]->code = (uint32_t) ((l << 14) | (0x1e + last) << 20) |                                  vlc[intra]->code =
133                                                                                            (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]++;                                  vlc[intra]++;
# Line 105  Line 139 
139    
140  loop_end:  loop_end:
141                                  if(level != 0) {                                  if(level != 0) {
142                                          vlc[intra]->code = (vlc[intra]->code << (coeff_ptr[run][abs(level) - 1].len + 1)) |                                          vlc[intra]->code =
143                                                                             (coeff_ptr[run][abs(level) - 1].code << 1);                                                  (vlc[intra]->
144                                          vlc[intra]->len = (coeff_ptr[run][abs(level) - 1].len + 1) + vlc[intra]->len;                                                   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)                                          if(level < 0)
152                                                  vlc[intra]->code += 1;                                                  vlc[intra]->code += 1;
# Line 122  Line 161 
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 144  Line 180 
180    
181  }  }
182    
183  static __inline void CodeVector(Bitstream *bs,  static __inline void
184                                  int16_t value,  CodeVector(Bitstream * bs,
185                                  int16_t f_code,                     int32_t value,
186                       int32_t f_code,
187                                  Statistics *pStat)                                  Statistics *pStat)
188  {  {
189    
# Line 163  Line 200 
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                                                     mb_motion_table[32].len);
205          } else {          } else {
206                  uint16_t length, code, mv_res, sign;                  uint16_t length, code, mv_res, sign;
207    
# Line 188  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);
# Line 197  Line 236 
236  }  }
237    
238    
239  static __inline void CodeCoeff(Bitstream *bs,  static __inline void
240                                 int16_t qcoeff[64],  CodeCoeff(Bitstream * bs,
241                      const int16_t qcoeff[64],
242                                 VLC *table,                                 VLC *table,
243                                 const uint16_t *zigzag,                                 const uint16_t *zigzag,
244                                 uint16_t intra)                                 uint16_t intra)
# Line 209  Line 249 
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 + 64*255 + (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                  }                  }
# Line 232  Line 275 
275  }  }
276    
277    
278  static void CodeBlockIntra(const MBParam * pParam,  static void
279    CodeBlockIntra(const FRAMEINFO * frame,
280                             const MACROBLOCK *pMB,                             const MACROBLOCK *pMB,
281                             int16_t qcoeff[6*64],                             int16_t qcoeff[6*64],
282                             Bitstream * bs,                             Bitstream * bs,
# Line 244  Line 288 
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 267  Line 312 
312                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, pMB->dquant, 2);
313    
314          // write interlacing          // write interlacing
315          if (pParam->global_flags & XVID_INTERLACING)          if (frame->global_flags & XVID_INTERLACING) {
         {  
316                  BitstreamPutBit(bs, pMB->field_dct);                  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,                          BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code,
                                          dcy_tab[qcoeff[i*64 + 0] + 255].code,  
322                                           dcy_tab[qcoeff[i*64 + 0] + 255].len);                                           dcy_tab[qcoeff[i*64 + 0] + 255].len);
323                  else                  else
324                          BitstreamPutBits(bs,                          BitstreamPutBits(bs, dcc_tab[qcoeff[i * 64 + 0] + 255].code,
                                          dcc_tab[qcoeff[i*64 + 0] + 255].code,  
325                                           dcc_tab[qcoeff[i*64 + 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,                          CodeCoeff(bs, &qcoeff[i * 64], intra_table,
331                                    &qcoeff[i*64],                                            scan_tables[pMB->acpred_directions[i]], 1);
                                   intra_table,  
                                   scan_tables[pMB->acpred_directions[i]],  
                                   1);  
332    
333                          bits = BitstreamPos(bs) - bits;                          bits = BitstreamPos(bs) - bits;
334                          pStat->iTextBits += bits;                          pStat->iTextBits += bits;
# Line 302  Line 338 
338  }  }
339    
340    
341  static void CodeBlockInter(const MBParam * pParam,  static void
342    CodeBlockInter(const FRAMEINFO * frame,
343                             const MACROBLOCK *pMB,                             const MACROBLOCK *pMB,
344                             int16_t qcoeff[6*64],                             int16_t qcoeff[6*64],
345                             Bitstream * bs,                             Bitstream * bs,
# Line 316  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 326  Line 364 
364                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, pMB->dquant, 2);
365    
366          // interlacing          // interlacing
367          if (pParam->global_flags & XVID_INTERLACING)          if (frame->global_flags & XVID_INTERLACING) {
368          {                  if (pMB->cbp) {
369                  BitstreamPutBit(bs, pMB->field_dct);                  BitstreamPutBit(bs, pMB->field_dct);
370                  DEBUG1("codep: field_dct: ", pMB->field_dct);                  DEBUG1("codep: field_dct: ", pMB->field_dct);
371                    }
372    
373                  // if inter block, write field ME flag                  // if inter block, write field ME flag
374                  if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)                  if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
                 {  
375                          BitstreamPutBit(bs, pMB->field_pred);                          BitstreamPutBit(bs, pMB->field_pred);
376                          DEBUG1("codep: field_pred: ", pMB->field_pred);                          DEBUG1("codep: field_pred: ", pMB->field_pred);
377    
378                          // write field prediction references                          // write field prediction references
379                          if (pMB->field_pred)                          if (pMB->field_pred) {
                         {  
380                                  BitstreamPutBit(bs, pMB->field_for_top);                                  BitstreamPutBit(bs, pMB->field_for_top);
381                                  BitstreamPutBit(bs, pMB->field_for_bot);                                  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 366  Line 401 
401  }  }
402    
403    
404  void MBCoding(const MBParam * pParam,  void
405    MBCoding(const FRAMEINFO * frame,
406                MACROBLOCK *pMB,                MACROBLOCK *pMB,
407                int16_t qcoeff[6*64],                int16_t qcoeff[6*64],
408                Bitstream * bs,                Bitstream * bs,
409                Statistics * pStat)                Statistics * pStat)
410  {  {
411    
412          int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q);          if (frame->coding_type == P_VOP) {
413                            BitstreamPutBit(bs, 0); // coded
414            }
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          if(pParam->coding_type == P_VOP) {  
424                  if(pMB->cbp == 0 && pMB->mode == MODE_INTER &&  void
425                     pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0)  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    
475          if(intra)  }
476                  CodeBlockIntra(pParam, pMB, qcoeff, bs, pStat);  
477          else  /*
478                  CodeBlockInter(pParam, pMB, qcoeff, bs, pStat);          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                    return BitstreamShowBitsFromByteAlign(bs, nbitsresyncmarker) == RESYNC_MARKER;
593            }
594    
595            return 0;
596    }
597    
         uint32_t index;  
598    
         while((index = BitstreamShowBits(bs, 9)) == 1)  
                 BitstreamSkip(bs, 9);  
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);
# Line 414  Line 612 
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    
# Line 428  Line 626 
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;
# Line 444  Line 644 
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;
# Line 454  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 475  Line 674 
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;
# Line 495  Line 696 
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);
# Line 508  Line 711 
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 528  Line 733 
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 546  Line 753 
753    
754  }  }
755    
756  int get_coeff(Bitstream * bs, int *run, int *last, int intra, int short_video_header)  void
757  {  get_intra_block(Bitstream * bs,
758                                    int16_t * block,
759          uint32_t mode;                                  int direction,
760          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)  
761  {  {
762    
763          const uint16_t * scan = scan_tables[ direction ];          const uint16_t * scan = scan_tables[ direction ];
# Line 648  Line 765 
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++;
# Line 667  Line 785 
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];
# Line 677  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++;

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

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