[svn] / branches / dev-api-3 / xvidcore / src / bitstream / mbcoding.c Repository:
ViewVC logotype

Diff of /branches/dev-api-3/xvidcore/src/bitstream/mbcoding.c

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

trunk/xvidcore/src/bitstream/mbcoding.c revision 114, Wed Apr 10 07:43:25 2002 UTC branches/dev-api-3/xvidcore/src/bitstream/mbcoding.c revision 622, Sun Nov 3 01:43:32 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.10.2002 GMC support - gruel                                                                                        *
45      *  28.06.2002 added check_resync_marker()                                    *
46      *  14.04.2002 bframe encoding                                                                                            *
47      *  08.03.2002 initial version; isibaar                                                           *
48      *                                                                                                                                                        *
49      ******************************************************************************/
50    
51    
52    
53  #include <stdlib.h>  #include <stdlib.h>
54  #include "../portab.h"  #include "../portab.h"
55  #include "bitstream.h"  #include "bitstream.h"
# Line 10  Line 62 
62  #define ABS(X) (((X)>0)?(X):-(X))  #define ABS(X) (((X)>0)?(X):-(X))
63  #define CLIP(X,A) (X > A) ? (A) : (X)  #define CLIP(X,A) (X > A) ? (A) : (X)
64    
65  VLC intra_table[65536];  VLC intra_table[524032];
66  VLC inter_table[65536];  VLC inter_table[524032];
67    
68  VLC DCT3Dintra[4096];  VLC DCT3Dintra[4096];
69  VLC DCT3Dinter[4096];  VLC DCT3Dinter[4096];
70    
71  static int16_t clip_table[4096];  /* not really MB related, but VLCs are only available here */
72    void bs_put_spritetrajectory(Bitstream * bs, const int val)
73    {
74            const int code = sprite_trajectory_code[val+16384].code;
75            const int len = sprite_trajectory_code[val+16384].len;
76            const int code2 = sprite_trajectory_len[len].code;
77            const int len2 = sprite_trajectory_len[len].len;
78    
79    //      printf("GMC=%d Code/Len  = %d / %d ",val, code,len);
80    //      printf("Code2 / Len2 = %d / %d \n",code2,len2);
81    
82            BitstreamPutBits(bs, code2, len2);
83            if (len) BitstreamPutBits(bs, code, len);
84    }
85    
86    
87  void init_vlc_tables(void)  void
88    init_vlc_tables(void)
89  {  {
90    
91          int32_t k, l, i, intra, last;          int32_t k, l, i, intra, last;
# Line 32  Line 99 
99          vlc[0] = intra_table;          vlc[0] = intra_table;
100          vlc[1] = inter_table;          vlc[1] = inter_table;
101    
         // 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;  
         }  
   
102          // generate encoding vlc lookup tables          // generate encoding vlc lookup tables
103            // the lookup table idea is taken from the excellent fame project by Vivien Chapellier
104          for(i = 0; i < 4; i++) {          for(i = 0; i < 4; i++) {
105                  intra = i % 2;                  intra = i % 2;
106                  last = i / 2;                  last = i / 2;
107    
108                  coeff_ptr = coeff_vlc[last + 2 * intra];                  coeff_ptr = coeff_vlc[last + 2 * intra];
109    
110                  for(k = -255; k < 256; k++) { // level                  for (k = -2047; k < 2048; k++) {        // level
111                          int8_t *max_level_ptr = max_level[last + 2 * intra];                          int8_t *max_level_ptr = max_level[last + 2 * intra];
112                          int8_t *max_run_ptr = max_run[last + 2 * intra];                          int8_t *max_run_ptr = max_run[last + 2 * intra];
113    
114                          for(l = 0; l < 64; l++) { // run                          for(l = 0; l < 64; l++) { // run
115                                  int32_t level = k;                                  int32_t level = k;
116                                  uint32_t run = l;                                  ptr_t run = l;
117    
118                                  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  
119    
120                                                  vlc[intra]->code = 0;                                                  vlc[intra]->code = 0;
121                                                  vlc[intra]->len = 0;                                                  vlc[intra]->len = 0;
122                                                  goto loop_end;                                                  goto loop_end;
123                                  }                                  } else {
                                 else {  
124                                          if(level > 0)                                        // correct level                                          if(level > 0)                                        // correct level
125                                                  level -= max_level_ptr[run];                                                  level -= max_level_ptr[run];
126                                          else                                          else
127                                                  level += max_level_ptr[run];                                                  level += max_level_ptr[run];
128    
129                                          if((abs(level) <= max_level_ptr[run]) &&                                          if((abs(level) <= max_level_ptr[run]) &&
130                                             (run <= max_run_ptr[abs(level)])) {                                                  (run <= (uint32_t) max_run_ptr[abs(level)])) {
131    
132                                                  vlc[intra]->code = 0x06;                                                  vlc[intra]->code = 0x06;
133                                                  vlc[intra]->len = 8;                                                  vlc[intra]->len = 8;
# Line 85  Line 142 
142                                          run -= max_run_ptr[abs(level)] + 1; // and change run                                          run -= max_run_ptr[abs(level)] + 1; // and change run
143    
144                                          if((abs(level) <= max_level_ptr[run]) &&                                          if((abs(level) <= max_level_ptr[run]) &&
145                                             (run <= max_run_ptr[abs(level)])) {                                                  (run <= (uint32_t) max_run_ptr[abs(level)])) {
146    
147                                                  vlc[intra]->code = 0x0e;                                                  vlc[intra]->code = 0x0e;
148                                                  vlc[intra]->len = 9;                                                  vlc[intra]->len = 9;
# Line 94  Line 151 
151                                          run += max_run_ptr[abs(level)] + 1;                                          run += max_run_ptr[abs(level)] + 1;
152                                  }                                  }
153    
154                                  vlc[intra]->code = (uint32_t) ((l << 14) | (0x1e + last) << 20) |                                  vlc[intra]->code =
155                                                                                            (1 << 13) | ((k & 0xfff) << 1) | 1;                                          (uint32_t) ((l << 14) | (0x1e + last) << 20) | (1 << 13) |
156                                            ((k & 0xfff) << 1) | 1;
157    
158                                  vlc[intra]->len = 30;                                  vlc[intra]->len = 30;
159                                  vlc[intra]++;                                  vlc[intra]++;
# Line 103  Line 161 
161    
162  loop_end:  loop_end:
163                                  if(level != 0) {                                  if(level != 0) {
164                                          vlc[intra]->code = (vlc[intra]->code << (coeff_ptr[run][abs(level) - 1].len + 1)) |                                          vlc[intra]->code =
165                                                                             (coeff_ptr[run][abs(level) - 1].code << 1);                                                  (vlc[intra]->
166                                          vlc[intra]->len = (coeff_ptr[run][abs(level) - 1].len + 1) + vlc[intra]->len;                                                   code << (coeff_ptr[run][abs(level) - 1].len +
167                                                                      1)) | (coeff_ptr[run][abs(level) -
168                                                                                                                    1].code << 1);
169                                            vlc[intra]->len =
170                                                    (coeff_ptr[run][abs(level) - 1].len + 1) +
171                                                    vlc[intra]->len;
172    
173                                          if(level < 0)                                          if(level < 0)
174                                                  vlc[intra]->code += 1;                                                  vlc[intra]->code += 1;
# Line 120  Line 183 
183                  if(i >= 512) {                  if(i >= 512) {
184                          *vlc1 = DCT3Dtab3[(i >> 5) - 16];                          *vlc1 = DCT3Dtab3[(i >> 5) - 16];
185                          *vlc2 = DCT3Dtab0[(i >> 5) - 16];                          *vlc2 = DCT3Dtab0[(i >> 5) - 16];
186                  }                  } else if (i >= 128) {
                 else if(i >= 128) {  
187                          *vlc1 = DCT3Dtab4[(i >> 2) - 32];                          *vlc1 = DCT3Dtab4[(i >> 2) - 32];
188                          *vlc2 = DCT3Dtab1[(i >> 2) - 32];                          *vlc2 = DCT3Dtab1[(i >> 2) - 32];
189                  }                  } else if (i >= 8) {
                 else if(i >= 8) {  
190                          *vlc1 = DCT3Dtab5[i - 8];                          *vlc1 = DCT3Dtab5[i - 8];
191                          *vlc2 = DCT3Dtab2[i - 8];                          *vlc2 = DCT3Dtab2[i - 8];
192                  }                  } else {
                 else {  
193                          *vlc1 = ERRtab[i];                          *vlc1 = ERRtab[i];
194                          *vlc2 = ERRtab[i];                          *vlc2 = ERRtab[i];
195                  }                  }
# Line 140  Line 200 
200          DCT3D[0] = DCT3Dinter;          DCT3D[0] = DCT3Dinter;
201          DCT3D[1] = DCT3Dintra;          DCT3D[1] = DCT3Dintra;
202    
203    
204    /* init sprite_trajectory tables */
205    /* even if GMC is not specified (it might be used later...) */
206    
207            sprite_trajectory_code[0+16384].code = 0;
208            sprite_trajectory_code[0+16384].len = 0;
209            for (k=0;k<14;k++)
210            {
211                    int limit = (1<<k);
212    
213                    for (i=-(2*limit-1); i<= -limit; i++)
214                    {
215                            sprite_trajectory_code[i+16384].code = (2*limit-1)+i;
216                            sprite_trajectory_code[i+16384].len = k+1;
217                    }
218    
219                    for (i=limit; i<= 2*limit-1; i++)
220                    {
221                            sprite_trajectory_code[i+16384].code = i;
222                            sprite_trajectory_code[i+16384].len = k+1;
223                    }
224            }
225  }  }
226    
227  static __inline void CodeVector(Bitstream *bs,  static __inline void
228                                  int16_t value,  CodeVector(Bitstream * bs,
229                                  int16_t f_code,                     int32_t value,
230                       int32_t f_code,
231                                  Statistics *pStat)                                  Statistics *pStat)
232  {  {
233    
# Line 161  Line 244 
244          pStat->iMvCount++;          pStat->iMvCount++;
245    
246          if (value == 0) {          if (value == 0) {
247                  BitstreamPutBits(bs, mb_motion_table[32].code, mb_motion_table[32].len);                  BitstreamPutBits(bs, mb_motion_table[32].code,
248                                                     mb_motion_table[32].len);
249          } else {          } else {
250                  uint16_t length, code, mv_res, sign;                  uint16_t length, code, mv_res, sign;
251    
# Line 186  Line 270 
270                          code = -code;                          code = -code;
271    
272                  code += 32;                  code += 32;
273                  BitstreamPutBits(bs, mb_motion_table[code].code, mb_motion_table[code].len);                  BitstreamPutBits(bs, mb_motion_table[code].code,
274                                                     mb_motion_table[code].len);
275    
276                  if(f_code)                  if(f_code)
277                          BitstreamPutBits(bs, mv_res, f_code);                          BitstreamPutBits(bs, mv_res, f_code);
# Line 195  Line 280 
280  }  }
281    
282    
283  static __inline void CodeCoeff(Bitstream *bs,  static __inline void
284                                 int16_t qcoeff[64],  CodeCoeff(Bitstream * bs,
285                      const int16_t qcoeff[64],
286                                 VLC *table,                                 VLC *table,
287                                 const uint16_t *zigzag,                                 const uint16_t *zigzag,
288                                 uint16_t intra)                                 uint16_t intra)
# Line 207  Line 293 
293          VLC *vlc;          VLC *vlc;
294    
295          j = intra;          j = intra;
296          last = 1 + intra;          last = intra;
297    
298          while((v = qcoeff[zigzag[j++]]) == 0);          while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)
299                    j++;
300    
301          do {          do {
302                    vlc = table + 64 * 2047 + (v << 6) + j - last;
303                    last = ++j;
304    
305                  // count zeroes                  // count zeroes
306                  vlc = table + 64*255 + (clip_table[2048+v] << 6) + j - last;                  while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)
307                  last = j + 1;                          j++;
                 while(j < 64 && (v = qcoeff[zigzag[j++]]) == 0);  
308    
309                  // write code                  // write code
310                  if(j != 64) {                  if(j != 64) {
311                          BitstreamPutBits(bs, vlc->code, vlc->len);                          BitstreamPutBits(bs, vlc->code, vlc->len);
312                  } else {                  } else {
313                          vlc += 64*511;                          vlc += 64 * 4095;
314                          BitstreamPutBits(bs, vlc->code, vlc->len);                          BitstreamPutBits(bs, vlc->code, vlc->len);
315                          break;                          break;
316                  }                  }
# Line 230  Line 319 
319  }  }
320    
321    
322  static void CodeBlockIntra(const MBParam * pParam,  static __inline void
323    CodeBlockIntra(const FRAMEINFO * const frame,
324                             const MACROBLOCK *pMB,                             const MACROBLOCK *pMB,
325                             int16_t qcoeff[6*64],                             int16_t qcoeff[6*64],
326                             Bitstream * bs,                             Bitstream * bs,
# Line 242  Line 332 
332          cbpy = pMB->cbp >> 2;          cbpy = pMB->cbp >> 2;
333    
334          // write mcbpc          // write mcbpc
335          if(pParam->coding_type == I_VOP) {          if (frame->coding_type == I_VOP) {
336                  mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2);                  mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2);
337                  BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, mcbpc_intra_tab[mcbpc].len);                  BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code,
338          }                                                   mcbpc_intra_tab[mcbpc].len);
339          else {          } else {
340                  mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);                  mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);
341                  BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len);                  BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,
342                                                     mcbpc_inter_tab[mcbpc].len);
343          }          }
344    
345          // ac prediction flag          // ac prediction flag
# Line 265  Line 356 
356                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, pMB->dquant, 2);
357    
358          // write interlacing          // write interlacing
359          if (pParam->global_flags & XVID_INTERLACING)          if (frame->global_flags & XVID_INTERLACING) {
         {  
360                  BitstreamPutBit(bs, pMB->field_dct);                  BitstreamPutBit(bs, pMB->field_dct);
361          }          }
   
