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

Diff of /xvidcore/src/encoder.c

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

revision 1.124, Wed Feb 15 20:58:43 2006 UTC revision 1.137, Wed Dec 29 22:39:35 2010 UTC
# Line 3  Line 3 
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  - Encoder main module -   *  - Encoder main module -
5   *   *
6   *  Copyright(C) 2002     Michael Militzer <isibaar@xvid.org>   *  Copyright(C) 2002-2010 Michael Militzer <isibaar@xvid.org>
7   *                         2002-2003 Peter Ross <pross@xvid.org>   *                         2002-2003 Peter Ross <pross@xvid.org>
8   *                         2002   Daniel Smith <danielsmith@astroboymail.com>   *                         2002   Daniel Smith <danielsmith@astroboymail.com>
9   *   *
# Line 49  Line 49 
49  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
50  #include "utils/mem_align.h"  #include "utils/mem_align.h"
51    
52    # include "motion/motion_smp.h"
53    
54    
55  /*****************************************************************************  /*****************************************************************************
56   * Local function prototypes   * Local function prototypes
57   ****************************************************************************/   ****************************************************************************/
# Line 432  Line 435 
435          /* timestamp stuff */          /* timestamp stuff */
436    
437          pEnc->mbParam.m_stamp = 0;          pEnc->mbParam.m_stamp = 0;
438          pEnc->m_framenum = 0;          pEnc->m_framenum = create->start_frame_num;
439          pEnc->current->stamp = 0;          pEnc->current->stamp = 0;
440          pEnc->reference->stamp = 0;          pEnc->reference->stamp = 0;
441    
# Line 441  Line 444 
444          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
445          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
446    
447            /* slices */
448            pEnc->num_slices = MIN(MAX(1, create->num_slices), (int) pEnc->mbParam.mb_height);
449    
450            /* multithreaded stuff */
451            if (create->num_threads > 0) {
452                    int t = MIN(create->num_threads, (int) (pEnc->mbParam.mb_height>>1)); /* at least two rows per thread */
453                    int threads_per_slice = MAX(1, (t / pEnc->num_slices));
454                    int rows_per_thread = (pEnc->mbParam.mb_height + threads_per_slice - 1) / threads_per_slice;
455    
456                    pEnc->num_threads = t;
457                    pEnc->smpData = xvid_malloc(t*sizeof(SMPData), CACHE_LINE);
458                    if (!pEnc->smpData)
459                            goto xvid_err_nosmp;
460    
461                    /* tmp bitstream buffer for slice coding */
462                    pEnc->smpData[0].tmp_buffer = xvid_malloc(16*pEnc->mbParam.edged_width*pEnc->mbParam.mb_height*sizeof(uint8_t), CACHE_LINE);
463                    if (! pEnc->smpData[0].tmp_buffer) goto xvid_err_nosmp;
464    
465                    for (n = 0; n < t; n++) {
466                            int s = MIN(pEnc->num_threads, pEnc->num_slices);
467    
468                            pEnc->smpData[n].complete_count_self =
469                                    xvid_malloc(rows_per_thread * sizeof(int), CACHE_LINE);
470    
471                            if (!pEnc->smpData[n].complete_count_self)
472                                    goto xvid_err_nosmp;
473    
474                            if (n > 0 && n < s) {
475                                    pEnc->smpData[n].bs = (Bitstream *) xvid_malloc(sizeof(Bitstream), CACHE_LINE);
476                                    if (!pEnc->smpData[n].bs)
477                                            goto xvid_err_nosmp;
478    
479                                    pEnc->smpData[n].sStat = (Statistics *) xvid_malloc(sizeof(Statistics), CACHE_LINE);
480                                    if (!pEnc->smpData[n].sStat)
481                                            goto xvid_err_nosmp;
482    
483                                    pEnc->smpData[n].tmp_buffer = pEnc->smpData[0].tmp_buffer + 16*(((n-1)*pEnc->mbParam.edged_width*pEnc->mbParam.mb_height)/s);
484                                    BitstreamInit(pEnc->smpData[n].bs, pEnc->smpData[n].tmp_buffer, 0);
485                            }
486    
487                            if (n != 0)
488                                    pEnc->smpData[n].complete_count_above =
489                                            pEnc->smpData[n-1].complete_count_self;
490                    }
491                    pEnc->smpData[0].complete_count_above =
492                            pEnc->smpData[t-1].complete_count_self - 1;
493    
494            } else {
495      xvid_err_nosmp:
496                    /* no SMP */
497                    if (pEnc->smpData) {
498                            if (pEnc->smpData[0].tmp_buffer)
499                                    xvid_free(pEnc->smpData[0].tmp_buffer);
500                    }
501                    else {
502                            pEnc->smpData = xvid_malloc(1*sizeof(SMPData), CACHE_LINE);
503                            if (pEnc->smpData == NULL)
504                                    goto xvid_err_memory5;
505                    }
506    
507                    create->num_threads = 0;
508            }
509    
510          create->handle = (void *) pEnc;          create->handle = (void *) pEnc;
511    
512          init_timer();          init_timer();
# Line 629  Line 695 
695                  xvid_free(pEnc->temp_dquants);                  xvid_free(pEnc->temp_dquants);
696          }          }
697    
698            if ((pEnc->mbParam.plugin_flags & XVID_REQLAMBDA)) {
699                    xvid_free(pEnc->temp_lambda);
700            }
701    
702          if (pEnc->num_plugins>0) {          if (pEnc->num_plugins>0) {
703                  xvid_plg_destroy_t pdestroy;                  xvid_plg_destroy_t pdestroy;
# Line 647  Line 716 
716    
717          xvid_free(pEnc->mbParam.mpeg_quant_matrices);          xvid_free(pEnc->mbParam.mpeg_quant_matrices);
718    
719          if (pEnc->num_plugins>0)          if (pEnc->num_zones > 0)
720                  xvid_free(pEnc->zones);                  xvid_free(pEnc->zones);
721    
722            if (pEnc->num_threads > 0) {
723                    for (i = 1; i < MAX(1, MIN(pEnc->num_threads, pEnc->num_slices)); i++) {
724                            xvid_free(pEnc->smpData[i].bs);
725                            xvid_free(pEnc->smpData[i].sStat);
726                    }
727                    if (pEnc->smpData[0].tmp_buffer) xvid_free(pEnc->smpData[0].tmp_buffer);
728    
729                    for (i = 0; i < pEnc->num_threads; i++)
730                            xvid_free(pEnc->smpData[i].complete_count_self);
731            }
732            xvid_free(pEnc->smpData);
733    
734          xvid_free(pEnc);          xvid_free(pEnc);
735    
736          return 0;  /* ok */          return 0;  /* ok */
# Line 722  Line 803 
803                  if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {                  if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
804                          data.dquant = pEnc->temp_dquants;                          data.dquant = pEnc->temp_dquants;
805                          data.dquant_stride = pEnc->mbParam.mb_width;                          data.dquant_stride = pEnc->mbParam.mb_width;
806                          memset(data.dquant, 0, data.mb_width*data.mb_height);                          memset(data.dquant, 0, data.mb_width*data.mb_height*sizeof(int));
807                  }                  }
808    
809                  if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {                  if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
810                          int block = 0;                          int block = 0;
811                            emms();
812                          data.lambda = pEnc->temp_lambda;                          data.lambda = pEnc->temp_lambda;
813                          for(i = 0;i < pEnc->mbParam.mb_height; i++)                          for(i = 0;i < pEnc->mbParam.mb_height; i++)
814                                  for(j = 0;j < pEnc->mbParam.mb_width; j++)                                  for(j = 0;j < pEnc->mbParam.mb_width; j++)
# Line 953  Line 1035 
1035          return;          return;
1036  }  }
1037    
   
