[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 35, Sat Mar 16 15:52:34 2002 UTC revision 209, Fri Jun 14 13:29:07 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      *  14.04.2002 bframe encoding                                                                                            *
45      *  08.03.2002 initial version; isibaar                                                           *
46      *                                                                                                                                                        *
47      ******************************************************************************/
48    
49    
50    
51    #include <stdlib.h>
52  #include "../portab.h"  #include "../portab.h"
53  #include "bitstream.h"  #include "bitstream.h"
54  #include "zigzag.h"  #include "zigzag.h"
55  #include "vlc_codes.h"  #include "vlc_codes.h"
56    #include "mbcoding.h"
57    
58  #include "../utils/mbfunctions.h"  #include "../utils/mbfunctions.h"
59    
 #include <stdlib.h> /* malloc, free */  
   
 #define ESCAPE 7167  
60  #define ABS(X) (((X)>0)?(X):-(X))  #define ABS(X) (((X)>0)?(X):-(X))
61  #define CLIP(X,A) (X > A) ? (A) : (X)  #define CLIP(X,A) (X > A) ? (A) : (X)
62    
63  static VLC *DCT3D[2];  VLC intra_table[524032];
64    VLC inter_table[524032];
65    
66  VLC *intra_table, *inter_table;  VLC DCT3Dintra[4096];
67  static short clip_table[4096];  VLC DCT3Dinter[4096];
68    
69  void create_vlc_tables(void)  void
70    init_vlc_tables(void)
71  {  {
72    
73          int32_t k, l, i, intra, last;          int32_t k, l, i, intra, last;
74          VLC *vlc[2];          VLC *vlc[2];
75          VLC **coeff_ptr;          VLC **coeff_ptr;
76          VLC *vlc1, *vlc2;          VLC *vlc1, *vlc2;
77    
         VLC *DCT3Dintra;  
         VLC *DCT3Dinter;  
   
         DCT3Dintra = (VLC *) malloc(sizeof(VLC) * 4096);  
         DCT3Dinter = (VLC *) malloc(sizeof(VLC) * 4096);  
   
78          vlc1 = DCT3Dintra;          vlc1 = DCT3Dintra;
79          vlc2 = DCT3Dinter;          vlc2 = DCT3Dinter;
80    
81          vlc[0] = intra_table = (VLC *) malloc(128 * 511 * sizeof(VLC));          vlc[0] = intra_table;
82          vlc[1] = inter_table = (VLC *) malloc(128 * 511 * sizeof(VLC));          vlc[1] = inter_table;
   
         // initialize the clipping table  
         for(i = -2048; i < 2048; i++) {  
                 clip_table[i + 2048] = i;  
                 if(i < -255)  
                         clip_table[i + 2048] = -255;  
                 if(i > 255)  
                         clip_table[i + 2048] = 255;  
         }  
83    
84          // generate intra/inter vlc lookup table          // generate encoding vlc lookup tables
85            // the lookup table idea is taken from the excellent fame project by Vivien Chapellier
86          for(i = 0; i < 4; i++) {          for(i = 0; i < 4; i++) {
87                  intra = i % 2;                  intra = i % 2;
88                  last = i >> 1;                  last = i / 2;
89    
90                  coeff_ptr = coeff_vlc[last + (intra << 1)];                  coeff_ptr = coeff_vlc[last + 2 * intra];
91    
92                  for(k = -255; k < 256; k++) { // level                  for (k = -2047; k < 2048; k++) {        // level
93                          char *max_level_ptr = max_level[last + (intra << 1)];                          int8_t *max_level_ptr = max_level[last + 2 * intra];
94                          char *max_run_ptr = max_run[last + (intra << 1)];                          int8_t *max_run_ptr = max_run[last + 2 * intra];
95    
96                          for(l = 0; l < 64; l++) { // run                          for(l = 0; l < 64; l++) { // run
97                                  int32_t level = k;                                  int32_t level = k;
98                                  uint32_t run = l;                                  ptr_t run = l;
99    
100                                  if(abs(level) <= max_level_ptr[run] && run <= max_run_ptr[abs(level)]) {                                  if ((abs(level) <= max_level_ptr[run]) && (run <= (uint32_t) max_run_ptr[abs(level)])) {        // level < max_level and run < max_run
101    
                                         if(level > 0) {  
                                                 vlc[intra]->code = (coeff_ptr[run][level - 1].code) << 1;  
                                                 vlc[intra]->len = coeff_ptr[run][level - 1].len + 1;  
                                         }  
                                         else if(level < 0) {  
                                                 vlc[intra]->code = ((coeff_ptr[run][-level - 1].code) << 1) + 1;  
                                                 vlc[intra]->len = coeff_ptr[run][-level - 1].len + 1;  
                                         }  
                                         else {  
102                                                  vlc[intra]->code = 0;                                                  vlc[intra]->code = 0;
103                                                  vlc[intra]->len = 0;                                                  vlc[intra]->len = 0;
104                                          }                                          goto loop_end;
105                                  } else {                                  } else {
106                                          if(level > 0)                                          if (level > 0)  // correct level
107                                                  level -= max_level_ptr[run];                                                  level -= max_level_ptr[run];
108                                          else                                          else
109                                                  level += max_level_ptr[run];                                                  level += max_level_ptr[run];
110    
111                                          if(abs(level) <= max_level_ptr[run] &&                                          if ((abs(level) <= max_level_ptr[run]) &&
112                                                  run <= max_run_ptr[abs(level)]) {                                                  (run <= (uint32_t) max_run_ptr[abs(level)])) {
113    
                                                 if(level > 0) {  
                                                         vlc[intra]->code = (0x06 << (coeff_ptr[run][level - 1].len + 1)) |  
                                                                 (coeff_ptr[run][level - 1].code << 1);  
                                                         vlc[intra]->len = (coeff_ptr[run][level - 1].len + 1) + 8;  
                                                 }  
                                                 else if(level < 0) {  
                                                         vlc[intra]->code = (0x06 << (coeff_ptr[run][-level - 1].len + 1)) |  
                                                                 ((coeff_ptr[run][-level - 1].code << 1) + 1);  
                                                         vlc[intra]->len = (coeff_ptr[run][-level - 1].len + 1) + 8;  
                                                 }  
                                                 else {  
114                                                          vlc[intra]->code = 0x06;                                                          vlc[intra]->code = 0x06;
115                                                          vlc[intra]->len = 8;                                                          vlc[intra]->len = 8;
116                                                    goto loop_end;
117                                                  }                                                  }
118                                          } else {  
119                                                  if(level > 0)                                          if (level > 0)  // still here?
120                                                          level += max_level_ptr[run];                                                  level += max_level_ptr[run];    // restore level
121                                                  else                                                  else
122                                                          level -= max_level_ptr[run];                                                          level -= max_level_ptr[run];
123    
124                                                  run -= max_run_ptr[abs(level)] + 1;                                          run -= max_run_ptr[abs(level)] + 1;     // and change run
125    
126                                                  if(abs(level) <= max_level_ptr[run] &&                                          if ((abs(level) <= max_level_ptr[run]) &&
127                                                          run <= max_run_ptr[abs(level)]) {                                                  (run <= (uint32_t) max_run_ptr[abs(level)])) {
128    
                                                         if(level > 0) {  
                                                                 vlc[intra]->code = (0x0e << (coeff_ptr[run][level - 1].len + 1)) |  
                                                                         (coeff_ptr[run][level - 1].code << 1);  
                                                                 vlc[intra]->len = (coeff_ptr[run][level - 1].len + 1) + 9;  
                                                         }  
                                                         else if(level < 0) {  
                                                                 vlc[intra]->code = (0x0e << (coeff_ptr[run][-level - 1].len + 1)) |  
                                                                         ((coeff_ptr[run][-level - 1].code << 1) + 1);  
                                                                 vlc[intra]->len = (coeff_ptr[run][-level - 1].len + 1) + 9;  
                                                         }  
                                                         else {  
129                                                                  vlc[intra]->code = 0x0e;                                                                  vlc[intra]->code = 0x0e;
130                                                                  vlc[intra]->len = 9;                                                                  vlc[intra]->len = 9;
131                                                    goto loop_end;
132                                                          }                                                          }
                                                 } else {  
                                                         if(level != 0)  
133                                                                  run += max_run_ptr[abs(level)] + 1;                                                                  run += max_run_ptr[abs(level)] + 1;
134                                                          else                                  }
                                                                 run++;  
135    
136                                                          vlc[intra]->code = (uint32_t) ((0x1e + last) << 20) |                                  vlc[intra]->code =
137                                                                                  (l << 14) | (1 << 13) | ((k & 0xfff) << 1) | 1;                                          (uint32_t) ((l << 14) | (0x1e + last) << 20) | (1 << 13) |
138                                            ((k & 0xfff) << 1) | 1;
139    
140                                                          vlc[intra]->len = 30;                                                          vlc[intra]->len = 30;
141                                    vlc[intra]++;
142                                    continue;
143    
144                              loop_end:
145                                    if (level != 0) {
146                                            vlc[intra]->code =
147                                                    (vlc[intra]->
148                                                     code << (coeff_ptr[run][abs(level) - 1].len +
149                                                                      1)) | (coeff_ptr[run][abs(level) -
150                                                                                                                    1].code << 1);
151                                            vlc[intra]->len =
152                                                    (coeff_ptr[run][abs(level) - 1].len + 1) +
153                                                    vlc[intra]->len;
154    
155                                            if (level < 0)
156                                                    vlc[intra]->code += 1;
157                                                  }                                                  }
158                                          }  
                                 }  
159                                  vlc[intra]++;                                  vlc[intra]++;
160                          }                          }
161                  }                  }
162          }          }
         intra_table += 64*255; // center vlc tables  
         inter_table += 64*255; // center vlc tables  
163    
164          for(i = 0; i < 4096; i++) {          for(i = 0; i < 4096; i++) {
165                  if(i >= 512) {                  if(i >= 512) {
166                          *vlc1 = DCT3Dtab3[(i >> 5) - 16];                          *vlc1 = DCT3Dtab3[(i >> 5) - 16];
167                          *vlc2 = DCT3Dtab0[(i >> 5) - 16];                          *vlc2 = DCT3Dtab0[(i >> 5) - 16];
168                  }                  } else if (i >= 128) {
                 else if(i >= 128) {  
169                          *vlc1 = DCT3Dtab4[(i >> 2) - 32];                          *vlc1 = DCT3Dtab4[(i >> 2) - 32];
170                          *vlc2 = DCT3Dtab1[(i >> 2) - 32];                          *vlc2 = DCT3Dtab1[(i >> 2) - 32];
171                  }                  } else if (i >= 8) {
                 else if(i >= 8) {  
172                          *vlc1 = DCT3Dtab5[i - 8];                          *vlc1 = DCT3Dtab5[i - 8];
173                          *vlc2 = DCT3Dtab2[i - 8];                          *vlc2 = DCT3Dtab2[i - 8];
174                  }                  } else {
                 else {  
175                          *vlc1 = ERRtab[i];                          *vlc1 = ERRtab[i];
176                          *vlc2 = ERRtab[i];                          *vlc2 = ERRtab[i];
177                  }                  }
# Line 167  Line 184 
184    
185  }  }
186    
187  void destroy_vlc_tables(void) {  static __inline void
188    CodeVector(Bitstream * bs,
189          if(intra_table != NULL && inter_table != NULL) {                     int32_t value,
190                  intra_table -= 64*255; // uncenter vlc tables                     int32_t f_code,
191                  inter_table -= 64*255; // uncenter vlc tables                     Statistics * pStat)
   
                 free(intra_table);  
                 free(inter_table);  
         }  
   
         if(DCT3D[0] != NULL && DCT3D[1] != NULL) {  
                 free(DCT3D[0]);  
                 free(DCT3D[1]);  
         }  
   
 }  
   
 static __inline void CodeVector(Bitstream *bs, int16_t value, int16_t f_code, Statistics *pStat)  
192  {  {
193    
194          const int scale_factor = 1 << (f_code - 1);          const int scale_factor = 1 << (f_code - 1);
195          const int cmp = scale_factor << 5;          const int cmp = scale_factor << 5;
196    
# Line 198  Line 203 
203      pStat->iMvSum += value * value;      pStat->iMvSum += value * value;
204      pStat->iMvCount++;      pStat->iMvCount++;
205    
206          if (value == 0)          if (value == 0) {
207                  BitstreamPutBits(bs, mb_motion_table[32].code, mb_motion_table[32].len);                  BitstreamPutBits(bs, mb_motion_table[32].code,
208      else {                                                   mb_motion_table[32].len);
209            } else {
210                  uint16_t length, code, mv_res, sign;                  uint16_t length, code, mv_res, sign;
211    
212                  length = 16 << f_code;                  length = 16 << f_code;
# Line 224  Line 230 
230                          code = -code;                          code = -code;
231    
232                  code += 32;                  code += 32;
233                  BitstreamPutBits(bs, mb_motion_table[code].code, mb_motion_table[code].len);                  BitstreamPutBits(bs, mb_motion_table[code].code,
234                                                     mb_motion_table[code].len);
235    
236                  if(f_code)                  if(f_code)
237                          BitstreamPutBits(bs, mv_res, f_code);                          BitstreamPutBits(bs, mv_res, f_code);
238    }    }
239    
240  }  }
241    
242    
243  static __inline void CodeCoeff(Bitstream *bs, int16_t qcoeff[64], VLC *table,  static __inline void
244                                                             const uint16_t *zigzag, uint16_t intra) {  CodeCoeff(Bitstream * bs,
245                      const int16_t qcoeff[64],
246                      VLC * table,
247                      const uint16_t * zigzag,
248                      uint16_t intra)
249    {
250    
251          uint32_t j, last;          uint32_t j, last;
252          short v;          short v;
253          VLC *vlc;          VLC *vlc;
254    
255          j = intra;          j = intra;
256          last = 1 + intra;          last = intra;
257    
258          while((v = qcoeff[zigzag[j++]]) == 0);          while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)
259                    j++;
260    
261          do {          do {
262                    vlc = table + 64 * 2047 + (v << 6) + j - last;
263                    last = ++j;
264    
265                  // count zeroes                  // count zeroes
266                  vlc = table + (clip_table[2048+v] << 6) + j - last;                  while (j < 64 && (v = qcoeff[zigzag[j]]) == 0)
267                  last = j + 1;                          j++;
                 while(j < 64 && (v = qcoeff[zigzag[j++]]) == 0);  
268    
269                  // write code                  // write code
270                  if(j != 64) {                  if(j != 64) {
271                          BitstreamPutBits(bs, vlc->code, vlc->len);                          BitstreamPutBits(bs, vlc->code, vlc->len);
272                  } else {                  } else {
273                          vlc += 64*511;                          vlc += 64 * 4095;
274                          BitstreamPutBits(bs, vlc->code, vlc->len);                          BitstreamPutBits(bs, vlc->code, vlc->len);
275                          break;                          break;
276                  }                  }
277          } while(1);          } while(1);
278    
279  }  }
280    
281    
282  static void CodeBlockIntra(const MBParam * pParam, const MACROBLOCK *pMB,  static void
283                                                                    int16_t qcoeff[][64], Bitstream * bs, Statistics * pStat)  CodeBlockIntra(const FRAMEINFO * frame,
284                               const MACROBLOCK * pMB,
285                               int16_t qcoeff[6 * 64],
286                               Bitstream * bs,
287                               Statistics * pStat)
288  {  {
289    
290          uint32_t i, mcbpc, cbpy, bits;          uint32_t i, mcbpc, cbpy, bits;
291    
292          cbpy = pMB->cbp >> 2;          cbpy = pMB->cbp >> 2;
293    
294      // write mcbpc      // write mcbpc
295          if(pParam->coding_type == I_VOP) {          if (frame->coding_type == I_VOP) {
296              mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2);              mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2);
297                  BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, mcbpc_intra_tab[mcbpc].len);                  BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code,
298          }                                                   mcbpc_intra_tab[mcbpc].len);
299          else {          } else {
300              mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);              mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);
301                  BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len);                  BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,
302                                                     mcbpc_inter_tab[mcbpc].len);
303          }          }
304    
305          // ac prediction flag          // ac prediction flag
# Line 291  Line 315 
315      if(pMB->mode == MODE_INTRA_Q)      if(pMB->mode == MODE_INTRA_Q)
316                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, pMB->dquant, 2);
317    
318            // write interlacing
319            if (frame->global_flags & XVID_INTERLACING) {
320                    BitstreamPutBit(bs, pMB->field_dct);
321            }
322          // code block coeffs          // code block coeffs
323          for(i = 0; i < 6; i++)          for (i = 0; i < 6; i++) {
         {  
324                  if(i < 4)                  if(i < 4)
325                          BitstreamPutBits(bs, dcy_tab[qcoeff[i][0] + 255].code,                          BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code,
326                                                           dcy_tab[qcoeff[i][0] + 255].len);                                                           dcy_tab[qcoeff[i * 64 + 0] + 255].len);
327                  else                  else
328                          BitstreamPutBits(bs, dcc_tab[qcoeff[i][0] + 255].code,                          BitstreamPutBits(bs, dcc_tab[qcoeff[i * 64 + 0] + 255].code,
329                                           dcc_tab[qcoeff[i][0] + 255].len);                                                           dcc_tab[qcoeff[i * 64 + 0] + 255].len);
330    
331                  if(pMB->cbp & (1 << (5 - i)))                  if (pMB->cbp & (1 << (5 - i))) {
                 {  
332                          bits = BitstreamPos(bs);                          bits = BitstreamPos(bs);
333    
334                          CodeCoeff(bs, qcoeff[i], intra_table, scan_tables[pMB->acpred_directions[i]], 1);                          CodeCoeff(bs, &qcoeff[i * 64], intra_table,
335                                              scan_tables[pMB->acpred_directions[i]], 1);
336    
337                          bits = BitstreamPos(bs) - bits;                          bits = BitstreamPos(bs) - bits;
338                          pStat->iTextBits += bits;                          pStat->iTextBits += bits;
339                  }                  }
340          }          }
341    
342  }  }
343    
344    
345  static void CodeBlockInter(const MBParam * pParam, const MACROBLOCK *pMB,  static void
346                                                                    int16_t qcoeff[][64], Bitstream * bs, Statistics * pStat)  CodeBlockInter(const FRAMEINFO * frame,
347                               const MACROBLOCK * pMB,
348                               int16_t qcoeff[6 * 64],
349                               Bitstream * bs,
350                               Statistics * pStat)
351  {  {
352    
353          int32_t i;          int32_t i;
354          uint32_t bits, mcbpc, cbpy;          uint32_t bits, mcbpc, cbpy;
355    
# Line 324  Line 357 
357          cbpy = 15 - (pMB->cbp >> 2);          cbpy = 15 - (pMB->cbp >> 2);
358    
359          // write mcbpc          // write mcbpc
360      BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len);          BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,
361                                             mcbpc_inter_tab[mcbpc].len);
362    
363          // write cbpy          // write cbpy
364          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);
# Line 333  Line 367 
367      if(pMB->mode == MODE_INTER_Q)      if(pMB->mode == MODE_INTER_Q)
368                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, pMB->dquant, 2);
369    
370            // interlacing
371            if (frame->global_flags & XVID_INTERLACING) {
372                    BitstreamPutBit(bs, pMB->field_dct);
373                    DEBUG1("codep: field_dct: ", pMB->field_dct);
374    
375                    // if inter block, write field ME flag
376                    if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
377                            BitstreamPutBit(bs, pMB->field_pred);
378                            DEBUG1("codep: field_pred: ", pMB->field_pred);
379    
380                            // write field prediction references
381                            if (pMB->field_pred) {
382                                    BitstreamPutBit(bs, pMB->field_for_top);
383                                    BitstreamPutBit(bs, pMB->field_for_bot);
384                            }
385                    }
386            }
387          // code motion vector(s)          // code motion vector(s)
388          for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++)          for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) {
389          {                  CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat);
390                  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);  
391          }          }
392    
393          bits = BitstreamPos(bs);          bits = BitstreamPos(bs);
# Line 345  Line 395 
395          // code block coeffs          // code block coeffs
396          for(i = 0; i < 6; i++)          for(i = 0; i < 6; i++)
397                  if(pMB->cbp & (1 << (5 - i)))                  if(pMB->cbp & (1 << (5 - i)))
398                          CodeCoeff(bs, qcoeff[i], inter_table, scan_tables[0], 0);                          CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0);
399    
400          bits = BitstreamPos(bs) - bits;          bits = BitstreamPos(bs) - bits;
401          pStat->iTextBits += bits;          pStat->iTextBits += bits;
402    
403  }  }
404    
405    
406  void MBCoding(const MBParam * pParam, MACROBLOCK *pMB,  void
407                int16_t qcoeff[][64],  MBCoding(const FRAMEINFO * frame,
408                    Bitstream * bs, Statistics * pStat)                   MACROBLOCK * pMB,
409                     int16_t qcoeff[6 * 64],
410                     Bitstream * bs,
411                     Statistics * pStat)
412  {  {
413    
414          int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q);          int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q);
415    
416      if(pParam->coding_type == P_VOP) {          if (frame->coding_type == P_VOP) {
417                  if(pMB->cbp == 0 && pMB->mode == MODE_INTER &&                  if (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 &&
418                          pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0)                          pMB->mvs[0].y == 0) {
                 {  
419                          BitstreamPutBit(bs, 1);         // not_coded                          BitstreamPutBit(bs, 1);         // not_coded
420                          return;                          return;
421                  }                  } else
                 else  
422                          BitstreamPutBit(bs, 0);         // coded                          BitstreamPutBit(bs, 0);         // coded
423          }          }
424    
425          if(intra)          if(intra)
426                  CodeBlockIntra(pParam, pMB, qcoeff, bs, pStat);                  CodeBlockIntra(frame, pMB, qcoeff, bs, pStat);
427          else          else
428                  CodeBlockInter(pParam, pMB, qcoeff, bs, pStat);                  CodeBlockInter(frame, pMB, qcoeff, bs, pStat);
429    
430    }
431    
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    }
476    
477    /*
478            dbquant
479            -2      10b
480            0       0b
481            +2      11b
482    */
483    
484    void
485    put_bvop_dbquant(Bitstream * bs,
486                                     int value)
487    {
488            switch (value) {
489            case 0:
490                    BitstreamPutBit(bs, 0);
491                    return;
492    
493            case -2:
494                    BitstreamPutBit(bs, 1);
495                    BitstreamPutBit(bs, 0);
496                    return;
497    
498            case 2:
499                    BitstreamPutBit(bs, 1);
500                    BitstreamPutBit(bs, 1);
501                    return;
502    
503            default:;                                       // invalid
504            }
505    }
506    
507    
508    
509    void
510    MBCodingBVOP(const MACROBLOCK * mb,
511                             const int16_t qcoeff[6 * 64],
512                             const int32_t fcode,
513                             const int32_t bcode,
514                             Bitstream * bs,
515                             Statistics * pStat)
516    {
517            int i;
518    
519    /*      ------------------------------------------------------------------
520                    when a block is skipped it is decoded DIRECT(0,)
521                    hence are interpolated from forward & backward frames
522            ------------------------------------------------------------------ */
523    
524            if (mb->mode == 5) {
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                    // TODO: direct
559  }  }
560    
561            for (i = 0; i < 6; i++) {
562                    if (mb->cbp & (1 << (5 - i))) {
563                            CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0);
564                    }
565            }
566    }
567    
568    
569    
570  /***************************************************************  /***************************************************************
571   * decoding stuff starts here                                  *   * decoding stuff starts here                                  *
572   ***************************************************************/   ***************************************************************/
573    
574  int get_mcbpc_intra(Bitstream * bs)  int
575    get_mcbpc_intra(Bitstream * bs)
576  {  {
577    
578          uint32_t index;          uint32_t index;
579    
580          while((index = BitstreamShowBits(bs, 9)) == 1)          while((index = BitstreamShowBits(bs, 9)) == 1)
# Line 390  Line 583 
583          index >>= 3;          index >>= 3;
584    
585          BitstreamSkip(bs, mcbpc_intra_table[index].len);          BitstreamSkip(bs, mcbpc_intra_table[index].len);
586    
587          return mcbpc_intra_table[index].code;          return mcbpc_intra_table[index].code;
588    
589  }  }
590    
591  int get_mcbpc_inter(Bitstream * bs)  int
592    get_mcbpc_inter(Bitstream * bs)
593  {  {
594    
595          uint32_t index;          uint32_t index;
596    
597          while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1)          while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1)
598                  BitstreamSkip(bs, 9);                  BitstreamSkip(bs, 9);
599    
600      BitstreamSkip(bs,  mcbpc_inter_table[index].len);      BitstreamSkip(bs,  mcbpc_inter_table[index].len);
601    
602          return mcbpc_inter_table[index].code;          return mcbpc_inter_table[index].code;
603    
604  }  }
605    
606  int get_cbpy(Bitstream * bs, int intra)  int
607    get_cbpy(Bitstream * bs,
608                     int intra)
609  {  {
610    
611          int cbpy;          int cbpy;
612          uint32_t index = BitstreamShowBits(bs, 6);          uint32_t index = BitstreamShowBits(bs, 6);
613    
# Line 416  Line 618 
618                  cbpy = 15 - cbpy;                  cbpy = 15 - cbpy;
619    
620          return cbpy;          return cbpy;
621    
622  }  }
623    
624  int get_mv_data(Bitstream * bs)  int
625    get_mv_data(Bitstream * bs)
626  {  {
627    
628          uint32_t index;          uint32_t index;
629    
630          if(BitstreamGetBit(bs))          if(BitstreamGetBit(bs))
# Line 427  Line 632 
632    
633          index = BitstreamShowBits(bs, 12);          index = BitstreamShowBits(bs, 12);
634    
635          if(index >= 512)          if (index >= 512) {
         {  
636                  index = (index >> 8) - 2;                  index = (index >> 8) - 2;
637                  BitstreamSkip(bs, TMNMVtab0[index].len);                  BitstreamSkip(bs, TMNMVtab0[index].len);
638                  return TMNMVtab0[index].code;                  return TMNMVtab0[index].code;
639          }          }
640    
641          if(index >= 128)          if (index >= 128) {
         {  
642                  index = (index >> 2) - 32;                  index = (index >> 2) - 32;
643                  BitstreamSkip(bs, TMNMVtab1[index].len);                  BitstreamSkip(bs, TMNMVtab1[index].len);
644                  return TMNMVtab1[index].code;                  return TMNMVtab1[index].code;
# Line 445  Line 648 
648    
649          BitstreamSkip(bs, TMNMVtab2[index].len);          BitstreamSkip(bs, TMNMVtab2[index].len);
650          return TMNMVtab2[index].code;          return TMNMVtab2[index].code;
651    
652  }  }
653    
654  int get_mv(Bitstream * bs, int fcode)  int
655    get_mv(Bitstream * bs,
656               int fcode)
657  {  {
658    
659          int data;          int data;
660          int res;          int res;
661          int mv;          int mv;
# Line 463  Line 670 
670          mv = ((ABS(data) - 1) * scale_fac) + res + 1;          mv = ((ABS(data) - 1) * scale_fac) + res + 1;
671    
672          return data < 0 ? -mv : mv;          return data < 0 ? -mv : mv;
673    
674  }  }
675    
676  int get_dc_dif(Bitstream * bs, uint32_t dc_size)  int
677    get_dc_dif(Bitstream * bs,
678                       uint32_t dc_size)
679  {  {
680    
681          int code = BitstreamGetBits(bs, dc_size);          int code = BitstreamGetBits(bs, dc_size);
682          int msb = code >> (dc_size - 1);          int msb = code >> (dc_size - 1);
683    
# Line 474  Line 685 
685                  return (-1 * (code^((1 << dc_size) - 1)));                  return (-1 * (code^((1 << dc_size) - 1)));
686    
687          return code;          return code;
688    
689  }  }
690    
691  int get_dc_size_lum(Bitstream * bs)  int
692    get_dc_size_lum(Bitstream * bs)
693  {  {
694    
695          int code, i;          int code, i;
696    
697          code = BitstreamShowBits(bs, 11);          code = BitstreamShowBits(bs, 11);
698    
699          for(i = 11; i > 3; i--) {          for(i = 11; i > 3; i--) {
# Line 491  Line 706 
706    
707          BitstreamSkip(bs, dc_lum_tab[code].len);          BitstreamSkip(bs, dc_lum_tab[code].len);
708          return dc_lum_tab[code].code;          return dc_lum_tab[code].code;
709    
710  }  }
711    
712    
713  int get_dc_size_chrom(Bitstream * bs)  int
714    get_dc_size_chrom(Bitstream * bs)
715  {  {
716    
717          uint32_t code, i;          uint32_t code, i;
718    
719          code = BitstreamShowBits(bs, 12);          code = BitstreamShowBits(bs, 12);
720    
721          for(i = 12; i > 2; i--) {          for(i = 12; i > 2; i--) {
# Line 508  Line 727 
727          }          }
728    
729          return 3 - BitstreamGetBits(bs, 2);          return 3 - BitstreamGetBits(bs, 2);
 }  
   
 int get_coeff(Bitstream * bs, int *run, int *last, int intra, int short_video_header)  
 {  
     uint32_t mode;  
     const VLC *tab;  
         int32_t level;  
   
         if(short_video_header) // inter-VLCs will be used for both intra and inter blocks  
                 intra = 0;  
   
         tab = &DCT3D[intra][BitstreamShowBits(bs, 12)];  
   
         if(tab->code == -1)  
                 goto error;  
730    
         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;  
731          }          }
732    
733          if(short_video_header)  void
734    get_intra_block(Bitstream * bs,
735                                    int16_t * block,
736                                    int direction,
737                                    int coeff)
738          {          {
                 // 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);  
739    
                 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)  
 {  
740          const uint16_t * scan = scan_tables[ direction ];          const uint16_t * scan = scan_tables[ direction ];
741          int level;          int level;
742          int run;          int run;
743          int last;          int last;
744    
745          do          do {
         {  
746                  level = get_coeff(bs, &run, &last, 1, 0);                  level = get_coeff(bs, &run, &last, 1, 0);
747                  if (run == -1)                  if (run == -1) {
                 {  
748                          DEBUG("fatal: invalid run");                          DEBUG("fatal: invalid run");
749                          break;                          break;
750                  }                  }
751                  coeff += run;                  coeff += run;
752                  block[ scan[coeff] ] = level;                  block[ scan[coeff] ] = level;
753                  if (level < -127 || level > 127)                  if (level < -127 || level > 127) {
                 {  
754                          DEBUG1("warning: intra_overflow", level);                          DEBUG1("warning: intra_overflow", level);
755                  }                  }
756                  coeff++;                  coeff++;
757          } while (!last);          } while (!last);
758    
759  }  }
760    
761  void get_inter_block(Bitstream * bs, int16_t * block)  void
762    get_inter_block(Bitstream * bs,
763                                    int16_t * block)
764  {  {
765    
766          const uint16_t * scan = scan_tables[0];          const uint16_t * scan = scan_tables[0];
767          int p;          int p;
768          int level;          int level;
# Line 636  Line 770 
770          int last;          int last;
771    
772          p = 0;          p = 0;
773          do          do {
         {  
774                  level = get_coeff(bs, &run, &last, 0, 0);                  level = get_coeff(bs, &run, &last, 0, 0);
775                  if (run == -1)                  if (run == -1) {
                 {  
776                          DEBUG("fatal: invalid run");                          DEBUG("fatal: invalid run");
777                          break;                          break;
778                  }                  }
779                  p += run;                  p += run;
780    
781                  block[ scan[p] ] = level;                  block[ scan[p] ] = level;
782                  if (level < -127 || level > 127)                  if (level < -127 || level > 127) {
                 {  
783                          DEBUG1("warning: inter_overflow", level);                          DEBUG1("warning: inter_overflow", level);
784                  }                  }
785                  p++;                  p++;
786          } while (!last);          } while (!last);
787    
788  }  }

Legend:
Removed from v.35  
changed lines
  Added in v.209

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