362          // code block coeffs          // code block coeffs
363          for(i = 0; i < 6; i++)          for (i = 0; i < 6; i++) {
         {  
364                  if(i < 4)                  if(i < 4)
365                          BitstreamPutBits(bs,                          BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code,
                                          dcy_tab[qcoeff[i*64 + 0] + 255].code,  
366                                           dcy_tab[qcoeff[i*64 + 0] + 255].len);                                           dcy_tab[qcoeff[i*64 + 0] + 255].len);
367                  else                  else
368                          BitstreamPutBits(bs,                          BitstreamPutBits(bs, dcc_tab[qcoeff[i * 64 + 0] + 255].code,
                                          dcc_tab[qcoeff[i*64 + 0] + 255].code,  
369                                           dcc_tab[qcoeff[i*64 + 0] + 255].len);                                           dcc_tab[qcoeff[i*64 + 0] + 255].len);
370    
371                  if(pMB->cbp & (1 << (5 - i)))                  if (pMB->cbp & (1 << (5 - i))) {
372                  {                          const uint16_t *scan_table =
373                                    frame->global_flags & XVID_ALTERNATESCAN ?
374                                    scan_tables[2] : scan_tables[pMB->acpred_directions[i]];
375    
376                          bits = BitstreamPos(bs);                          bits = BitstreamPos(bs);
377    
378                          CodeCoeff(bs,                          CodeCoeff(bs, &qcoeff[i * 64], intra_table, scan_table, 1);
                                   &qcoeff[i*64],  
                                   intra_table,  
                                   scan_tables[pMB->acpred_directions[i]],  
                                   1);  
379    
380                          bits = BitstreamPos(bs) - bits;                          bits = BitstreamPos(bs) - bits;
381                          pStat->iTextBits += bits;                          pStat->iTextBits += bits;
# Line 300  Line 385 
385  }  }
386    
387    
388  static void CodeBlockInter(const MBParam * pParam,  static void
389    CodeBlockInter(const FRAMEINFO * const frame,
390                             const MACROBLOCK *pMB,                             const MACROBLOCK *pMB,
391                             int16_t qcoeff[6*64],                             int16_t qcoeff[6*64],
392                             Bitstream * bs,                             Bitstream * bs,
# Line 309  Line 395 
395    
396          int32_t i;          int32_t i;
397          uint32_t bits, mcbpc, cbpy;          uint32_t bits, mcbpc, cbpy;
398            int mcsel=0;
399    
400          mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);          mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);
401          cbpy = 15 - (pMB->cbp >> 2);          cbpy = 15 - (pMB->cbp >> 2);
402    
403          // write mcbpc          // write mcbpc
404          BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len);          BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,
405                                             mcbpc_inter_tab[mcbpc].len);
406    
407            if ( (frame->coding_type == S_VOP) && (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) )
408            {
409                    if (frame->quarterpel) {
410                            if ( (pMB->qmvs[0].x == frame->GMC_MV.x) && (pMB->qmvs[0].y == frame->GMC_MV.y) )
411                                    mcsel=1;
412                    } else {
413                            if ( (pMB->mvs[0].x == frame->GMC_MV.x) && (pMB->mvs[0].y == frame->GMC_MV.y) )
414                                    mcsel=1;
415                    }
416                    BitstreamPutBit(bs, mcsel);             // mcsel: '0'=local motion, '1'=GMC
417            }
418    
419          // write cbpy          // write cbpy
420          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);
# Line 324  Line 424 
424                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, pMB->dquant, 2);
425    
426          // interlacing          // interlacing
427          if (pParam->global_flags & XVID_INTERLACING)          if (frame->global_flags & XVID_INTERLACING) {
428          {                  if (pMB->cbp) {
429                  BitstreamPutBit(bs, pMB->field_dct);                  BitstreamPutBit(bs, pMB->field_dct);
430                  DEBUG1("codep: field_dct: ", pMB->field_dct);                  DEBUG1("codep: field_dct: ", pMB->field_dct);
431                    }
432    
433                  // if inter block, write field ME flag                  // if inter block, write field ME flag
434                  if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)                  if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
                 {  
435                          BitstreamPutBit(bs, pMB->field_pred);                          BitstreamPutBit(bs, pMB->field_pred);
436                          DEBUG1("codep: field_pred: ", pMB->field_pred);                          DEBUG1("codep: field_pred: ", pMB->field_pred);
437    
438                          // write field prediction references                          // write field prediction references
439                          if (pMB->field_pred)                          if (pMB->field_pred) {
                         {  
440                                  BitstreamPutBit(bs, pMB->field_for_top);                                  BitstreamPutBit(bs, pMB->field_for_top);
441                                  BitstreamPutBit(bs, pMB->field_for_bot);                                  BitstreamPutBit(bs, pMB->field_for_bot);
442                          }                          }
443                  }                  }
444          }          }
445            // code motion vector(s) if motion is local
446          // code motion vector(s)          if (mcsel==0)
447          for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++)                  for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) {
448          {                          CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat);
449                  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);  
450          }          }
451    
452          bits = BitstreamPos(bs);          bits = BitstreamPos(bs);
# Line 356  Line 454 
454          // code block coeffs          // code block coeffs
455          for(i = 0; i < 6; i++)          for(i = 0; i < 6; i++)
456                  if(pMB->cbp & (1 << (5 - i)))                  if(pMB->cbp & (1 << (5 - i)))
457                          CodeCoeff(bs, &qcoeff[i*64], inter_table, scan_tables[0], 0);                  {
458                            const uint16_t *scan_table =
459                                    frame->global_flags & XVID_ALTERNATESCAN ?
460                                    scan_tables[2] : scan_tables[0];
461    
462                            CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_table, 0);
463                    }
464    
465          bits = BitstreamPos(bs) - bits;          bits = BitstreamPos(bs) - bits;
466          pStat->iTextBits += bits;          pStat->iTextBits += bits;
   