1038  /*****************************************************************************  /*****************************************************************************
1039   * IPB frame encoder entry point   * IPB frame encoder entry point
1040   *   *
# Line 1263  Line 1344 
1344                  if (pEnc->current->stamp > 0) {                  if (pEnc->current->stamp > 0) {
1345                          call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);                          call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1346                  }                  }
1347                  else          else if (stats) {
1348                          stats->type = XVID_TYPE_NOTHING;                          stats->type = XVID_TYPE_NOTHING;
1349          }          }
1350            }
1351    
1352          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1353           * closed-gop           * closed-gop
# Line 1447  Line 1529 
1529    
1530    
1531  static __inline void  static __inline void
1532  CodeIntraMB(Encoder * pEnc,  CodeIntraMB(MACROBLOCK * pMB)
                         MACROBLOCK * pMB)  
1533  {  {
   
1534          pMB->mode = MODE_INTRA;          pMB->mode = MODE_INTRA;
1535    
1536          /* zero mv statistics */          /* zero mv statistics */
# Line 1464  Line 1544 
1544          }          }
1545  }  }
1546    
1547    static void
1548    SliceCodeI(SMPData *data)
1549    {
1550            Encoder *pEnc = (Encoder *) data->pEnc;
1551            Bitstream *bs = (Bitstream *) data->bs;
1552    
1553            uint16_t x, y;
1554            int mb_width = pEnc->mbParam.mb_width;
1555            int mb_height = pEnc->mbParam.mb_height;
1556    
1557            int bound = 0, num_slices = pEnc->num_slices;
1558            FRAMEINFO *const current = pEnc->current;
1559    
1560            DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1561            DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1562    
1563            if (data->start_y > 0) { /* write resync marker */
1564                    bound = data->start_y*mb_width;
1565                    write_video_packet_header(bs, &pEnc->mbParam, current, bound);
1566            }
1567    
1568            for (y = data->start_y; y < data->stop_y; y++) {
1569                    int new_bound = mb_width * ((((y*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices);
1570    
1571                    if (new_bound > bound) {
1572                            bound = new_bound;
1573                            BitstreamPadAlways(bs);
1574                            write_video_packet_header(bs, &pEnc->mbParam, current, bound);
1575                    }
1576    
1577                    for (x = 0; x < mb_width; x++) {
1578                            MACROBLOCK *pMB = &current->mbs[x + y * mb_width];
1579    
1580                            CodeIntraMB(pMB);
1581    
1582                            MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,
1583                                                              dct_codes, qcoeff);
1584    
1585                            start_timer();
1586                            MBPrediction(current, x, y, mb_width, qcoeff, bound);
1587                            stop_prediction_timer();
1588    
1589                            start_timer();
1590                            MBCoding(current, pMB, qcoeff, bs, data->sStat);
1591                            stop_coding_timer();
1592    
1593                    }
1594            }
1595    
1596            emms();
1597            BitstreamPadAlways(bs);
1598    }
1599    
1600    static __inline void
1601    SerializeBitstreams(Encoder *pEnc, FRAMEINFO *current, Bitstream *bs, int num_threads)
1602    {
1603            int k;
1604            uint32_t pos = BitstreamLength(bs);
1605    
1606            for (k = 1; k < num_threads; k++) {
1607                    uint32_t len = BitstreamLength(pEnc->smpData[k].bs);
1608    
1609                    memcpy((void *)((ptr_t)bs->start + pos),
1610                               (void *)((ptr_t)pEnc->smpData[k].bs->start), len);
1611    
1612                    current->length = pos += len;
1613    
1614                    /* collect stats */
1615                    current->sStat.iTextBits += pEnc->smpData[k].sStat->iTextBits;
1616                    current->sStat.kblks += pEnc->smpData[k].sStat->kblks;
1617                    current->sStat.mblks += pEnc->smpData[k].sStat->mblks;
1618                    current->sStat.ublks += pEnc->smpData[k].sStat->ublks;
1619                    current->sStat.iMVBits += pEnc->smpData[k].sStat->iMVBits;
1620            }
1621    
1622            if (num_threads > 1) {
1623                    uint32_t pos32 = pos>>2;
1624                    bs->tail = bs->start + pos32;
1625                    bs->pos = 8*(pos - (pos32<<2));
1626                    bs->buf = 0;
1627    
1628                    if (bs->pos > 0) {
1629                            uint32_t pos8 = bs->pos/8;
1630                            memset((void *)((ptr_t)bs->tail+pos8), 0, (4-pos8));
1631                            pos = *bs->tail;
1632    #ifndef ARCH_IS_BIG_ENDIAN
1633                            BSWAP(pos);
1634    #endif
1635                            bs->buf = pos;
1636                    }
1637            }
1638    }
1639    
1640  static int  static int
1641  FrameCodeI(Encoder * pEnc,  FrameCodeI(Encoder * pEnc,
1642                     Bitstream * bs)                     Bitstream * bs)
1643  {  {
1644          int bits = BitstreamPos(bs);          int bits = BitstreamPos(bs);
1645          int mb_width = pEnc->mbParam.mb_width;          int bound = 0, num_slices = pEnc->num_slices;
1646            int num_threads = MAX(1, MIN(pEnc->num_threads, num_slices));
1647            int slices_per_thread = (num_slices*1024 / num_threads);
1648          int mb_height = pEnc->mbParam.mb_height;          int mb_height = pEnc->mbParam.mb_height;
1649            void * status = NULL;
1650          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          uint16_t k;
         DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);  
   
         uint16_t x, y;  
1651    
1652          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1653          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
# Line 1487  Line 1657 
1657    
1658          SetMacroblockQuants(&pEnc->mbParam, pEnc->current);          SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1659    
1660          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current, num_slices);
1661    
1662          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1663    
# Line 1496  Line 1666 
1666          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant);
1667    
1668          pEnc->current->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = 0;
         pEnc->current->sStat.iMVBits = 0;  
         pEnc->current->sStat.kblks = mb_width * mb_height;  
         pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;  
1669    
1670          for (y = 0; y < mb_height; y++)          /* multithreaded intra coding - dispatch threads */
1671                  for (x = 0; x < mb_width; x++) {          for (k = 0; k < num_threads; k++) {
1672                          MACROBLOCK *pMB =                  int add = ((slices_per_thread + 512) >> 10);
                                 &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
1673    
1674                          CodeIntraMB(pEnc, pMB);                  slices_per_thread += ((num_slices*1024 / num_threads) - add*1024);
1675    
1676                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,                  pEnc->smpData[k].pEnc = (void *) pEnc;
1677                                                            dct_codes, qcoeff);                  pEnc->smpData[k].stop_y = (((bound+add) * mb_height + (num_slices-1)) / num_slices);
1678                    pEnc->smpData[k].start_y = ((bound * mb_height + (num_slices-1)) / num_slices);
1679    
1680                          start_timer();                  bound += add;
                         MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);  
                         stop_prediction_timer();  
1681    
1682                          start_timer();                  if (k > 0) {
1683                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                          BitstreamReset(pEnc->smpData[k].bs);
1684                          stop_coding_timer();                          pEnc->smpData[k].sStat->iTextBits = 0;
1685                  }                  }
1686            }
1687            pEnc->smpData[0].bs = bs;
1688            pEnc->smpData[0].sStat = &pEnc->current->sStat;
1689    
1690          emms();          /* create threads */
1691            for (k = 1; k < num_threads; k++) {
1692                    pthread_create(&pEnc->smpData[k].handle, NULL,
1693                                   (void*)SliceCodeI, (void*)&pEnc->smpData[k]);
1694            }
1695    
1696          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */          SliceCodeI(&pEnc->smpData[0]);
1697    
1698            /* wait until all threads are finished */
1699            for (k = 1; k < num_threads; k++) {
1700                    pthread_join(pEnc->smpData[k].handle, &status);
1701            }
1702    
1703            pEnc->current->length = BitstreamLength(bs) - (bits/8);
1704    
1705          pEnc->current->length = (BitstreamPos(bs) - bits) / 8;          /* reassemble the pieces together */
1706            SerializeBitstreams(pEnc, pEnc->current, bs, num_threads);
1707    
1708            pEnc->current->sStat.iMVBits = 0;
1709            pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1710            pEnc->current->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1711    
1712          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
1713          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
# Line 1560  Line 1744 
1744    
1745  #define BFRAME_SKIP_THRESHHOLD 30  #define BFRAME_SKIP_THRESHHOLD 30
1746    
1747  /* FrameCodeP also handles S(GMC)-VOPs */  static void
1748  static int  SliceCodeP(SMPData *data)
 FrameCodeP(Encoder * pEnc,  
                    Bitstream * bs)  
