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

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

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

revision 1.44, Fri Feb 21 08:32:34 2003 UTC revision 1.44.2.6, Sun Apr 27 19:45:52 2003 UTC
# Line 51  Line 51 
51    
52  #include <stdio.h>  #include <stdio.h>
53  #include <stdlib.h>  #include <stdlib.h>
54    #include <string.h>
55    
56  #include "../portab.h"  #include "../portab.h"
57  #include "../global.h"  #include "../global.h"
58  #include "bitstream.h"  #include "bitstream.h"
# Line 498  Line 500 
500          {          {
501                  if ((level = qcoeff[zigzag[i++]]) != 0)                  if ((level = qcoeff[zigzag[i++]]) != 0)
502                  {                  {
503                          abs_level = ABS(prev_level);                          abs_level = abs(prev_level);
504                          abs_level = abs_level < 64 ? abs_level : 0;                          abs_level = abs_level < 64 ? abs_level : 0;
505                          code      = coeff_VLC[1][0][abs_level][prev_run].code;                          code      = coeff_VLC[1][0][abs_level][prev_run].code;
506                          len               = coeff_VLC[1][0][abs_level][prev_run].len;                          len               = coeff_VLC[1][0][abs_level][prev_run].len;
# Line 518  Line 520 
520                          run++;                          run++;
521          }          }
522    
523          abs_level = ABS(prev_level);          abs_level = abs(prev_level);
524          abs_level = abs_level < 64 ? abs_level : 0;          abs_level = abs_level < 64 ? abs_level : 0;
525          code      = coeff_VLC[1][1][abs_level][prev_run].code;          code      = coeff_VLC[1][1][abs_level][prev_run].code;
526          len               = coeff_VLC[1][1][abs_level][prev_run].len;          len               = coeff_VLC[1][1][abs_level][prev_run].len;
# Line 559  Line 561 
561          {          {
562                  if ((level = qcoeff[zigzag[i++]]) != 0)                  if ((level = qcoeff[zigzag[i++]]) != 0)
563                  {                  {
564                          abs_level = ABS(prev_level);                          abs_level = abs(prev_level);
565                          abs_level = abs_level < 64 ? abs_level : 0;                          abs_level = abs_level < 64 ? abs_level : 0;
566                          len               = coeff_VLC[1][0][abs_level][prev_run].len;                          len               = coeff_VLC[1][0][abs_level][prev_run].len;
567                          bits      += len!=128 ? len : 30;                          bits      += len!=128 ? len : 30;
# Line 572  Line 574 
574                          run++;                          run++;
575          }          }
576    
577          abs_level = ABS(prev_level);          abs_level = abs(prev_level);
578          abs_level = abs_level < 64 ? abs_level : 0;          abs_level = abs_level < 64 ? abs_level : 0;
579          len               = coeff_VLC[1][1][abs_level][prev_run].len;          len               = coeff_VLC[1][1][abs_level][prev_run].len;
580          bits      += len!=128 ? len : 30;          bits      += len!=128 ? len : 30;
# Line 627  Line 629 
629    
630  #endif  #endif
631    
632    
633    static int iDQtab[5] = {
634            1, 0, -1 /* no change */, 2, 3
635    };
636    #define DQ_VALUE2INDEX(value)  iDQtab[(value)+2]
637    
638    
639  static __inline void  static __inline void
640  CodeBlockIntra(const FRAMEINFO * const frame,  CodeBlockIntra(const FRAMEINFO * const frame,
641                             const MACROBLOCK * pMB,                             const MACROBLOCK * pMB,
# Line 657  Line 666 
666                  BitstreamPutBits(bs, 0, 1);                  BitstreamPutBits(bs, 0, 1);
667    
668          // write cbpy          // write cbpy
669          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);          BitstreamPutBits(bs, xvid_cbpy_tab[cbpy].code, xvid_cbpy_tab[cbpy].len);
670    
671          // write dquant          // write dquant
672          if (pMB->mode == MODE_INTRA_Q)          if (pMB->mode == MODE_INTRA_Q)
673                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, DQ_VALUE2INDEX(pMB->dquant), 2);
674    
675          // write interlacing          // write interlacing
676          if (frame->global_flags & XVID_INTERLACING) {          if (frame->vol_flags & XVID_VOL_INTERLACING) {
677                  BitstreamPutBit(bs, pMB->field_dct);                  BitstreamPutBit(bs, pMB->field_dct);
678          }          }
679          // code block coeffs          // code block coeffs
# Line 678  Line 687 
687    
688                  if (pMB->cbp & (1 << (5 - i))) {                  if (pMB->cbp & (1 << (5 - i))) {
689                          const uint16_t *scan_table =                          const uint16_t *scan_table =
690                                  frame->global_flags & XVID_ALTERNATESCAN ?                                  frame->vop_flags & XVID_VOP_ALTERNATESCAN ?
691                                  scan_tables[2] : scan_tables[pMB->acpred_directions[i]];                                  scan_tables[2] : scan_tables[pMB->acpred_directions[i]];
692    
693                          bits = BitstreamPos(bs);                          bits = BitstreamPos(bs);
# Line 719  Line 728 
728                  BitstreamPutBit(bs, pMB->mcsel);                // mcsel: '0'=local motion, '1'=GMC                  BitstreamPutBit(bs, pMB->mcsel);                // mcsel: '0'=local motion, '1'=GMC
729    
730          // write cbpy          // write cbpy
731          BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);          BitstreamPutBits(bs, xvid_cbpy_tab[cbpy].code, xvid_cbpy_tab[cbpy].len);
732    
733          // write dquant          // write dquant
734          if (pMB->mode == MODE_INTER_Q)          if (pMB->mode == MODE_INTER_Q)
735                  BitstreamPutBits(bs, pMB->dquant, 2);                  BitstreamPutBits(bs, DQ_VALUE2INDEX(pMB->dquant), 2);
736    
737          // interlacing          // interlacing
738          if (frame->global_flags & XVID_INTERLACING) {          if (frame->vol_flags & XVID_VOL_INTERLACING) {
739                  if (pMB->cbp) {                  if (pMB->cbp) {
740                          BitstreamPutBit(bs, pMB->field_dct);                          BitstreamPutBit(bs, pMB->field_dct);
741                          DPRINTF(DPRINTF_MB,"codep: field_dct: %i", pMB->field_dct);                          DPRINTF(DPRINTF_MB,"codep: field_dct: %i", pMB->field_dct);
# Line 758  Line 767 
767                  if (pMB->cbp & (1 << (5 - i)))                  if (pMB->cbp & (1 << (5 - i)))
768                  {                  {
769                          const uint16_t *scan_table =                          const uint16_t *scan_table =
770                                  frame->global_flags & XVID_ALTERNATESCAN ?                                  frame->vop_flags & XVID_VOP_ALTERNATESCAN ?
771                                  scan_tables[2] : scan_tables[0];                                  scan_tables[2] : scan_tables[0];
772    
773  #ifdef BIGLUT  #ifdef BIGLUT
# Line 1052  Line 1061 
1061                  return data;                  return data;
1062    
1063          res = BitstreamGetBits(bs, fcode - 1);          res = BitstreamGetBits(bs, fcode - 1);
1064          mv = ((ABS(data) - 1) * scale_fac) + res + 1;          mv = ((abs(data) - 1) * scale_fac) + res + 1;
1065    
1066          return data < 0 ? -mv : mv;          return data < 0 ? -mv : mv;
1067    
# Line 1260  Line 1269 
1269    
1270  }  }
1271    
1272    
1273    
1274    
1275    
1276    
1277    
1278    /************************************************************************
1279     *               Trellis based R-D optimal quantization                 *
1280     *  not really "bitstream" or "mbcoding" related, but needs VLC tables  *
1281     *                                                                      *
1282     ************************************************************************/
1283    
1284    
1285    int __inline
1286    RunLevel_CalcBits_inter(const int16_t run, int16_t level)
1287    {
1288            const int esc_length = 30;
1289    
1290            if (!((level+32) & -64))
1291                    return coeff_VLC[0][0][level+32][run].len;
1292            else
1293                    return esc_length;
1294    }
1295    
1296    int __inline
1297    RunLevelLast_CalcBits_inter(const int16_t run, const int16_t level)
1298    {
1299            const int esc_length = 30;
1300    
1301            if (!((level+32) & -64))
1302                    return coeff_VLC[0][1][level+32][run].len;
1303            else
1304                    return esc_length;
1305    }
1306    
1307    
1308    int __inline
1309    RunLevel_CalcBits_intra(const int16_t run, int16_t level)
1310    {
1311            const int esc_length = 30;
1312            int bits;
1313    
1314            level = abs(level);
1315            if (!(level & -64)) {
1316                    bits = coeff_VLC[1][0][level][run].len;
1317                    if (bits!=128)
1318                            return bits;
1319            }
1320            return esc_length;
1321    }
1322    
1323    int __inline
1324    RunLevelLast_CalcBits_intra(const int16_t run, int16_t level)
1325    {
1326            const int esc_length = 30;
1327            int bits;
1328    
1329            level = abs(level);
1330            if (!(level & -64)) {
1331                    bits = coeff_VLC[1][1][level][run].len;
1332                    if (bits!=128)
1333                            return bits;
1334            }
1335            return esc_length;
1336    }
1337    
1338    /* based on ffmpeg's trellis quant, thanks! */
1339    /* (C) 2003 Michael Niedermayer <michaelni@gmx.at> */
1340    
1341    int
1342    dct_quantize_trellis_inter_h263_c (int16_t *qcoeff, const int16_t *data, int quant)
1343    {
1344    
1345    /* input: original quantized DCT coefficients (to calc distorion)*/
1346    /*                already quantized DCT coefficients */
1347    /*                quantizer */
1348    /* output: modified table of quantized DCT coefficients */
1349    
1350    /* maybe combining quantize&Trellis would be faster (even that it disables MMX quant) */
1351    
1352      int run_tab[65];
1353      int level_tab[65];
1354      int score_tab[65];
1355      int last_run = 0;
1356      int last_level = 0;
1357      int last_score = 0;
1358      int last_i = 0;
1359      int coeff[64];
1360      int coeff_count[64];  /* is a table useful for this 0-1 (or 1-2) table? */
1361      int last_non_zero, i;
1362    
1363      const uint16_t *const zigzag = &scan_tables[0][0];
1364            /* ordinary zigzag order, so it's not INTERLACE compatible, yet  */
1365    
1366      const int qmul = 2*quant;
1367      const int qadd = ((quant-1)|1);
1368    
1369    /* quant is not needed anymore after this */
1370    
1371      int score_limit = 0;
1372      int left_limit = 0;
1373    
1374      const int lambda = (quant * quant * 123 + 64) >> 7;   // default lagrangian
1375    
1376    /*  control lambda through a ENVIRONMENT variable (for automatic optmization) */
1377    
1378    /*
1379      const int lfact=123;  // better control of the lagrangian lambda
1380      int lambda = (quant * quant * 123 + 64) >> 7; // default lagrangian
1381    
1382      const char * const trellis_lambda = getenv("TRELLIS_LAMBDA");
1383      if(trellis_lambda)
1384                    lfact = atoi(trellis_lambda);
1385      if (lfact < 1)
1386            lfact = 123;    // why this value? Who knows? But 123 seems better than 109 = 0.85<<7
1387    
1388      lambda = (quant * quant * lfact + 64) >> 7;   // lagrangian
1389    */
1390    
1391      last_non_zero = -1;
1392      for (i = 0; i < 64; i++)
1393        {
1394          const int level = qcoeff[zigzag[i]];
1395    
1396                    if (level) {
1397                            last_non_zero = i;
1398    
1399                            if (level>0) {
1400                                    if (level==1) {
1401                                            coeff[i] = 1;
1402                                            coeff_count[i] = 0;
1403                                    } else {
1404                                            coeff[i] = level;
1405                                            coeff_count[i] = 1;
1406                                    }
1407                            } else {
1408                                    if (level==-1) {
1409                                            coeff[i] = -1;
1410                                            coeff_count[i] = 0;
1411                                    } else {
1412                                            coeff[i] = level+1;     // because we check coeff[i] and coeff[i]-1
1413                                            coeff_count[i] = 1;
1414                                    }
1415                            }
1416                } else {
1417                            coeff[i] = ((data[zigzag[i]]>>31)|1); /* +- 1 because of gap */
1418                            coeff_count[i] = 0;
1419                    }
1420        }
1421    
1422      if (last_non_zero < 0)
1423          return last_non_zero;
1424    
1425      score_tab[0] = 0;
1426    
1427      for (i = 0; i <= last_non_zero; i++) {
1428        int level, run, j;
1429        const int dct_coeff = data[zigzag[i]];
1430        const int zero_distortion = dct_coeff * dct_coeff;
1431        int best_score = 256 * 256 * 256 * 120;
1432    
1433            int distortion;
1434            int dequant_err;
1435    
1436            last_score += zero_distortion;
1437    
1438    
1439    /****************** level loop unrolled: first check coeff[i] *********/
1440        level = coeff[i];
1441    
1442            if (level > 0)  // coeff[i]==0 is not possible here
1443                    dequant_err = level * qmul + qadd - dct_coeff;
1444            else
1445                    dequant_err = level * qmul - qadd - dct_coeff;
1446    
1447            distortion = dequant_err*dequant_err;
1448    
1449            for (run = 0; run <= i - left_limit; run++) {
1450    
1451              int score = distortion + lambda*RunLevel_CalcBits_inter(run, level) + score_tab[i - run];
1452    
1453              if (score < best_score)
1454                {
1455                  best_score = score_tab[i + 1] = score;
1456                  run_tab[i + 1] = run;
1457                  level_tab[i + 1] = level;
1458                }
1459            }
1460    
1461            for (run = 0; run <= i - left_limit; run++) {
1462              int score = distortion + lambda*RunLevelLast_CalcBits_inter(run, level) + score_tab[i - run];
1463    
1464          if (score < last_score)
1465                    {
1466                      last_score = score;
1467                      last_run = run;
1468                      last_level = level;
1469                      last_i = i + 1;
1470                    }
1471        }
1472    
1473    /****************** level loop unrolled: if possible, check coeff[i]-1 *********/
1474    
1475        if (coeff_count[i]) {
1476    
1477                    level--;
1478                    dequant_err -= qmul;
1479                    distortion = dequant_err*dequant_err;
1480    
1481                    for (run = 0; run <= i - left_limit; run++) {
1482                      int score = distortion + lambda*RunLevel_CalcBits_inter(run, level) + score_tab[i-run];
1483    
1484                      if (score < best_score)
1485                        {
1486                          best_score = score_tab[i + 1] = score;
1487                          run_tab[i + 1] = run;
1488                          level_tab[i + 1] = level;
1489                        }
1490                    }
1491    
1492              for (run = 0; run <= i - left_limit; run++) {
1493                      int score = distortion + lambda*RunLevelLast_CalcBits_inter(run, level) + score_tab[i-run];
1494    
1495                  if (score < last_score)
1496                            {
1497                              last_score = score;
1498                              last_run = run;
1499                              last_level = level;
1500                              last_i = i + 1;
1501                            }
1502    
1503                }
1504            } // of check coeff[i]-1
1505    
1506    
1507    /****************** checking coeff[i]-2 doesn't isn't supported  *********/
1508    
1509    /****************** add distorsion for higher RUN (-> coeff[i]==0) *******/
1510        for (j = left_limit; j <= i; j++)
1511              score_tab[j] += zero_distortion;
1512    
1513        score_limit += zero_distortion;
1514    
1515        if (score_tab[i + 1] < score_limit)
1516              score_limit = score_tab[i + 1];
1517    
1518     // there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
1519     // so we finalize only if we have no chance of getting lower than  score_limit + 1*lambda   anymore
1520    
1521            while (score_tab[left_limit] > score_limit + lambda)
1522              left_limit++;
1523    
1524    
1525      } // end of (i=0;i<=last_non_zero;i++)
1526    
1527    
1528      last_score = 256 * 256 * 256 * 120;
1529      for (i = left_limit; i <= last_non_zero + 1; i++)
1530            {
1531              int score = score_tab[i];
1532              if (i)
1533                score += 2*lambda;  /* coded block means 2 extra bits (roughly) */
1534    
1535              if (score < last_score)
1536                {
1537                  last_score = score;
1538                  last_i = i;
1539                  last_level = level_tab[i];
1540                  last_run = run_tab[i];
1541                }
1542            }
1543    
1544      last_non_zero = last_i - 1;
1545      if (last_non_zero < 0)
1546        return last_non_zero;
1547    
1548      i = last_i;
1549    
1550      memset(qcoeff,0x00,64*sizeof(int16_t));
1551    
1552      qcoeff[zigzag[last_non_zero]] = last_level;
1553      i -= last_run + 1;
1554    
1555      for (; i > 0; i -= run_tab[i] + 1)
1556        {
1557          qcoeff[zigzag[i-1]] = level_tab[i];
1558        }
1559    
1560      return last_non_zero;
1561    }
1562    
1563    int
1564    dct_quantize_trellis_inter_mpeg_c (int16_t *qcoeff, const int16_t *data, int quant)
1565    { return 64; }
1566    
1567    
1568    
1569    
1570    
1571  /*****************************************************************************  /*****************************************************************************
1572   * VLC tables and other constant arrays   * VLC tables and other constant arrays
1573   ****************************************************************************/   ****************************************************************************/
# Line 1618  Line 1926 
1926          {5, 6}, {5, 9}, {5, 8}, {3, 7}, {2, 9}          {5, 6}, {5, 9}, {5, 8}, {3, 7}, {2, 9}
1927  };  };
1928    
1929  const VLC cbpy_tab[16] = {  const VLC xvid_cbpy_tab[16] = {
1930          {3, 4}, {5, 5}, {4, 5}, {9, 4}, {3, 5}, {7, 4}, {2, 6}, {11, 4},          {3, 4}, {5, 5}, {4, 5}, {9, 4}, {3, 5}, {7, 4}, {2, 6}, {11, 4},
1931          {2, 5}, {3, 6}, {5, 4}, {10, 4}, {4, 4}, {8, 4}, {6, 4}, {3, 2}          {2, 5}, {3, 6}, {5, 4}, {10, 4}, {4, 4}, {8, 4}, {6, 4}, {3, 2}
1932  };  };

Legend:
Removed from v.1.44  
changed lines
  Added in v.1.44.2.6

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