467  }  }
468    
469    
470  void MBCoding(const MBParam * pParam,  void
471    MBCoding(const FRAMEINFO * const frame,
472                MACROBLOCK *pMB,                MACROBLOCK *pMB,
473                int16_t qcoeff[6*64],                int16_t qcoeff[6*64],
474                Bitstream * bs,                Bitstream * bs,
475                Statistics * pStat)                Statistics * pStat)
476  {  {
477            if (frame->coding_type != I_VOP)
478                            BitstreamPutBit(bs, 0); // not_coded
479    
480          int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q);          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)
481                    CodeBlockIntra(frame, pMB, qcoeff, bs, pStat);
482            else
483                    CodeBlockInter(frame, pMB, qcoeff, bs, pStat);
484    
485    }
486    
487          if(pParam->coding_type == P_VOP) {  /*
488                  if(pMB->cbp == 0 && pMB->mode == MODE_INTER &&  // moved to mbcoding.h so that in can be 'static __inline'
489                     pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0)  void
490    MBSkip(Bitstream * bs)
491                  {                  {
492                          BitstreamPutBit(bs, 1);         // not_coded          BitstreamPutBit(bs, 1); // not coded
493    }
494    */
495    
496    /***************************************************************
497     * bframe encoding start
498     ***************************************************************/
499    
500    /*
501            mbtype
502            0       1b              direct(h263)            mvdb
503            1       01b             interpolate mc+q        dbquant, mvdf, mvdb
504            2       001b    backward mc+q           dbquant, mvdb
505            3       0001b   forward mc+q            dbquant, mvdf
506    */
507    
508    static __inline void
509    put_bvop_mbtype(Bitstream * bs,
510                                    int value)
511    {
512            switch (value) {
513                    case MODE_FORWARD:
514                            BitstreamPutBit(bs, 0);
515                    case MODE_BACKWARD:
516                            BitstreamPutBit(bs, 0);
517                    case MODE_INTERPOLATE:
518                            BitstreamPutBit(bs, 0);
519                    case MODE_DIRECT:
520                            BitstreamPutBit(bs, 1);
521                    default:
522                            break;
523            }
524    }
525    
526    /*
527            dbquant
528            -2      10b
529            0       0b
530            +2      11b
531    */
532    
533    static __inline void
534    put_bvop_dbquant(Bitstream * bs,
535                                     int value)
536    {
537            switch (value) {
538            case 0:
539                    BitstreamPutBit(bs, 0);
540                          return;                          return;
541    
542            case -2:
543                    BitstreamPutBit(bs, 1);
544                    BitstreamPutBit(bs, 0);
545                    return;
546    
547            case 2:
548                    BitstreamPutBit(bs, 1);
549                    BitstreamPutBit(bs, 1);
550                    return;
551    
552            default:;                                       // invalid
553                  }                  }
                 else  
                         BitstreamPutBit(bs, 0);         // coded  
554          }          }
555    
         if(intra)  
                 CodeBlockIntra(pParam, pMB, qcoeff, bs, pStat);  
         else  
                 CodeBlockInter(pParam, pMB, qcoeff, bs, pStat);  