1749  {  {
1750          int bits = BitstreamPos(bs);          Encoder *pEnc = (Encoder *) data->pEnc;
1751            Bitstream *bs = (Bitstream *) data->bs;
1752    
1753            int x, y, k;
1754            FRAMEINFO *const current = pEnc->current;
1755            FRAMEINFO *const reference = pEnc->reference;
1756            MBParam * const pParam = &pEnc->mbParam;
1757            int mb_width = pParam->mb_width;
1758            int mb_height = pParam->mb_height;
1759    
1760          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1761          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1762    
1763          int x, y, k;          int bound = 0, num_slices = pEnc->num_slices;
1764    
1765            if (data->start_y > 0) { /* write resync marker */
1766                    bound = data->start_y*mb_width;
1767                    write_video_packet_header(bs, pParam, current, bound);
1768            }
1769    
1770            for (y = data->start_y; y < data->stop_y; y++) {
1771                    int new_bound = mb_width * ((((y*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices);
1772    
1773                    if (new_bound > bound) {
1774                            bound = new_bound;
1775                            BitstreamPadAlways(bs);
1776                            write_video_packet_header(bs, pParam, current, bound);
1777                    }
1778    
1779                    for (x = 0; x < mb_width; x++) {
1780                            MACROBLOCK *pMB = &current->mbs[x + y * pParam->mb_width];
1781                            int skip_possible;
1782    
1783                            if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
1784                                    CodeIntraMB(pMB);
1785                                    MBTransQuantIntra(pParam, current, pMB, x, y,
1786                                                                      dct_codes, qcoeff);
1787    
1788                                    start_timer();
1789                                    MBPrediction(current, x, y, pParam->mb_width, qcoeff, bound);
1790                                    stop_prediction_timer();
1791    
1792                                    data->sStat->kblks++;
1793    
1794                                    MBCoding(current, pMB, qcoeff, bs, data->sStat);
1795                                    stop_coding_timer();
1796                                    continue;
1797                            }
1798    
1799                            start_timer();
1800                            MBMotionCompensation(pMB, x, y, &reference->image,
1801                                                                     &pEnc->vInterH, &pEnc->vInterV,
1802                                                                     &pEnc->vInterHV, &pEnc->vGMC,
1803                                                                     &current->image,
1804                                                                     dct_codes, pParam->width,
1805                                                                     pParam->height,
1806                                                                     pParam->edged_width,
1807                                                                     (current->vol_flags & XVID_VOL_QUARTERPEL),
1808                                                                     current->rounding_type,
1809                                                                     data->RefQ);
1810    
1811                            stop_comp_timer();
1812    
1813                            pMB->field_pred = 0;
1814    
1815                            if (pMB->cbp != 0) {
1816                                    pMB->cbp = MBTransQuantInter(pParam, current, pMB, x, y,
1817                                                                 dct_codes, qcoeff);
1818                            }
1819    
1820                            if (pMB->dquant != 0)
1821                                    MBSetDquant(pMB, x, y, pParam);
1822    
1823    
1824                            if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1825                                       pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1826                                       pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1827                                    data->sStat->mblks++;
1828                            }  else {
1829                                    data->sStat->ublks++;
1830                            }
1831    
1832                            start_timer();
1833    
1834                            /* Finished processing the MB, now check if to CODE or SKIP */
1835    
1836                            skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER);
1837    
1838                            if (current->coding_type == S_VOP)
1839                                    skip_possible &= (pMB->mcsel == 1);
1840                            else { /* PVOP */
1841                                    const VECTOR * const mv = (pParam->vol_flags & XVID_VOL_QUARTERPEL) ?
1842                                                                                    pMB->qmvs : pMB->mvs;
1843                                    skip_possible &= ((mv->x|mv->y) == 0);
1844                            }
1845    
1846                            if ((pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
1847                                    /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */
1848                                    int bSkip = 1;
1849    
1850                                    if (current->coding_type == P_VOP) {    /* special rule for P-VOP's SKIP */
1851                                            for (k = pEnc->bframenum_head; k < pEnc->bframenum_tail; k++) {
1852                                                    int iSAD;
1853                                                    iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x,
1854                                                                                    pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x,
1855                                                                                    pParam->edged_width, BFRAME_SKIP_THRESHHOLD * pMB->quant);
1856                                                    if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant || ((bound > 1) &&
1857                                                            ((y*mb_width+x == bound) || (y*mb_width+x == bound+1)))) { /* Some third-party decoders have problems with coloc skip MB before or after
1858                                                                                                                                                                               resync marker in BVOP. We avoid any ambiguity and force no skip at slice boundary */
1859                                                            bSkip = 0; /* could not SKIP */
1860                                                            if (pParam->vol_flags & XVID_VOL_QUARTERPEL) {
1861                                                                    VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, bound, x, y, 0);
1862                                                                    pMB->pmvs[0].x = - predMV.x;
1863                                                                    pMB->pmvs[0].y = - predMV.y;
1864                                                            } else {
1865                                                                    VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, bound, x, y, 0);
1866                                                                    pMB->pmvs[0].x = - predMV.x;
1867                                                                    pMB->pmvs[0].y = - predMV.y;
1868                                                            }
1869                                                            pMB->mode = MODE_INTER;
1870                                                            pMB->cbp = 0;
1871                                                            break;
1872                                                    }
1873                                            }
1874                                    }
1875    
1876                                    if (bSkip) {
1877                                            /* do SKIP */
1878                                            pMB->mode = MODE_NOT_CODED;
1879                                            MBSkip(bs);
1880                                            stop_coding_timer();
1881                                            continue;       /* next MB */
1882                                    }
1883                            }
1884    
1885                            /* ordinary case: normal coded INTER/INTER4V block */
1886                            MBCoding(current, pMB, qcoeff, bs, data->sStat);
1887                            stop_coding_timer();
1888                    }
1889            }
1890    
1891            BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
1892            emms();
1893    }
1894    
1895    /* FrameCodeP also handles S(GMC)-VOPs */
1896    static int
1897    FrameCodeP(Encoder * pEnc, Bitstream * bs)
1898    {
1899            int bits = BitstreamPos(bs);
1900    
1901          FRAMEINFO *const current = pEnc->current;          FRAMEINFO *const current = pEnc->current;
1902          FRAMEINFO *const reference = pEnc->reference;          FRAMEINFO *const reference = pEnc->reference;
1903          MBParam * const pParam = &pEnc->mbParam;          MBParam * const pParam = &pEnc->mbParam;
# Line 1578  Line 1905 
1905          int mb_height = pParam->mb_height;          int mb_height = pParam->mb_height;
1906          int coded = 1;          int coded = 1;
1907    
1908            int k = 0, bound = 0, num_slices = pEnc->num_slices;
1909            int num_threads = MAX(1, MIN(pEnc->num_threads, num_slices));
1910            void * status = NULL;
1911            int slices_per_thread = (num_slices*1024 / num_threads);
1912            int threads_per_slice = (pEnc->num_threads*1024 / num_threads);
1913    
1914          IMAGE *pRef = &reference->image;          IMAGE *pRef = &reference->image;
1915    
1916          if (!reference->is_edged) {          if (!reference->is_edged) {
# Line 1611  Line 1944 
1944    
1945          current->coding_type = P_VOP;          current->coding_type = P_VOP;
1946    
1947            if (current->vop_flags & XVID_VOP_RD_PSNRHVSM) {
1948                    image_block_variance(&current->image, pParam->edged_width, current->mbs,
1949                                         pParam->mb_width, pParam->mb_height);
1950            }
1951    
1952          call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);          call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1953    
1954          SetMacroblockQuants(&pEnc->mbParam, current);          SetMacroblockQuants(&pEnc->mbParam, current);
# Line 1619  Line 1957 
1957          if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */          if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */
1958          {       int gmcval;          {       int gmcval;
1959                  current->warp = GlobalMotionEst( current->mbs, pParam, current, reference,                  current->warp = GlobalMotionEst( current->mbs, pParam, current, reference,
1960                                                                   &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);                                                                   &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, num_slices);
1961    
1962                  if (current->motion_flags & XVID_ME_GME_REFINE) {                  if (current->motion_flags & XVID_ME_GME_REFINE) {
1963                          gmcval = GlobalMotionEstRefine(&current->warp,                          gmcval = GlobalMotionEstRefine(&current->warp,
# Line 1666  Line 2004 
2004                  }                  }
2005          }          }
2006    
2007          MotionEstimation(&pEnc->mbParam, current, reference,          if (pEnc->num_threads > 0) {
                                          &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,  
                                          &pEnc->vGMC, 256*4096);  
   
2008    
2009          stop_motion_timer();                  /* multithreaded motion estimation - dispatch threads */
2010                    while (k < pEnc->num_threads) {
2011                            int i, add_s = (slices_per_thread + 512) >> 10;
2012                            int add_t = (threads_per_slice + 512) >> 10;
2013    
2014          set_timecodes(current,reference,pParam->fbase);                          int start_y = (bound * mb_height + (num_slices-1)) / num_slices;
2015                            int stop_y = ((bound+add_s) * mb_height + (num_slices-1)) / num_slices;
2016                            int rows_per_thread = (stop_y - start_y + add_t - 1) / add_t;
2017    
2018          BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant);                          slices_per_thread += ((num_slices*1024 / num_threads) - add_s*1024);
2019                            threads_per_slice += ((pEnc->num_threads*1024 / num_threads) - add_t*1024);
2020    
2021          for (y = 0; y < mb_height; y++) {                          for (i = 0; i < add_t; i++) {
2022                  for (x = 0; x < mb_width; x++) {                                  memset(pEnc->smpData[k+i].complete_count_self, 0, rows_per_thread * sizeof(int));
                         MACROBLOCK *pMB = &current->mbs[x + y * pParam->mb_width];  
                         int skip_possible;  
2023    
2024                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {                                  pEnc->smpData[k+i].pEnc = (void *) pEnc;
2025                                  CodeIntraMB(pEnc, pMB);                                  pEnc->smpData[k+i].y_row = i;
2026                                  MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,                                  pEnc->smpData[k+i].y_step = add_t;
2027                                                                    dct_codes, qcoeff);                                  pEnc->smpData[k+i].stop_y = stop_y;
2028                                    pEnc->smpData[k+i].start_y = start_y;
2029    
2030                                  start_timer();                                  /* todo: sort out temp space once and for all */
2031                                  MBPrediction(current, x, y, pParam->mb_width, qcoeff);                                  pEnc->smpData[k+i].RefQ = (((k+i)&1) ? pEnc->vInterV.u : pEnc->vInterV.v) +
2032                                  stop_prediction_timer();                                                                                          16*((k+i)>>1)*pParam->edged_width;
2033                            }
2034    
2035                                  current->sStat.kblks++;                          pEnc->smpData[k].complete_count_above =
2036                                    pEnc->smpData[k+add_t-1].complete_count_self - 1;
2037    
2038                                  MBCoding(current, pMB, qcoeff, bs, &current->sStat);                          bound += add_s;
2039                                  stop_coding_timer();                          k += add_t;
                                 continue;  
2040                          }                          }
2041    
2042                          start_timer();                  for (k = 1; k < pEnc->num_threads; k++) {
2043                          MBMotionCompensation(pMB, x, y, &reference->image,                          pthread_create(&pEnc->smpData[k].handle, NULL,
2044                                                                   &pEnc->vInterH, &pEnc->vInterV,                                  (void*)MotionEstimateSMP, (void*)&pEnc->smpData[k]);
2045                                                                   &pEnc->vInterHV, &pEnc->vGMC,                  }
                                                                  &current->image,  
                                                                  dct_codes, pParam->width,  
                                                                  pParam->height,  
                                                                  pParam->edged_width,  
                                                                  (current->vol_flags & XVID_VOL_QUARTERPEL),  
                                                                  current->rounding_type);  
2046    
2047                          stop_comp_timer();                  MotionEstimateSMP(&pEnc->smpData[0]);
2048    
2049                          pMB->field_pred = 0;                  for (k = 1; k < pEnc->num_threads; k++) {
2050                            pthread_join(pEnc->smpData[k].handle, &status);
2051                    }
2052    
2053                          if (pMB->cbp != 0) {                  current->fcode = 0;
2054                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y,                  for (k = 0; k < pEnc->num_threads; k++) {
2055                                                                            dct_codes, qcoeff);                          current->sStat.iMvSum += pEnc->smpData[k].mvSum;
2056                            current->sStat.iMvCount += pEnc->smpData[k].mvCount;
2057                            if (pEnc->smpData[k].minfcode > current->fcode)
2058                                    current->fcode = pEnc->smpData[k].minfcode;
2059                          }                          }
2060    
2061                          if (pMB->dquant != 0)          } else {
                                 MBSetDquant(pMB, x, y, &pEnc->mbParam);  
2062    
2063                    /* regular ME */
2064    
2065                    MotionEstimation(&pEnc->mbParam, current, reference,
2066                                                     &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
2067                                                     &pEnc->vGMC, 256*4096, num_slices);
2068    
                         if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||  
                                    pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||  
                                    pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {  
                                 current->sStat.mblks++;  
                         }  else {  
                                 current->sStat.ublks++;  
2069                          }                          }
2070    
2071                          start_timer();          stop_motion_timer();
2072    
2073                          /* Finished processing the MB, now check if to CODE or SKIP */          set_timecodes(current,reference,pParam->fbase);
2074    
2075                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant);
2076    
2077                          if (current->coding_type == S_VOP)          /* multithreaded inter coding - dispatch threads */
                                 skip_possible &= (pMB->mcsel == 1);  
                         else { /* PVOP */  
                                 const VECTOR * const mv = (pParam->vol_flags & XVID_VOL_QUARTERPEL) ?  
                                                                                 pMB->qmvs : pMB->mvs;  
                                 skip_possible &= ((mv->x|mv->y) == 0);  
                         }  
2078    
2079                          if ((pMB->mode == MODE_NOT_CODED) || (skip_possible)) {          bound = 0;
2080                                  /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */          slices_per_thread = (num_slices*1024 / num_threads);
                                 int bSkip = 1;  
2081    
2082                                  if (current->coding_type == P_VOP) {    /* special rule for P-VOP's SKIP */          for (k = 0; k < num_threads; k++) {
2083                    int add = ((slices_per_thread + 512) >> 10);
2084    
2085                                          for (k = pEnc->bframenum_head; k < pEnc->bframenum_tail; k++) {                  slices_per_thread += ((num_slices*1024 / num_threads) - add*1024);
                                                 int iSAD;  
                                                 iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x,  
                                                                                 pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x,  
                                                                                 pParam->edged_width, BFRAME_SKIP_THRESHHOLD * pMB->quant);  
                                                 if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) {  
                                                         bSkip = 0; /* could not SKIP */  
                                                         if (pParam->vol_flags & XVID_VOL_QUARTERPEL) {  
                                                                 VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0);  
                                                                 pMB->pmvs[0].x = - predMV.x;  
                                                                 pMB->pmvs[0].y = - predMV.y;  
                                                         } else {  
                                                                 VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0);  
                                                                 pMB->pmvs[0].x = - predMV.x;  
                                                                 pMB->pmvs[0].y = - predMV.y;  
                                                         }  
                                                         pMB->mode = MODE_INTER;  
                                                         pMB->cbp = 0;  
                                                         break;  
                                                 }  
                                         }  
                                 }  
