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

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

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

revision 100, Thu Apr 4 13:58:18 2002 UTC revision 161, Mon May 6 03:51:43 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>  #include <stdlib.h>
52  #include "../portab.h"  #include "../portab.h"
53  #include "bitstream.h"  #include "bitstream.h"
# Line 7  Line 57 
57    
58  #include "../utils/mbfunctions.h"  #include "../utils/mbfunctions.h"
59    
 #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    VLC intra_table[524032];
64    VLC inter_table[524032];
65    
66  VLC DCT3Dintra[4096];  VLC DCT3Dintra[4096];
67  VLC DCT3Dinter[4096];  VLC DCT3Dinter[4096];
 static VLC *DCT3D[2];  
   
 VLC intra_table[65536];  
 VLC inter_table[65536];  
   
 static short clip_table[4096];  
68    
69  void init_vlc_tables(void)  void init_vlc_tables(void)
70  {  {
# Line 34  Line 80 
80          vlc[0] = intra_table;          vlc[0] = intra_table;
81          vlc[1] = inter_table;          vlc[1] = inter_table;
82    
         // 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          // generate encoding vlc lookup tables          // generate encoding vlc lookup tables
84            // the lookup table idea is taken from the excellent fame project by Vivien Chapellier
85          for(i = 0; i < 4; i++) {          for(i = 0; i < 4; i++) {
86                  intra = i % 2;                  intra = i % 2;
87                  last = i / 2;                  last = i / 2;
88    
89                  coeff_ptr = coeff_vlc[last + 2 * intra];                  coeff_ptr = coeff_vlc[last + 2 * intra];
90    
91                  for(k = -255; k < 256; k++) { // level                  for(k = -2047; k < 2048; k++) { // level
92                          int8_t *max_level_ptr = max_level[last + 2 * intra];                          int8_t *max_level_ptr = max_level[last + 2 * intra];
93                          int8_t *max_run_ptr = max_run[last + 2 * intra];                          int8_t *max_run_ptr = max_run[last + 2 * intra];
94    
# Line 59  Line 97 
97                                  uint32_t run = l;                                  uint32_t run = l;
98    
99                                  if((abs(level) <= max_level_ptr[run]) &&                                  if((abs(level) <= max_level_ptr[run]) &&
100                                     (run <= max_run_ptr[abs(level)])) { // level < max_level and run < max_run                                     (run <= (uint32_t)max_run_ptr[abs(level)])) { // level < max_level and run < max_run
101    
102                                                  vlc[intra]->code = 0;                                                  vlc[intra]->code = 0;
103                                                  vlc[intra]->len = 0;                                                  vlc[intra]->len = 0;
# Line 72  Line 110 
110                                                  level += max_level_ptr[run];                                                  level += max_level_ptr[run];
111    
112                                          if((abs(level) <= max_level_ptr[run]) &&                                          if((abs(level) <= max_level_ptr[run]) &&
113                                             (run <= max_run_ptr[abs(level)])) {                                             (run <= (uint32_t) max_run_ptr[abs(level)])) {
114    
115                                                  vlc[intra]->code = 0x06;                                                  vlc[intra]->code = 0x06;
116                                                  vlc[intra]->len = 8;                                                  vlc[intra]->len = 8;
# Line 87  Line 125 
125                                          run -= max_run_ptr[abs(level)] + 1; // and change run                                          run -= max_run_ptr[abs(level)] + 1; // and change run
126    
127                                          if((abs(level) <= max_level_ptr[run]) &&                                          if((abs(level) <= max_level_ptr[run]) &&
128                                             (run <= max_run_ptr[abs(level)])) {                                             (run <= (uint32_t) max_run_ptr[abs(level)])) {
129    
130                                                  vlc[intra]->code = 0x0e;                                                  vlc[intra]->code = 0x0e;
131                                                  vlc[intra]->len = 9;                                                  vlc[intra]->len = 9;
# Line 145  Line 183 
183  }  }
184    
185  static __inline void CodeVector(Bitstream *bs,  static __inline void CodeVector(Bitstream *bs,
186                                  int16_t value,                                  int32_t value,
187                                  int16_t f_code,                                  int32_t f_code,
188                                  Statistics *pStat)                                  Statistics *pStat)
189  {  {
190    
# Line 198  Line 236 
236    
237    
238  static __inline void CodeCoeff(Bitstream *bs,  static __inline void CodeCoeff(Bitstream *bs,
239                                 int16_t qcoeff[64],                                 const int16_t qcoeff[64],
240                                 VLC *table,                                 VLC *table,
241                                 const uint16_t *zigzag,                                 const uint16_t *zigzag,
242                                 uint16_t intra)                                 uint16_t intra)
# Line 209  Line 247 
247          VLC *vlc;          VLC *vlc;
248    
249          j = intra;          j = intra;
250          last = 1 + intra;          last = intra;
251    
252          while((v = qcoeff[zigzag[j++]]) == 0);          while(j < 64 && (v = qcoeff[zigzag[j]]) == 0) j++;
253    
254          do {          do {
255                    vlc = table + 64 * 2047 + (v << 6) + j - last;
256                    last = ++j;
257    
258                  // count zeroes                  // count zeroes
259                  vlc = table + 64*255 + (clip_table[2048+v] << 6) + j - last;                  while(j < 64 && (v = qcoeff[zigzag[j]]) == 0) j++;
                 last = j + 1;  
                 while(j < 64 && (v = qcoeff[zigzag[j++]]) == 0);  
260    
261                  // write code                  // write code
262                  if(j != 64) {                  if(j != 64) {
263                          BitstreamPutBits(bs, vlc->code, vlc->len);                          BitstreamPutBits(bs, vlc->code, vlc->len);
264                  } else {                  } else {
265                          vlc += 64*511;                          vlc += 64 * 4095;
266                          BitstreamPutBits(bs, vlc->code, vlc->len);                          BitstreamPutBits(bs, vlc->code, vlc->len);
267                          break;                          break;
268                  }                  }
# Line 232  Line 271 
271  }  }
272    
273    
274  static void CodeBlockIntra(const MBParam * pParam,  static void CodeBlockIntra(const FRAMEINFO * frame,
275                             const MACROBLOCK *pMB,                             const MACROBLOCK *pMB,
276                             int16_t qcoeff[6*64],                             int16_t qcoeff[6*64],
277                             Bitstream * bs,                             Bitstream * bs,
# Line 244  Line 283 
283          cbpy = pMB->cbp >> 2;          cbpy = pMB->cbp >> 2;
284    
285          // write mcbpc          // write mcbpc
286          if(pParam->coding_type == I_VOP) {          if(frame->coding_type == I_VOP) {
287                  mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2);                  mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2);
288                  BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, mcbpc_intra_tab[mcbpc].len);                  BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, mcbpc_intra_tab[mcbpc].len);
289          }          }
# Line 267  Line 306 
306                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, pMB->dquant, 2);
307    
308          // write interlacing          // write interlacing
309          if (pParam->global_flags & XVID_INTERLACING)          if (frame->global_flags & XVID_INTERLACING)
310          {          {
311                  BitstreamPutBit(bs, pMB->field_dct);                  BitstreamPutBit(bs, pMB->field_dct);
312          }          }
# Line 302  Line 341 
341  }  }
342    
343    
344  static void CodeBlockInter(const MBParam * pParam,  static void CodeBlockInter(const FRAMEINFO * frame,
345                             const MACROBLOCK *pMB,                             const MACROBLOCK *pMB,
346                             int16_t qcoeff[6*64],                             int16_t qcoeff[6*64],
347                             Bitstream * bs,                             Bitstream * bs,
# Line 326  Line 365 
365                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, pMB->dquant, 2);
366    
367          // interlacing          // interlacing
368          if (pParam->global_flags & XVID_INTERLACING)          if (frame->global_flags & XVID_INTERLACING)
369          {          {
370                  BitstreamPutBit(bs, pMB->field_dct);                  BitstreamPutBit(bs, pMB->field_dct);
371                  DEBUG1("codep: field_dct: ", pMB->field_dct);                  DEBUG1("codep: field_dct: ", pMB->field_dct);
# Line 349  Line 388 
388          // code motion vector(s)          // code motion vector(s)
389          for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++)          for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++)
390          {          {
391                  CodeVector(bs, pMB->pmvs[i].x, pParam->fixed_code, pStat);                  CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat);
392                  CodeVector(bs, pMB->pmvs[i].y, pParam->fixed_code, pStat);                  CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat);
393          }          }
394    
395          bits = BitstreamPos(bs);          bits = BitstreamPos(bs);
# Line 366  Line 405 
405  }  }
406    
407    
408  void MBCoding(const MBParam * pParam,  void MBCoding(const FRAMEINFO * frame,
409                MACROBLOCK *pMB,                MACROBLOCK *pMB,
410                int16_t qcoeff[6*64],                int16_t qcoeff[6*64],
411                Bitstream * bs,                Bitstream * bs,
# Line 375  Line 414 
414    
415          int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q);          int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q);
416    
417          if(pParam->coding_type == P_VOP) {          if(frame->coding_type == P_VOP) {
418                  if(pMB->cbp == 0 && pMB->mode == MODE_INTER &&                  if(pMB->cbp == 0 && pMB->mode == MODE_INTER &&
419                     pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0)                     pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0)
420                  {                  {
# Line 387  Line 426 
426          }          }
427    
428          if(intra)          if(intra)
429                  CodeBlockIntra(pParam, pMB, qcoeff, bs, pStat);                  CodeBlockIntra(frame, pMB, qcoeff, bs, pStat);
430          else          else
431                  CodeBlockInter(pParam, pMB, qcoeff, bs, pStat);                  CodeBlockInter(frame, pMB, qcoeff, bs, pStat);
432    
433    }
434    
435    /***************************************************************
436     * bframe encoding start
437     ***************************************************************/
438    
439    /*
440            mbtype
441            0       1b              direct(h263)            mvdb
442            1       01b             interpolate mc+q        dbquant, mvdf, mvdb
443            2       001b    backward mc+q           dbquant, mvdb
444            3       0001b   forward mc+q            dbquant, mvdf
445    */
446    
447    void put_bvop_mbtype(Bitstream * bs, int value)
448    {
449            switch(value)
450            {
451            case 0 :        BitstreamPutBit(bs, 1);
452                                    return;
453    
454            case 1 :        BitstreamPutBit(bs, 0);
455                                    BitstreamPutBit(bs, 1);
456                                    return;
457    
458            case 2 :        BitstreamPutBit(bs, 0);
459                                    BitstreamPutBit(bs, 0);
460                                    BitstreamPutBit(bs, 1);
461                                    return;
462    
463            case 3 :        BitstreamPutBit(bs, 0);
464                                    BitstreamPutBit(bs, 0);
465                                    BitstreamPutBit(bs, 0);
466                                    BitstreamPutBit(bs, 1);
467                                    return;
468    
469            default :       ; // invalid!
470    
471            }
472    
473    }
474    
475    /*
476            dbquant
477            -2      10b
478            0       0b
479            +2      11b
480    */
481    
482    void put_bvop_dbquant(Bitstream *bs, int value)
483    {
484            switch (value)
485            {
486            case 0 :        BitstreamPutBit(bs, 0);
487                                    return;
488    
489            case -2 :       BitstreamPutBit(bs, 1);
490                                    BitstreamPutBit(bs, 0);
491                                    return;
492    
493            case 2 :        BitstreamPutBit(bs, 1);
494                                    BitstreamPutBit(bs, 1);
495                                    return;
496    
497            default :       ; // invalid
498  }  }
499    }
500    
501    
502    
503    void MBCodingBVOP(const MACROBLOCK * mb,
504                                      const int16_t qcoeff[6*64],
505                                      const int32_t fcode,
506                                      const int32_t bcode,
507                                      Bitstream * bs,
508                                      Statistics * pStat)
509    {
510            int i;
511    
512    /*      ------------------------------------------------------------------
513                    when a block is skipped it is decoded DIRECT(0,)
514                    hence are interpolated from forward & backward frames
515            ------------------------------------------------------------------ */
516    
517            if (mb->mode == 5)
518            {
519                    BitstreamPutBit(bs, 1);         // skipped
520                    return;
521            }
522    
523            BitstreamPutBit(bs, 0);         // not skipped
524    
525            if (mb->cbp == 0)
526            {
527                    BitstreamPutBit(bs, 1);         // cbp == 0
528            }
529            else
530            {
531                    BitstreamPutBit(bs, 0);         // cbp == xxx
532            }
533    
534            put_bvop_mbtype(bs, mb->mode);
535    
536            if (mb->cbp)
537            {
538                    BitstreamPutBits(bs, mb->cbp, 6);
539            }
540    
541            if (mb->mode != MODE_DIRECT && mb->cbp != 0)
542            {
543                    put_bvop_dbquant(bs, 0); // todo: mb->dquant = 0
544            }
545    
546            if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD)
547            {
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            {
554                CodeVector(bs, mb->b_pmvs[0].x, bcode, pStat);
555                CodeVector(bs, mb->b_pmvs[0].y, bcode, pStat);
556            }
557    
558            if (mb->mode == MODE_DIRECT)
559            {
560                    // TODO: direct
561            }
562    
563        for (i = 0; i < 6; i++)
564        {
565                    if (mb->cbp & (1 << (5 - i)))
566                    {
567                            CodeCoeff(bs, &qcoeff[i*64], inter_table, scan_tables[0], 0);
568                    }
569        }
570    }
571    
572    
573    
574  /***************************************************************  /***************************************************************
# Line 546  Line 723 
723    
724  }  }
725    
 int get_coeff(Bitstream * bs, int *run, int *last, int intra, int short_video_header)  
 {  
   
         uint32_t mode;  
         const VLC *tab;  
         int32_t level;  
   
         if(short_video_header) // inter-VLCs will be used for both intra and inter blocks  
                 intra = 0;  
   
         tab = &DCT3D[intra][BitstreamShowBits(bs, 12)];  
   
         if(tab->code == -1)  
                 goto error;  
   
         BitstreamSkip(bs, tab->len);  
   
         if(tab->code != ESCAPE) {  
                 if(!intra)  
                 {  
                         *run = (tab->code >> 4) & 255;  
                         level = tab->code & 15;  
                         *last = (tab->code >> 12) & 1;  
                 }  
                 else  
                 {  
                         *run = (tab->code >> 8) & 255;  
                         level = tab->code & 255;  
                         *last = (tab->code >> 16) & 1;  
                 }  
                 return BitstreamGetBit(bs) ? -level : level;  
         }  
   
         if(short_video_header)  
         {  
                 // escape mode 4 - H.263 type, only used if short_video_header = 1  
                 *last = BitstreamGetBit(bs);  
                 *run = BitstreamGetBits(bs, 6);  
                 level = BitstreamGetBits(bs, 8);  
   
                 if (level == 0 || level == 128)  
                         DEBUG1("Illegal LEVEL for ESCAPE mode 4:", level);  
   
                 return (level >= 128 ? -(256 - level) : level);  
         }  
   
         mode = BitstreamShowBits(bs, 2);  
   
         if(mode < 3) {  
                 BitstreamSkip(bs, (mode == 2) ? 2 : 1);  
   
                 tab = &DCT3D[intra][BitstreamShowBits(bs, 12)];  
                 if (tab->code == -1)  
                         goto error;  
   
                 BitstreamSkip(bs, tab->len);  
   
                 if (!intra) {  
                         *run = (tab->code >> 4) & 255;  
                         level = tab->code & 15;  
                         *last = (tab->code >> 12) & 1;  
                 }  
                 else  
                 {  
                         *run = (tab->code >> 8) & 255;  
                         level = tab->code & 255;  
                         *last = (tab->code >> 16) & 1;  
                 }  
   
                 if(mode < 2) // first escape mode, level is offset  
                         level += max_level[*last + (!intra<<1)][*run]; // need to add back the max level  
                 else if(mode == 2)  // second escape mode, run is offset  
                         *run += max_run[*last + (!intra<<1)][level] + 1;  
   
                 return BitstreamGetBit(bs) ? -level : level;  
         }  
   
         // third escape mode - fixed length codes  
         BitstreamSkip(bs, 2);  
         *last = BitstreamGetBits(bs, 1);  
         *run = BitstreamGetBits(bs, 6);  
         BitstreamSkip(bs, 1);                           // marker  
         level = BitstreamGetBits(bs, 12);  
         BitstreamSkip(bs, 1);                           // marker  
   
         return (level & 0x800) ? (level | (-1 ^ 0xfff)) : level;  
   
  error:  
         *run = VLC_ERROR;  
         return 0;  
   
 }  
   
   
726  void get_intra_block(Bitstream * bs, int16_t * block, int direction, int coeff)  void get_intra_block(Bitstream * bs, int16_t * block, int direction, int coeff)
727  {  {
728    
# Line 686  Line 769 
769                          break;                          break;
770                  }                  }
771                  p += run;                  p += run;
772    
773                  block[ scan[p] ] = level;                  block[ scan[p] ] = level;
774                  if (level < -127 || level > 127)                  if (level < -127 || level > 127)
775                  {                  {

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

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