556    
557    
558    void
559    MBCodingBVOP(const MACROBLOCK * mb,
560                             const int16_t qcoeff[6 * 64],
561                             const int32_t fcode,
562                             const int32_t bcode,
563                             Bitstream * bs,
564                             Statistics * pStat,
565                             int direction)
566    {
567            int vcode = fcode;
568            unsigned int i;
569    
570    /*      ------------------------------------------------------------------
571                    when a block is skipped it is decoded DIRECT(0,0)
572                    hence is interpolated from forward & backward frames
573            ------------------------------------------------------------------ */
574    
575            if (mb->mode == MODE_DIRECT_NONE_MV) {
576                    BitstreamPutBit(bs, 1); // skipped
577                    return;
578            }
579    
580            BitstreamPutBit(bs, 0);         // not skipped
581    
582            if (mb->cbp == 0) {
583                    BitstreamPutBit(bs, 1); // cbp == 0
584            } else {
585                    BitstreamPutBit(bs, 0); // cbp == xxx
586            }
587    
588            put_bvop_mbtype(bs, mb->mode);
589    
590            if (mb->cbp) {
591                    BitstreamPutBits(bs, mb->cbp, 6);
592            }
593    
594            if (mb->mode != MODE_DIRECT && mb->cbp != 0) {
595                    put_bvop_dbquant(bs, 0);        // todo: mb->dquant = 0
596            }
597    
598            switch (mb->mode) {
599                    case MODE_INTERPOLATE:
600                            CodeVector(bs, mb->pmvs[1].x, vcode, pStat); //forward vector of interpolate mode
601                            CodeVector(bs, mb->pmvs[1].y, vcode, pStat);
602                    case MODE_BACKWARD:
603                            vcode = bcode;
604                    case MODE_FORWARD:
605                            CodeVector(bs, mb->pmvs[0].x, vcode, pStat);
606                            CodeVector(bs, mb->pmvs[0].y, vcode, pStat);
607                            break;
608                    case MODE_DIRECT:
609                            CodeVector(bs, mb->pmvs[3].x, 1, pStat);        // fcode is always 1 for delta vector
610                            CodeVector(bs, mb->pmvs[3].y, 1, pStat);        // prediction is always (0,0)
611                    default: break;
612            }
613    
614            for (i = 0; i < 6; i++) {
615                    if (mb->cbp & (1 << (5 - i))) {
616                            CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[direction], 0);
617                    }
618  }  }
619    }
620    
621    
622    
623  /***************************************************************  /***************************************************************
624   * decoding stuff starts here                                  *   * decoding stuff starts here                                  *
625   ***************************************************************/   ***************************************************************/
626    
627  int get_mcbpc_intra(Bitstream * bs)  
628    // for IVOP addbits == 0
629    // for PVOP addbits == fcode - 1
630    // for BVOP addbits == max(fcode,bcode) - 1
631    // returns true or false
632    int
633    check_resync_marker(Bitstream * bs, int addbits)
634    {
635            uint32_t nbits;
636            uint32_t code;
637            uint32_t nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER + addbits;
638    
639            nbits = BitstreamNumBitsToByteAlign(bs);
640            code = BitstreamShowBits(bs, nbits);
641    
642            if (code == (((uint32_t)1 << (nbits - 1)) - 1))
643  {  {
644                    return BitstreamShowBitsFromByteAlign(bs, nbitsresyncmarker) == RESYNC_MARKER;
645            }
646    
647          uint32_t index;          return 0;
648    }
649    
650    
651    
652    int
653    get_mcbpc_intra(Bitstream * bs)
654    {
655    
656          while((index = BitstreamShowBits(bs, 9)) == 1)          uint32_t index;
                 BitstreamSkip(bs, 9);  
657    
658            index = BitstreamShowBits(bs, 9);
659          index >>= 3;          index >>= 3;
660    
661          BitstreamSkip(bs, mcbpc_intra_table[index].len);          BitstreamSkip(bs, mcbpc_intra_table[index].len);
# Line 412  Line 664 
664    
665  }  }
666    
667  int get_mcbpc_inter(Bitstream * bs)  int
668    get_mcbpc_inter(Bitstream * bs)
669  {  {
670    
671          uint32_t index;          uint32_t index;
672    
673          while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1)          index = CLIP(BitstreamShowBits(bs, 9), 256);
                 BitstreamSkip(bs, 9);  
674    
675          BitstreamSkip(bs,  mcbpc_inter_table[index].len);          BitstreamSkip(bs,  mcbpc_inter_table[index].len);
676    
# Line 426  Line 678 
678    
679  }  }
680    
681  int get_cbpy(Bitstream * bs, int intra)  int
682    get_cbpy(Bitstream * bs,
683                     int intra)
684  {  {
685    
686          int cbpy;          int cbpy;
# Line 442  Line 696 
696    
697  }  }
698    
699  int get_mv_data(Bitstream * bs)  static __inline int
700    get_mv_data(Bitstream * bs)
701  {  {
702    
703          uint32_t index;          uint32_t index;
# Line 452  Line 707 
707    
708          index = BitstreamShowBits(bs, 12);          index = BitstreamShowBits(bs, 12);
709    
710          if(index >= 512)          if (index >= 512) {
         {  
711                  index = (index >> 8) - 2;                  index = (index >> 8) - 2;
712                  BitstreamSkip(bs, TMNMVtab0[index].len);                  BitstreamSkip(bs, TMNMVtab0[index].len);
713                  return TMNMVtab0[index].code;                  return TMNMVtab0[index].code;
714          }          }
715    
716          if(index >= 128)          if (index >= 128) {
         {  
717                  index = (index >> 2) - 32;                  index = (index >> 2) - 32;
718                  BitstreamSkip(bs, TMNMVtab1[index].len);                  BitstreamSkip(bs, TMNMVtab1[index].len);
719                  return TMNMVtab1[index].code;                  return TMNMVtab1[index].code;
# Line 473  Line 726 
726    
727  }  }
728    
729  int get_mv(Bitstream * bs, int fcode)  int
730    get_mv(Bitstream * bs,
731               int fcode)
732  {  {
733    
734          int data;          int data;
# Line 493  Line 748 
748    
749  }  }
750    
751  int get_dc_dif(Bitstream * bs, uint32_t dc_size)  int
752    get_dc_dif(Bitstream * bs,
753                       uint32_t dc_size)
754  {  {
755    
756          int code = BitstreamGetBits(bs, dc_size);          int code = BitstreamGetBits(bs, dc_size);
# Line 506  Line 763 
763    
764  }  }
765    
766  int get_dc_size_lum(Bitstream * bs)  int
767    get_dc_size_lum(Bitstream * bs)
768  {  {
769    
770          int code, i;          int code, i;
771    
772          code = BitstreamShowBits(bs, 11);          code = BitstreamShowBits(bs, 11);
773    
774          for(i = 11; i > 3; i--) {          for(i = 11; i > 3; i--) {
# Line 526  Line 785 
785  }  }
786    
787    
788  int get_dc_size_chrom(Bitstream * bs)  int
789    get_dc_size_chrom(Bitstream * bs)
790  {  {
791    
792          uint32_t code, i;          uint32_t code, i;
793    
794          code = BitstreamShowBits(bs, 12);          code = BitstreamShowBits(bs, 12);
795    
796          for(i = 12; i > 2; i--) {          for(i = 12; i > 2; i--) {
# Line 544  Line 805 
805    
806  }  }
807    
808  void get_intra_block(Bitstream * bs, int16_t * block, int direction, int coeff)  void
809    get_intra_block(Bitstream * bs,
810                                    int16_t * block,
811                                    int direction,
812                                    int coeff)
813  {  {
814    
815          const uint16_t * scan = scan_tables[ direction ];          const uint16_t * scan = scan_tables[ direction ];
816          int level;          int level, run, last;
         int run;  
         int last;  
817    
818          do          do {
         {  
819                  level = get_coeff(bs, &run, &last, 1, 0);                  level = get_coeff(bs, &run, &last, 1, 0);
820                  if (run == -1)                  if (run == -1) {
                 {  
821                          DEBUG("fatal: invalid run");                          DEBUG("fatal: invalid run");
822                          break;                          break;
823                  }                  }
824                  coeff += run;                  coeff += run;
825                  block[ scan[coeff] ] = level;                  block[ scan[coeff] ] = level;
826                  if (level < -127 || level > 127)  
827                  {                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level);
828                    //DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32));
829    
830                    if (level < -127 || level > 127) {
831                          DEBUG1("warning: intra_overflow", level);                          DEBUG1("warning: intra_overflow", level);
832                  }                  }
833                  coeff++;                  coeff++;
# Line 571  Line 835 
835    
836  }  }
837    
838  void get_inter_block(Bitstream * bs, int16_t * block)  void
839    get_inter_block(Bitstream * bs,
840                                    int16_t * block,
841                                    int direction)
842  {  {
843    
844          const uint16_t * scan = scan_tables[0];          const uint16_t *scan = scan_tables[direction];
845          int p;          int p;
846          int level;          int level;
847          int run;          int run;
848          int last;          int last;
849    
850          p = 0;          p = 0;
851          do          do {
         {  
852                  level = get_coeff(bs, &run, &last, 0, 0);                  level = get_coeff(bs, &run, &last, 0, 0);
853                  if (run == -1)                  if (run == -1) {
                 {  
854                          DEBUG("fatal: invalid run");                          DEBUG("fatal: invalid run");
855                          break;                          break;
856                  }                  }
857                  p += run;                  p += run;
858    
859                  block[ scan[p] ] = level;                  block[ scan[p] ] = level;
860                  if (level < -127 || level > 127)  
861                  {                  DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level);
862                    // DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32));
863    
864                    if (level < -127 || level > 127) {
865                          DEBUG1("warning: inter_overflow", level);                          DEBUG1("warning: inter_overflow", level);
866                  }                  }
867                  p++;                  p++;

Legend:
Removed from v.114  
changed lines
  Added in v.622

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