2086    
2087                                  if (bSkip) {                  pEnc->smpData[k].pEnc = (void *) pEnc;
2088                                          /* do SKIP */                  pEnc->smpData[k].stop_y = (((bound+add) * mb_height + (num_slices-1)) / num_slices);
2089                                          pMB->mode = MODE_NOT_CODED;                  pEnc->smpData[k].start_y = ((bound * mb_height + (num_slices-1)) / num_slices);
2090                                          MBSkip(bs);                  pEnc->smpData[k].RefQ = ((k&1) ? pEnc->vInterV.u : pEnc->vInterV.v) + 16*(k>>1)*pParam->edged_width;
2091                                          stop_coding_timer();  
2092                                          continue;       /* next MB */                  bound += add;
2093    
2094                    if (k > 0) {
2095                            pEnc->smpData[k].sStat->iTextBits = pEnc->smpData[k].sStat->kblks =
2096                            pEnc->smpData[k].sStat->mblks = pEnc->smpData[k].sStat->ublks =
2097                            pEnc->smpData[k].sStat->iMVBits = 0;
2098    
2099                            BitstreamReset(pEnc->smpData[k].bs);
2100                                  }                                  }
2101                          }                          }
2102            pEnc->smpData[0].bs = bs;
2103            pEnc->smpData[0].sStat = &current->sStat;
2104    
2105                          /* ordinary case: normal coded INTER/INTER4V block */          /* create threads */
2106                          MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat);          for (k = 1; k < num_threads; k++) {
2107                          stop_coding_timer();                  pthread_create(&pEnc->smpData[k].handle, NULL,
2108                            (void*)SliceCodeP, (void*)&pEnc->smpData[k]);
2109                  }                  }
2110    
2111            SliceCodeP(&pEnc->smpData[0]);
2112    
2113            /* wait until all threads are finished */
2114            for (k = 1; k < num_threads; k++) {
2115                    pthread_join(pEnc->smpData[k].handle, &status);
2116          }          }
2117    
2118          emms();          current->length = BitstreamLength(bs) - (bits/8);
2119    
2120            /* reassemble the pieces together */
2121            SerializeBitstreams(pEnc, pEnc->current, bs, num_threads);
2122    
2123          updateFcode(&current->sStat, pEnc);          updateFcode(&current->sStat, pEnc);
2124    
2125          /* frame drop code */          /* frame drop code */
2126  #if 0  #if 0
2127          DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks);          DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks);
2128  #endif  #endif
2129          if (current->sStat.kblks + current->sStat.mblks <=  
2130            if (current->sStat.kblks + current->sStat.mblks <
2131                  (pParam->frame_drop_ratio * mb_width * mb_height) / 100 &&                  (pParam->frame_drop_ratio * mb_width * mb_height) / 100 &&
2132                  ( (pEnc->bframenum_head >= pEnc->bframenum_tail) || !(pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP)) )                  ( (pEnc->bframenum_head >= pEnc->bframenum_tail) || !(pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP)) )
2133          {          {
# Line 1818  Line 2150 
2150                  memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);                  memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
2151                  coded = 0;                  coded = 0;
2152    
2153                    BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
2154    
2155                    current->length = (BitstreamPos(bs) - bits) / 8;
2156    
2157          } else {          } else {
2158    
2159                  pEnc->current->is_edged = 0; /* not edged */                  pEnc->current->is_edged = 0; /* not edged */
# Line 1846  Line 2182 
2182          }          }
2183          */          */
2184    
2185          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */          return coded;
2186    }
2187    
2188          current->length = (BitstreamPos(bs) - bits) / 8;  static void
2189    SliceCodeB(SMPData *data)
2190    {
2191            Encoder *pEnc = (Encoder *) data->pEnc;
2192            Bitstream *bs = (Bitstream *) data->bs;
2193    
2194          return coded;          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
2195            DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
2196    
2197            int x, y;
2198            FRAMEINFO * const frame = (FRAMEINFO * const) data->current;
2199            MBParam * const pParam = &pEnc->mbParam;
2200            int mb_width = pParam->mb_width;
2201            int mb_height = pParam->mb_height;
2202            IMAGE *f_ref = &pEnc->reference->image;
2203            IMAGE *b_ref = &pEnc->current->image;
2204    
2205            int bound = data->start_y*mb_width;
2206            int num_slices = pEnc->num_slices;
2207    
2208            if (data->start_y > 0) { /* write resync marker */
2209                    write_video_packet_header(bs, pParam, frame, bound+1);
2210            }
2211    
2212            for (y = data->start_y; y < MIN(data->stop_y+1, mb_height); y++) {
2213                    int new_bound = mb_width * ((((y*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices);
2214                    int stop_x = (y == data->stop_y) ? 1 : mb_width;
2215                    int start_x = (y == data->start_y && y > 0) ? 1 : 0;
2216    
2217                    for (x = start_x; x < stop_x; x++) {
2218                            MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
2219    
2220                            /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
2221                            if (mb->mode == MODE_NOT_CODED) {
2222                                    if (pParam->plugin_flags & XVID_REQORIGINAL) {
2223                                            MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image,
2224                                                                                     NULL, 0, 0, pParam->edged_width, 0, 0, data->RefQ);
2225                                    }
2226                                    continue;
2227                            }
2228    
2229                            if (new_bound > bound && x > 0) {
2230                                    bound = new_bound;
2231                                    BitstreamPadAlways(bs);
2232                                    write_video_packet_header(bs, pParam, frame, y*mb_width+x);
2233  }  }
2234    
2235                            mb->quant = frame->quant;
2236    
2237                            if (mb->cbp != 0 || pParam->plugin_flags & XVID_REQORIGINAL) {
2238                                    /* we have to motion-compensate, transfer etc,
2239                                            because there might be blocks to code */
2240    
2241                                    MBMotionCompensationBVOP(pParam, mb, x, y, &frame->image,
2242                                                                                     f_ref, &pEnc->f_refh, &pEnc->f_refv,
2243                                                                                     &pEnc->f_refhv, b_ref, &pEnc->vInterH,
2244                                                                                     &pEnc->vInterV, &pEnc->vInterHV, dct_codes,
2245                                                                                     data->RefQ);
2246    
2247                                    mb->cbp = MBTransQuantInterBVOP(pParam, frame, mb, x, y,  dct_codes, qcoeff);
2248                            }
2249    
2250                            if (mb->mode == MODE_DIRECT_NO4V)
2251                                    mb->mode = MODE_DIRECT;
2252    
2253                            if (mb->mode == MODE_DIRECT && (mb->cbp | mb->pmvs[3].x | mb->pmvs[3].y) == 0)
2254                                    mb->mode = MODE_DIRECT_NONE_MV; /* skipped */
2255                            else
2256                                    if (frame->vop_flags & XVID_VOP_GREYSCALE)
2257                                            /* keep only bits 5-2 -- Chroma blocks will just be skipped by MBCodingBVOP */
2258                                            mb->cbp &= 0x3C;
2259    
2260                            start_timer();
2261                            MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs, data->sStat);
2262                            stop_coding_timer();
2263                    }
2264            }
2265    
2266            BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
2267            emms();
2268    }
2269    
2270  static void  static void
2271  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
# Line 1860  Line 2273 
2273                     Bitstream * bs)                     Bitstream * bs)
2274  {  {
2275          int bits = BitstreamPos(bs);          int bits = BitstreamPos(bs);
2276          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          int k = 0, bound = 0, num_slices = pEnc->num_slices;
2277          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          int num_threads = MAX(1, MIN(pEnc->num_threads, num_slices));
2278          uint32_t x, y;          void * status = NULL;
2279            int slices_per_thread = (num_slices*1024 / num_threads);
2280            int threads_per_slice = (pEnc->num_threads*1024 / num_threads);
2281    
2282          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
2283          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
2284    
2285            MBParam * const pParam = &pEnc->mbParam;
2286            int mb_height = pParam->mb_height;
2287    
2288          #ifdef BFRAMES_DEC_DEBUG          #ifdef BFRAMES_DEC_DEBUG
2289          FILE *fp;          FILE *fp;
2290          static char first=0;          static char first=0;
# Line 1884  Line 2302 
2302                  image_setedges(f_ref, pEnc->mbParam.edged_width,                  image_setedges(f_ref, pEnc->mbParam.edged_width,
2303                                             pEnc->mbParam.edged_height, pEnc->mbParam.width,                                             pEnc->mbParam.edged_height, pEnc->mbParam.width,
2304                                             pEnc->mbParam.height, 0);                                             pEnc->mbParam.height, 0);
2305                  pEnc->current->is_edged = 1;                  pEnc->reference->is_edged = 1;
2306          }          }
2307    
2308          if (pEnc->reference->is_interpolated != 0) {          if (pEnc->reference->is_interpolated != 0) {
# Line 1914  Line 2332 
2332          }          }
2333    
2334          frame->coding_type = B_VOP;          frame->coding_type = B_VOP;
2335    
2336            if ((frame->vop_flags & XVID_VOP_RD_PSNRHVSM) && (frame->vop_flags & XVID_VOP_RD_BVOP)) {
2337                    image_block_variance(&frame->image, pEnc->mbParam.edged_width, frame->mbs,
2338                                         pEnc->mbParam.mb_width, pEnc->mbParam.mb_height);
2339            }
2340    
2341          call_plugins(pEnc, frame, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);          call_plugins(pEnc, frame, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
2342    
2343            frame->fcode = frame->bcode = pEnc->current->fcode;
2344    
2345          start_timer();          start_timer();
2346    
2347            if (pEnc->num_threads > 0) {
2348    
2349                    /* multithreaded motion estimation - dispatch threads */
2350                    while (k < pEnc->num_threads) {
2351                            int i, add_s = (slices_per_thread + 512) >> 10;
2352                            int add_t = (threads_per_slice + 512) >> 10;
2353    
2354                            int start_y = (bound * mb_height + (num_slices-1)) / num_slices;
2355                            int stop_y = ((bound+add_s) * mb_height + (num_slices-1)) / num_slices;
2356                            int rows_per_thread = (stop_y - start_y + add_t - 1) / add_t;
2357    
2358                            slices_per_thread += ((num_slices*1024 / num_threads) - add_s*1024);
2359                            threads_per_slice += ((pEnc->num_threads*1024 / num_threads) - add_t*1024);
2360    
2361                            for (i = 0; i < add_t; i++) {
2362                                    memset(pEnc->smpData[k+i].complete_count_self, 0, rows_per_thread * sizeof(int));
2363    
2364                                    pEnc->smpData[k+i].pEnc = (void *) pEnc;
2365                                    pEnc->smpData[k+i].current = frame;
2366    
2367                                    pEnc->smpData[k+i].y_row = i;
2368                                    pEnc->smpData[k+i].y_step = add_t;
2369                                    pEnc->smpData[k+i].stop_y = stop_y;
2370                                    pEnc->smpData[k+i].start_y = start_y;
2371    
2372                                    /* todo: sort out temp space once and for all */
2373                                    pEnc->smpData[k+i].RefQ = (((k+i)&1) ? pEnc->vInterV.u : pEnc->vInterV.v) +
2374                                                                                            16*((k+i)>>1)*pParam->edged_width;
2375                            }
2376    
2377                            pEnc->smpData[k].complete_count_above =
2378                                    pEnc->smpData[k+add_t-1].complete_count_self - 1;
2379    
2380                            bound += add_s;
2381                            k += add_t;
2382                    }
2383    
2384                    for (k = 1; k < pEnc->num_threads; k++) {
2385                            pthread_create(&pEnc->smpData[k].handle, NULL,
2386                                    (void*)SMPMotionEstimationBVOP, (void*)&pEnc->smpData[k]);
2387                    }
2388    
2389                    SMPMotionEstimationBVOP(&pEnc->smpData[0]);
2390    
2391                    for (k = 1; k < pEnc->num_threads; k++) {
2392                            pthread_join(pEnc->smpData[k].handle, &status);
2393                    }
2394    
2395                    frame->fcode = frame->bcode = 0;
2396                    for (k = 0; k < pEnc->num_threads; k++) {
2397                            if (pEnc->smpData[k].minfcode > frame->fcode)
2398                                    frame->fcode = pEnc->smpData[k].minfcode;
2399                            if (pEnc->smpData[k].minbcode > frame->bcode)
2400                                    frame->bcode = pEnc->smpData[k].minbcode;
2401                    }
2402            } else {
2403    
2404          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
2405                                                   ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */                                                   ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */
2406                                                   ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */                                                   ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */
2407                                                   pEnc->reference->mbs, f_ref,                                                   pEnc->reference->mbs, f_ref,
2408                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
2409                                                   pEnc->current, b_ref, &pEnc->vInterH,                                                   pEnc->current, b_ref, &pEnc->vInterH,
2410                                                   &pEnc->vInterV, &pEnc->vInterHV);                                                           &pEnc->vInterV, &pEnc->vInterHV,
2411                                                             pEnc->num_slices);
2412            }
2413          stop_motion_timer();          stop_motion_timer();
2414    
2415          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
2416          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant);
2417    
2418            /* reset stats */
2419          frame->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
2420          frame->sStat.iMVBits = 0;          frame->sStat.iMVBits = 0;
2421          frame->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
# Line 1937  Line 2424 
2424          frame->sStat.mblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;          frame->sStat.mblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
2425          frame->sStat.kblks = frame->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.ublks = 0;
2426    
2427          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          /* multithreaded inter coding - dispatch threads */
2428                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {          bound = 0;
2429                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];          slices_per_thread = (num_slices*1024 / num_threads);
2430    
2431                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */          for (k = 0; k < num_threads; k++) {
2432                          if (mb->mode == MODE_NOT_CODED) {                  int add = ((slices_per_thread + 512) >> 10);
                                 if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {  
                                         MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image,  
                                                                                         NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0);  
                                 }  
                                 continue;  
                         }  
2433    
2434                          mb->quant = frame->quant;                  slices_per_thread += ((num_slices*1024 / num_threads) - add*1024);
2435    
2436                          if (mb->cbp != 0 || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {                  pEnc->smpData[k].pEnc = (void *) pEnc;
2437                                  /* we have to motion-compensate, transfer etc,                  pEnc->smpData[k].current = frame;
2438                                          because there might be blocks to code */                  pEnc->smpData[k].stop_y = (((bound+add) * mb_height + (num_slices-1)) / num_slices);
2439                    pEnc->smpData[k].start_y = ((bound * mb_height + (num_slices-1)) / num_slices);
2440                    bound += add;
2441    
2442                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                  /* todo: sort out temp space once and for all */
2443                                                                                   f_ref, &pEnc->f_refh, &pEnc->f_refv,                  pEnc->smpData[k].RefQ = ((k&1) ? pEnc->vInterV.u : pEnc->vInterV.v) + 16*(k>>1)*pParam->edged_width;
                                                                                  &pEnc->f_refhv, b_ref, &pEnc->vInterH,  
                                                                                  &pEnc->vInterV, &pEnc->vInterHV,  
                                                                                  dct_codes);  
2444    
2445                                  mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);                  if (k > 0) {
2446                            BitstreamReset(pEnc->smpData[k].bs);
2447                            pEnc->smpData[k].sStat->iTextBits = pEnc->smpData[k].sStat->kblks =
2448                            pEnc->smpData[k].sStat->mblks = pEnc->smpData[k].sStat->ublks = pEnc->smpData[k].sStat->iMVBits = 0;
2449                    }
2450                          }                          }
2451    
2452                          if (mb->mode == MODE_DIRECT_NO4V)          for (k = 1; k < num_threads; k++) {
2453                                  mb->mode = MODE_DIRECT;                  pthread_create(&pEnc->smpData[k].handle, NULL,
2454                            (void*)SliceCodeB, (void*)&pEnc->smpData[k]);
2455            }
2456    
2457                          if (mb->mode == MODE_DIRECT && (mb->cbp | mb->pmvs[3].x | mb->pmvs[3].y) == 0)          pEnc->smpData[0].bs = bs;
2458                                  mb->mode = MODE_DIRECT_NONE_MV; /* skipped */          pEnc->smpData[0].sStat = &frame->sStat;
2459                          else          SliceCodeB(&pEnc->smpData[0]);
                                 if (frame->vop_flags & XVID_VOP_GREYSCALE)  
                                         /* keep only bits 5-2 -- Chroma blocks will just be skipped by MBCodingBVOP */  
                                         mb->cbp &= 0x3C;  
2460    
2461                          start_timer();          for (k = 1; k < num_threads; k++) {
2462                          MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs,                  pthread_join(pEnc->smpData[k].handle, &status);
                                                  &frame->sStat);  
                         stop_coding_timer();  
                 }  
2463          }          }
2464    
2465          emms();          frame->length = BitstreamLength(bs) - (bits/8);
2466    
2467          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */          /* reassemble the pieces together */
2468          frame->length = (BitstreamPos(bs) - bits) / 8;          SerializeBitstreams(pEnc, frame, bs, num_threads);
2469    
2470  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
2471          if (!first){          if (!first){

Legend:
Removed from v.1.124  
changed lines
  Added in v.1.137

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