[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.130, Thu Feb 8 13:10:24 2007 UTC revision 1.135, Fri Dec 24 13:20:07 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 435  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 444  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 */          /* multithreaded stuff */
451          if (create->num_threads > 0) {          if (create->num_threads > 0) {
452                  int t = create->num_threads;                  int t = MIN(create->num_threads, (int) (pEnc->mbParam.mb_height>>1)); /* at least two rows per thread */
453                  int rows_per_thread = (pEnc->mbParam.mb_height+t-1)/t;                  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;                  pEnc->num_threads = t;
457                  pEnc->motionData = xvid_malloc(t*sizeof(SMPmotionData), CACHE_LINE);                  pEnc->smpData = xvid_malloc(t*sizeof(SMPData), CACHE_LINE);
458                  if (!pEnc->motionData)                  if (!pEnc->smpData)
459                          goto xvid_err_nosmp;                          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++) {                  for (n = 0; n < t; n++) {
466                          pEnc->motionData[n].complete_count_self =                          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);                                  xvid_malloc(rows_per_thread * sizeof(int), CACHE_LINE);
470    
471                          if (!pEnc->motionData[n].complete_count_self)                          if (!pEnc->smpData[n].complete_count_self)
472                                  goto xvid_err_nosmp;                                  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)                          if (n != 0)
488                                  pEnc->motionData[n].complete_count_above =                                  pEnc->smpData[n].complete_count_above =
489                                          pEnc->motionData[n-1].complete_count_self;                                          pEnc->smpData[n-1].complete_count_self;
490                  }                  }
491                  pEnc->motionData[0].complete_count_above =                  pEnc->smpData[0].complete_count_above =
492                          pEnc->motionData[t-1].complete_count_self - 1;                          pEnc->smpData[t-1].complete_count_self - 1;
493    
494          } else {          } else {
495    xvid_err_nosmp:    xvid_err_nosmp:
496                  /* no SMP */                  /* 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;                  create->num_threads = 0;
                 pEnc->motionData = NULL;  
508          }          }
509    
510          create->handle = (void *) pEnc;          create->handle = (void *) pEnc;
# Line 687  Line 720 
720                  xvid_free(pEnc->zones);                  xvid_free(pEnc->zones);
721    
722          if (pEnc->num_threads > 0) {          if (pEnc->num_threads > 0) {
723                  for (i = 0; i < pEnc->num_threads; i++)                  for (i = 1; i < MAX(1, MIN(pEnc->num_threads, pEnc->num_slices)); i++) {
724                          xvid_free(pEnc->motionData[i].complete_count_self);                          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                  xvid_free(pEnc->motionData);                  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    
# Line 997  Line 1035 
1035          return;          return;
1036  }  }
1037    
   
1038  /*****************************************************************************  /*****************************************************************************
1039   * IPB frame encoder entry point   * IPB frame encoder entry point
1040   *   *
# Line 1492  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 1509  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 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_width = pEnc->mbParam.mb_width;          int mb_width = pEnc->mbParam.mb_width;
1649          int mb_height = pEnc->mbParam.mb_height;          int mb_height = pEnc->mbParam.mb_height;
1650            void * status = NULL;
1651          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;  
1652    
1653          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1654          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
# Line 1532  Line 1658 
1658    
1659          SetMacroblockQuants(&pEnc->mbParam, pEnc->current);          SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1660    
1661          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current, num_slices);
1662    
1663          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1664    
# Line 1541  Line 1667 
1667          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant);
1668    
1669          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;  
1670    
1671          for (y = 0; y < mb_height; y++)          /* multithreaded intra coding - dispatch threads */
1672                  for (x = 0; x < mb_width; x++) {          for (k = 0; k < num_threads; k++) {
1673                          MACROBLOCK *pMB =                  int add = ((slices_per_thread + 512) >> 10);
                                 &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
1674    
1675                          CodeIntraMB(pEnc, pMB);                  slices_per_thread += ((num_slices*1024 / num_threads) - add*1024);
1676    
1677                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,                  pEnc->smpData[k].pEnc = (void *) pEnc;
1678                                                            dct_codes, qcoeff);                  pEnc->smpData[k].stop_y = (((bound+add) * mb_height + (num_slices-1)) / num_slices);
1679                    pEnc->smpData[k].start_y = ((bound * mb_height + (num_slices-1)) / num_slices);
1680    
1681                          start_timer();                  bound += add;
                         MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);  
                         stop_prediction_timer();  
1682    
1683                          start_timer();                  if (k > 0) {
1684                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                          BitstreamReset(pEnc->smpData[k].bs);
1685                          stop_coding_timer();                          pEnc->smpData[k].sStat->iTextBits = 0;
1686                    }
1687                  }                  }
1688            pEnc->smpData[0].bs = bs;
1689            pEnc->smpData[0].sStat = &pEnc->current->sStat;
1690    
1691          emms();          /* create threads */
1692            for (k = 1; k < num_threads; k++) {
1693                    pthread_create(&pEnc->smpData[k].handle, NULL,
1694                                   (void*)SliceCodeI, (void*)&pEnc->smpData[k]);
1695            }
1696    
1697          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */          SliceCodeI(&pEnc->smpData[0]);
1698    
1699          pEnc->current->length = (BitstreamPos(bs) - bits) / 8;          /* wait until all threads are finished */
1700            for (k = 1; k < num_threads; k++) {
1701                    pthread_join(pEnc->smpData[k].handle, &status);
1702            }
1703    
1704            pEnc->current->length = BitstreamLength(bs) - (bits/8);
1705    
1706            /* reassemble the pieces together */
1707            SerializeBitstreams(pEnc, pEnc->current, bs, num_threads);
1708    
1709            pEnc->current->sStat.iMVBits = 0;
1710            pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1711            pEnc->current->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1712    
1713          pEnc->fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
1714          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
# Line 1605  Line 1745 
1745    
1746  #define BFRAME_SKIP_THRESHHOLD 30  #define BFRAME_SKIP_THRESHHOLD 30
1747    
1748  /* FrameCodeP also handles S(GMC)-VOPs */  static void
1749  static int  SliceCodeP(SMPData *data)
 FrameCodeP(Encoder * pEnc,  
                    Bitstream * bs)  
1750  {  {
1751          int bits = BitstreamPos(bs);          Encoder *pEnc = (Encoder *) data->pEnc;
1752            Bitstream *bs = (Bitstream *) data->bs;
1753    
1754            int x, y, k;
1755            FRAMEINFO *const current = pEnc->current;
1756            FRAMEINFO *const reference = pEnc->reference;
1757            MBParam * const pParam = &pEnc->mbParam;
1758            IMAGE *pRef = &reference->image;
1759            int mb_width = pParam->mb_width;
1760            int mb_height = pParam->mb_height;
1761    
1762          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1763          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1764    
1765          int x, y, k;          int bound = 0, num_slices = pEnc->num_slices;
1766    
1767            if (data->start_y > 0) { /* write resync marker */
1768                    bound = data->start_y*mb_width;
1769                    write_video_packet_header(bs, pParam, current, bound);
1770            }
1771    
1772            for (y = data->start_y; y < data->stop_y; y++) {
1773                    int new_bound = mb_width * ((((y*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices);
1774    
1775                    if (new_bound > bound) {
1776                            bound = new_bound;
1777                            BitstreamPadAlways(bs);
1778                            write_video_packet_header(bs, pParam, current, bound);
1779                    }
1780    
1781                    for (x = 0; x < mb_width; x++) {
1782                            MACROBLOCK *pMB = &current->mbs[x + y * pParam->mb_width];
1783                            int skip_possible;
1784    
1785                            if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
1786                                    CodeIntraMB(pMB);
1787                                    MBTransQuantIntra(pParam, current, pMB, x, y,
1788                                                                      dct_codes, qcoeff);
1789    
1790                                    start_timer();
1791                                    MBPrediction(current, x, y, pParam->mb_width, qcoeff, bound);
1792                                    stop_prediction_timer();
1793    
1794                                    data->sStat->kblks++;
1795    
1796                                    MBCoding(current, pMB, qcoeff, bs, data->sStat);
1797                                    stop_coding_timer();
1798                                    continue;
1799                            }
1800    
1801                            start_timer();
1802                            MBMotionCompensation(pMB, x, y, &reference->image,
1803                                                                     &pEnc->vInterH, &pEnc->vInterV,
1804                                                                     &pEnc->vInterHV, &pEnc->vGMC,
1805                                                                     &current->image,
1806                                                                     dct_codes, pParam->width,
1807                                                                     pParam->height,
1808                                                                     pParam->edged_width,
1809                                                                     (current->vol_flags & XVID_VOL_QUARTERPEL),
1810                                                                     current->rounding_type,
1811                                                                     data->RefQ);
1812    
1813                            stop_comp_timer();
1814    
1815                            pMB->field_pred = 0;
1816    
1817                            if (pMB->cbp != 0) {
1818                                    pMB->cbp = MBTransQuantInter(pParam, current, pMB, x, y,
1819                                                                 dct_codes, qcoeff);
1820                            }
1821    
1822                            if (pMB->dquant != 0)
1823                                    MBSetDquant(pMB, x, y, pParam);
1824    
1825    
1826                            if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1827                                       pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1828                                       pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1829                                    data->sStat->mblks++;
1830                            }  else {
1831                                    data->sStat->ublks++;
1832                            }
1833    
1834                            start_timer();
1835    
1836                            /* Finished processing the MB, now check if to CODE or SKIP */
1837    
1838                            skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER);
1839    
1840                            if (current->coding_type == S_VOP)
1841                                    skip_possible &= (pMB->mcsel == 1);
1842                            else { /* PVOP */
1843                                    const VECTOR * const mv = (pParam->vol_flags & XVID_VOL_QUARTERPEL) ?
1844                                                                                    pMB->qmvs : pMB->mvs;
1845                                    skip_possible &= ((mv->x|mv->y) == 0);
1846                            }
1847    
1848                            if ((pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
1849                                    /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */
1850                                    int bSkip = 1;
1851    
1852                                    if (current->coding_type == P_VOP) {    /* special rule for P-VOP's SKIP */
1853                                            for (k = pEnc->bframenum_head; k < pEnc->bframenum_tail; k++) {
1854                                                    int iSAD;
1855                                                    iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x,
1856                                                                                    pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x,
1857                                                                                    pParam->edged_width, BFRAME_SKIP_THRESHHOLD * pMB->quant);
1858                                                    if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant || ((bound > 1) &&
1859                                                            ((y*mb_width+x == bound) || (y*mb_width+x == bound+1)))) { /* Some third-party decoders have problems with coloc skip MB before or after
1860                                                                                                                                                                               resync marker in BVOP. We avoid any ambiguity and force no skip at slice boundary */
1861                                                            bSkip = 0; /* could not SKIP */
1862                                                            if (pParam->vol_flags & XVID_VOL_QUARTERPEL) {
1863                                                                    VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, bound, x, y, 0);
1864                                                                    pMB->pmvs[0].x = - predMV.x;
1865                                                                    pMB->pmvs[0].y = - predMV.y;
1866                                                            } else {
1867                                                                    VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, bound, x, y, 0);
1868                                                                    pMB->pmvs[0].x = - predMV.x;
1869                                                                    pMB->pmvs[0].y = - predMV.y;
1870                                                            }
1871                                                            pMB->mode = MODE_INTER;
1872                                                            pMB->cbp = 0;
1873                                                            break;
1874                                                    }
1875                                            }
1876                                    }
1877    
1878                                    if (bSkip) {
1879                                            /* do SKIP */
1880                                            pMB->mode = MODE_NOT_CODED;
1881                                            MBSkip(bs);
1882                                            stop_coding_timer();
1883                                            continue;       /* next MB */
1884                                    }
1885                            }
1886    
1887                            /* ordinary case: normal coded INTER/INTER4V block */
1888                            MBCoding(current, pMB, qcoeff, bs, data->sStat);
1889                            stop_coding_timer();
1890                    }
1891            }
1892    
1893            BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
1894            emms();
1895    }
1896    
1897    /* FrameCodeP also handles S(GMC)-VOPs */
1898    static int
1899    FrameCodeP(Encoder * pEnc, Bitstream * bs)
1900    {
1901            int bits = BitstreamPos(bs);
1902    
1903          FRAMEINFO *const current = pEnc->current;          FRAMEINFO *const current = pEnc->current;
1904          FRAMEINFO *const reference = pEnc->reference;          FRAMEINFO *const reference = pEnc->reference;
1905          MBParam * const pParam = &pEnc->mbParam;          MBParam * const pParam = &pEnc->mbParam;
# Line 1623  Line 1907 
1907          int mb_height = pParam->mb_height;          int mb_height = pParam->mb_height;
1908          int coded = 1;          int coded = 1;
1909    
1910            int k = 0, bound = 0, num_slices = pEnc->num_slices;
1911            int num_threads = MAX(1, MIN(pEnc->num_threads, num_slices));
1912            void * status = NULL;
1913            int slices_per_thread = (num_slices*1024 / num_threads);
1914            int threads_per_slice = (pEnc->num_threads*1024 / num_threads);
1915    
1916          IMAGE *pRef = &reference->image;          IMAGE *pRef = &reference->image;
1917    
1918          if (!reference->is_edged) {          if (!reference->is_edged) {
# Line 1656  Line 1946 
1946    
1947          current->coding_type = P_VOP;          current->coding_type = P_VOP;
1948    
1949            if (current->vop_flags & XVID_VOP_RD_PSNRHVSM) {
1950                    image_block_variance(&current->image, pParam->edged_width, current->mbs,
1951                                         pParam->mb_width, pParam->mb_height);
1952            }
1953    
1954          call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);          call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1955    
1956          SetMacroblockQuants(&pEnc->mbParam, current);          SetMacroblockQuants(&pEnc->mbParam, current);
# Line 1664  Line 1959 
1959          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 */
1960          {       int gmcval;          {       int gmcval;
1961                  current->warp = GlobalMotionEst( current->mbs, pParam, current, reference,                  current->warp = GlobalMotionEst( current->mbs, pParam, current, reference,
1962                                                                   &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);                                                                   &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, num_slices);
1963    
1964                  if (current->motion_flags & XVID_ME_GME_REFINE) {                  if (current->motion_flags & XVID_ME_GME_REFINE) {
1965                          gmcval = GlobalMotionEstRefine(&current->warp,                          gmcval = GlobalMotionEstRefine(&current->warp,
# Line 1711  Line 2006 
2006                  }                  }
2007          }          }
2008    
   
2009          if (pEnc->num_threads > 0) {          if (pEnc->num_threads > 0) {
                 /* multithreaded motion estimation - dispatch threads */  
2010    
2011                  void * status;                  /* multithreaded motion estimation - dispatch threads */
2012                  int rows_per_thread = (pParam->mb_height + pEnc->num_threads - 1)/pEnc->num_threads;                  while (k < pEnc->num_threads) {
2013                            int i, add_s = (slices_per_thread + 512) >> 10;
2014                            int add_t = (threads_per_slice + 512) >> 10;
2015    
2016                            int start_y = (bound * mb_height + (num_slices-1)) / num_slices;
2017                            int stop_y = ((bound+add_s) * mb_height + (num_slices-1)) / num_slices;
2018                            int rows_per_thread = (stop_y - start_y + add_t - 1) / add_t;
2019    
2020                            slices_per_thread += ((num_slices*1024 / num_threads) - add_s*1024);
2021                            threads_per_slice += ((pEnc->num_threads*1024 / num_threads) - add_t*1024);
2022    
2023                            for (i = 0; i < add_t; i++) {
2024                                    memset(pEnc->smpData[k+i].complete_count_self, 0, rows_per_thread * sizeof(int));
2025    
2026                                    pEnc->smpData[k+i].pEnc = (void *) pEnc;
2027                                    pEnc->smpData[k+i].y_row = i;
2028                                    pEnc->smpData[k+i].y_step = add_t;
2029                                    pEnc->smpData[k+i].stop_y = stop_y;
2030                                    pEnc->smpData[k+i].start_y = start_y;
2031    
                 for (k = 0; k < pEnc->num_threads; k++) {  
                         memset(pEnc->motionData[k].complete_count_self, 0, rows_per_thread * sizeof(int));  
                         pEnc->motionData[k].pParam = &pEnc->mbParam;  
                         pEnc->motionData[k].current = current;  
                         pEnc->motionData[k].reference = reference;  
                         pEnc->motionData[k].pRefH = &pEnc->vInterH;  
                         pEnc->motionData[k].pRefV = &pEnc->vInterV;  
                         pEnc->motionData[k].pRefHV = &pEnc->vInterHV;  
                         pEnc->motionData[k].pGMC = &pEnc->vGMC;  
                         pEnc->motionData[k].y_step = pEnc->num_threads;  
                         pEnc->motionData[k].start_y = k;  
2032                          /* todo: sort out temp space once and for all */                          /* todo: sort out temp space once and for all */
2033                          pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pParam->edged_width;                                  pEnc->smpData[k+i].RefQ = (((k+i)&1) ? pEnc->vInterV.u : pEnc->vInterV.v) +
2034                                                                                            16*((k+i)>>1)*pParam->edged_width;
2035                            }
2036    
2037                            pEnc->smpData[k].complete_count_above =
2038                                    pEnc->smpData[k+add_t-1].complete_count_self - 1;
2039    
2040                            bound += add_s;
2041                            k += add_t;
2042                  }                  }
2043    
2044                  for (k = 1; k < pEnc->num_threads; k++) {                  for (k = 1; k < pEnc->num_threads; k++) {
2045                          pthread_create(&pEnc->motionData[k].handle, NULL,                          pthread_create(&pEnc->smpData[k].handle, NULL,
2046                                  (void*)MotionEstimateSMP, (void*)&pEnc->motionData[k]);                                  (void*)MotionEstimateSMP, (void*)&pEnc->smpData[k]);
2047                  }                  }
2048    
2049                  MotionEstimateSMP(&pEnc->motionData[0]);                  MotionEstimateSMP(&pEnc->smpData[0]);
2050    
2051                  for (k = 1; k < pEnc->num_threads; k++) {                  for (k = 1; k < pEnc->num_threads; k++) {
2052                          pthread_join(pEnc->motionData[k].handle, &status);                          pthread_join(pEnc->smpData[k].handle, &status);
2053                  }                  }
2054    
2055                  current->fcode = 0;                  current->fcode = 0;
2056                  for (k = 0; k < pEnc->num_threads; k++) {                  for (k = 0; k < pEnc->num_threads; k++) {
2057                          current->sStat.iMvSum += pEnc->motionData[k].mvSum;                          current->sStat.iMvSum += pEnc->smpData[k].mvSum;
2058                          current->sStat.iMvCount += pEnc->motionData[k].mvCount;                          current->sStat.iMvCount += pEnc->smpData[k].mvCount;
2059                          if (pEnc->motionData[k].minfcode > current->fcode)                          if (pEnc->smpData[k].minfcode > current->fcode)
2060                                  current->fcode = pEnc->motionData[k].minfcode;                                  current->fcode = pEnc->smpData[k].minfcode;
2061                  }                  }
2062    
2063          } else {          } else {
2064    
2065                  /* regular ME */                  /* regular ME */
2066    
2067                  MotionEstimation(&pEnc->mbParam, current, reference,                  MotionEstimation(&pEnc->mbParam, current, reference,
2068                                                   &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                                   &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
2069                                                   &pEnc->vGMC, 256*4096);                                                   &pEnc->vGMC, 256*4096, num_slices);
2070    
2071          }          }
2072    
2073          stop_motion_timer();          stop_motion_timer();
# Line 1766  Line 2076 
2076    
2077          BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant);
2078    
2079          for (y = 0; y < mb_height; y++) {          /* multithreaded inter coding - dispatch threads */
                 for (x = 0; x < mb_width; x++) {  
                         MACROBLOCK *pMB = &current->mbs[x + y * pParam->mb_width];  
                         int skip_possible;  
2080    
2081                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {          bound = 0;
2082                                  CodeIntraMB(pEnc, pMB);          slices_per_thread = (num_slices*1024 / num_threads);
                                 MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,  
                                                                   dct_codes, qcoeff);  
2083    
2084                                  start_timer();          for (k = 0; k < num_threads; k++) {
2085                                  MBPrediction(current, x, y, pParam->mb_width, qcoeff);                  int add = ((slices_per_thread + 512) >> 10);
                                 stop_prediction_timer();  
   
                                 current->sStat.kblks++;  
2086    
2087                                  MBCoding(current, pMB, qcoeff, bs, &current->sStat);                  slices_per_thread += ((num_slices*1024 / num_threads) - add*1024);
                                 stop_coding_timer();  
                                 continue;  
                         }  
2088    
2089                          start_timer();                  pEnc->smpData[k].pEnc = (void *) pEnc;
2090                          MBMotionCompensation(pMB, x, y, &reference->image,                  pEnc->smpData[k].stop_y = (((bound+add) * mb_height + (num_slices-1)) / num_slices);
2091                                                                   &pEnc->vInterH, &pEnc->vInterV,                  pEnc->smpData[k].start_y = ((bound * mb_height + (num_slices-1)) / num_slices);
2092                                                                   &pEnc->vInterHV, &pEnc->vGMC,                  pEnc->smpData[k].RefQ = ((k&1) ? pEnc->vInterV.u : pEnc->vInterV.v) + 16*(k>>1)*pParam->edged_width;
                                                                  &current->image,  
                                                                  dct_codes, pParam->width,  
                                                                  pParam->height,  
                                                                  pParam->edged_width,  
                                                                  (current->vol_flags & XVID_VOL_QUARTERPEL),  
                                                                  current->rounding_type);  
2093    
2094                          stop_comp_timer();                  bound += add;
2095    
2096                          pMB->field_pred = 0;                  if (k > 0) {
2097                            pEnc->smpData[k].sStat->iTextBits = pEnc->smpData[k].sStat->kblks =
2098                            pEnc->smpData[k].sStat->mblks = pEnc->smpData[k].sStat->ublks =
2099                            pEnc->smpData[k].sStat->iMVBits = 0;
2100    
2101                          if (pMB->cbp != 0) {                          BitstreamReset(pEnc->smpData[k].bs);
                                 pMB->cbp = MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y,  
                                                                           dct_codes, qcoeff);  
2102                          }                          }
   
                         if (pMB->dquant != 0)  
                                 MBSetDquant(pMB, x, y, &pEnc->mbParam);  
   
   
                         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++;  
2103                          }                          }
2104            pEnc->smpData[0].bs = bs;
2105            pEnc->smpData[0].sStat = &current->sStat;
2106    
2107                          start_timer();          /* create threads */
2108            for (k = 1; k < num_threads; k++) {
2109                          /* Finished processing the MB, now check if to CODE or SKIP */                  pthread_create(&pEnc->smpData[k].handle, NULL,
2110                            (void*)SliceCodeP, (void*)&pEnc->smpData[k]);
                         skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER);  
   
                         if (current->coding_type == S_VOP)  
                                 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);  
2111                          }                          }
2112    
2113                          if ((pMB->mode == MODE_NOT_CODED) || (skip_possible)) {          SliceCodeP(&pEnc->smpData[0]);
                                 /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */  
                                 int bSkip = 1;  
2114    
2115                                  if (current->coding_type == P_VOP) {    /* special rule for P-VOP's SKIP */          /* wait until all threads are finished */
2116            for (k = 1; k < num_threads; k++) {
2117                                          for (k = pEnc->bframenum_head; k < pEnc->bframenum_tail; k++) {                  pthread_join(pEnc->smpData[k].handle, &status);
                                                 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;  
                                                 }  
                                         }  
2118                                  }                                  }
2119    
2120                                  if (bSkip) {          current->length = BitstreamLength(bs) - (bits/8);
                                         /* do SKIP */  
                                         pMB->mode = MODE_NOT_CODED;  
                                         MBSkip(bs);  
                                         stop_coding_timer();  
                                         continue;       /* next MB */  
                                 }  
                         }  
2121    
2122                          /* ordinary case: normal coded INTER/INTER4V block */          /* reassemble the pieces together */
2123                          MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat);          SerializeBitstreams(pEnc, pEnc->current, bs, num_threads);
                         stop_coding_timer();  
                 }  
         }  
2124    
         emms();  
2125          updateFcode(&current->sStat, pEnc);          updateFcode(&current->sStat, pEnc);
2126    
2127          /* frame drop code */          /* frame drop code */
2128  #if 0  #if 0
2129          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);
2130  #endif  #endif
2131          if (current->sStat.kblks + current->sStat.mblks <=  
2132            if (current->sStat.kblks + current->sStat.mblks <
2133                  (pParam->frame_drop_ratio * mb_width * mb_height) / 100 &&                  (pParam->frame_drop_ratio * mb_width * mb_height) / 100 &&
2134                  ( (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)) )
2135          {          {
# Line 1907  Line 2152 
2152                  memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);                  memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
2153                  coded = 0;                  coded = 0;
2154    
2155                    BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
2156    
2157                    current->length = (BitstreamPos(bs) - bits) / 8;
2158    
2159          } else {          } else {
2160    
2161                  pEnc->current->is_edged = 0; /* not edged */                  pEnc->current->is_edged = 0; /* not edged */
# Line 1935  Line 2184 
2184          }          }
2185          */          */
2186    
2187          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */          return coded;
2188    }
2189    
2190          current->length = (BitstreamPos(bs) - bits) / 8;  static void
2191    SliceCodeB(SMPData *data)
2192    {
2193            Encoder *pEnc = (Encoder *) data->pEnc;
2194            Bitstream *bs = (Bitstream *) data->bs;
2195    
2196          return coded;          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
2197            DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
2198    
2199            int x, y;
2200            FRAMEINFO * const frame = (FRAMEINFO * const) data->current;
2201            MBParam * const pParam = &pEnc->mbParam;
2202            int mb_width = pParam->mb_width;
2203            int mb_height = pParam->mb_height;
2204            IMAGE *f_ref = &pEnc->reference->image;
2205            IMAGE *b_ref = &pEnc->current->image;
2206    
2207            int bound = data->start_y*mb_width;
2208            int num_slices = pEnc->num_slices;
2209    
2210            if (data->start_y > 0) { /* write resync marker */
2211                    write_video_packet_header(bs, pParam, frame, bound+1);
2212            }
2213    
2214            for (y = data->start_y; y < MIN(data->stop_y+1, mb_height); y++) {
2215                    int new_bound = mb_width * ((((y*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices);
2216                    int stop_x = (y == data->stop_y) ? 1 : mb_width;
2217                    int start_x = (y == data->start_y && y > 0) ? 1 : 0;
2218    
2219                    for (x = start_x; x < stop_x; x++) {
2220                            MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
2221    
2222                            /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
2223                            if (mb->mode == MODE_NOT_CODED) {
2224                                    if (pParam->plugin_flags & XVID_REQORIGINAL) {
2225                                            MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image,
2226                                                                                     NULL, 0, 0, pParam->edged_width, 0, 0, data->RefQ);
2227                                    }
2228                                    continue;
2229                            }
2230    
2231                            if (new_bound > bound && x > 0) {
2232                                    bound = new_bound;
2233                                    BitstreamPadAlways(bs);
2234                                    write_video_packet_header(bs, pParam, frame, y*mb_width+x);
2235  }  }
2236    
2237                            mb->quant = frame->quant;
2238    
2239                            if (mb->cbp != 0 || pParam->plugin_flags & XVID_REQORIGINAL) {
2240                                    /* we have to motion-compensate, transfer etc,
2241                                            because there might be blocks to code */
2242    
2243                                    MBMotionCompensationBVOP(pParam, mb, x, y, &frame->image,
2244                                                                                     f_ref, &pEnc->f_refh, &pEnc->f_refv,
2245                                                                                     &pEnc->f_refhv, b_ref, &pEnc->vInterH,
2246                                                                                     &pEnc->vInterV, &pEnc->vInterHV, dct_codes,
2247                                                                                     data->RefQ);
2248    
2249                                    mb->cbp = MBTransQuantInterBVOP(pParam, frame, mb, x, y,  dct_codes, qcoeff);
2250                            }
2251    
2252                            if (mb->mode == MODE_DIRECT_NO4V)
2253                                    mb->mode = MODE_DIRECT;
2254    
2255                            if (mb->mode == MODE_DIRECT && (mb->cbp | mb->pmvs[3].x | mb->pmvs[3].y) == 0)
2256                                    mb->mode = MODE_DIRECT_NONE_MV; /* skipped */
2257                            else
2258                                    if (frame->vop_flags & XVID_VOP_GREYSCALE)
2259                                            /* keep only bits 5-2 -- Chroma blocks will just be skipped by MBCodingBVOP */
2260                                            mb->cbp &= 0x3C;
2261    
2262                            start_timer();
2263                            MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs, data->sStat);
2264                            stop_coding_timer();
2265                    }
2266            }
2267    
2268            BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
2269            emms();
2270    }
2271    
2272  static void  static void
2273  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
# Line 1949  Line 2275 
2275                     Bitstream * bs)                     Bitstream * bs)
2276  {  {
2277          int bits = BitstreamPos(bs);          int bits = BitstreamPos(bs);
2278          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          int k = 0, bound = 0, num_slices = pEnc->num_slices;
2279          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          int num_threads = MAX(1, MIN(pEnc->num_threads, num_slices));
2280          uint32_t x, y;          void * status = NULL;
2281            int slices_per_thread = (num_slices*1024 / num_threads);
2282            int threads_per_slice = (pEnc->num_threads*1024 / num_threads);
2283    
2284          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
2285          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
2286    
2287            MBParam * const pParam = &pEnc->mbParam;
2288            int mb_width = pParam->mb_width;
2289            int mb_height = pParam->mb_height;
2290    
2291          #ifdef BFRAMES_DEC_DEBUG          #ifdef BFRAMES_DEC_DEBUG
2292          FILE *fp;          FILE *fp;
2293          static char first=0;          static char first=0;
# Line 2003  Line 2335 
2335          }          }
2336    
2337          frame->coding_type = B_VOP;          frame->coding_type = B_VOP;
2338    
2339            if (pEnc->current->vop_flags & XVID_VOP_RD_PSNRHVSM) {
2340                    image_block_variance(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->current->mbs,
2341                                         pEnc->mbParam.mb_width, pEnc->mbParam.mb_height);
2342            }
2343    
2344          call_plugins(pEnc, frame, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);          call_plugins(pEnc, frame, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
2345    
2346          frame->fcode = frame->bcode = pEnc->current->fcode;          frame->fcode = frame->bcode = pEnc->current->fcode;
2347    
2348          start_timer();          start_timer();
2349    
2350          if (pEnc->num_threads > 0) {          if (pEnc->num_threads > 0) {
2351                  void * status;  
                 int k;  
2352                  /* multithreaded motion estimation - dispatch threads */                  /* multithreaded motion estimation - dispatch threads */
2353                  int rows_per_thread = (pEnc->mbParam.mb_height + pEnc->num_threads - 1)/pEnc->num_threads;                  while (k < pEnc->num_threads) {
2354                            int i, add_s = (slices_per_thread + 512) >> 10;
2355                            int add_t = (threads_per_slice + 512) >> 10;
2356    
2357                            int start_y = (bound * mb_height + (num_slices-1)) / num_slices;
2358                            int stop_y = ((bound+add_s) * mb_height + (num_slices-1)) / num_slices;
2359                            int rows_per_thread = (stop_y - start_y + add_t - 1) / add_t;
2360    
2361                            slices_per_thread += ((num_slices*1024 / num_threads) - add_s*1024);
2362                            threads_per_slice += ((pEnc->num_threads*1024 / num_threads) - add_t*1024);
2363    
2364                            for (i = 0; i < add_t; i++) {
2365                                    memset(pEnc->smpData[k+i].complete_count_self, 0, rows_per_thread * sizeof(int));
2366    
2367                                    pEnc->smpData[k+i].pEnc = (void *) pEnc;
2368                                    pEnc->smpData[k+i].current = frame;
2369    
2370                                    pEnc->smpData[k+i].y_row = i;
2371                                    pEnc->smpData[k+i].y_step = add_t;
2372                                    pEnc->smpData[k+i].stop_y = stop_y;
2373                                    pEnc->smpData[k+i].start_y = start_y;
2374    
                 for (k = 0; k < pEnc->num_threads; k++) {  
                         memset(pEnc->motionData[k].complete_count_self, 0, rows_per_thread * sizeof(int));  
                         pEnc->motionData[k].pParam = &pEnc->mbParam;  
                         pEnc->motionData[k].current = frame;  
                         pEnc->motionData[k].reference = pEnc->current;  
                         pEnc->motionData[k].fRef = f_ref;  
                         pEnc->motionData[k].fRefH = &pEnc->f_refh;  
                         pEnc->motionData[k].fRefV = &pEnc->f_refv;  
                         pEnc->motionData[k].fRefHV = &pEnc->f_refhv;  
                         pEnc->motionData[k].pRef = b_ref;  
                         pEnc->motionData[k].pRefH = &pEnc->vInterH;  
                         pEnc->motionData[k].pRefV = &pEnc->vInterV;  
                         pEnc->motionData[k].pRefHV = &pEnc->vInterHV;  
                         pEnc->motionData[k].time_bp = (int32_t)(pEnc->current->stamp - frame->stamp);  
                         pEnc->motionData[k].time_pp = (int32_t)(pEnc->current->stamp - pEnc->reference->stamp);  
                         pEnc->motionData[k].y_step = pEnc->num_threads;  
                         pEnc->motionData[k].start_y = k;  
2375                          /* todo: sort out temp space once and for all */                          /* todo: sort out temp space once and for all */
2376                          pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pEnc->mbParam.edged_width;                                  pEnc->smpData[k+i].RefQ = (((k+i)&1) ? pEnc->vInterV.u : pEnc->vInterV.v) +
2377                                                                                            16*((k+i)>>1)*pParam->edged_width;
2378                            }
2379    
2380                            pEnc->smpData[k].complete_count_above =
2381                                    pEnc->smpData[k+add_t-1].complete_count_self - 1;
2382    
2383                            bound += add_s;
2384                            k += add_t;
2385                  }                  }
2386    
2387                  for (k = 1; k < pEnc->num_threads; k++) {                  for (k = 1; k < pEnc->num_threads; k++) {
2388                          pthread_create(&pEnc->motionData[k].handle, NULL,                          pthread_create(&pEnc->smpData[k].handle, NULL,
2389                                  (void*)SMPMotionEstimationBVOP, (void*)&pEnc->motionData[k]);                                  (void*)SMPMotionEstimationBVOP, (void*)&pEnc->smpData[k]);
2390                  }                  }
2391    
2392                  SMPMotionEstimationBVOP(&pEnc->motionData[0]);                  SMPMotionEstimationBVOP(&pEnc->smpData[0]);
2393    
2394                  for (k = 1; k < pEnc->num_threads; k++) {                  for (k = 1; k < pEnc->num_threads; k++) {
2395                          pthread_join(pEnc->motionData[k].handle, &status);                          pthread_join(pEnc->smpData[k].handle, &status);
2396                  }                  }
2397    
2398                  frame->fcode = frame->bcode = 0;                  frame->fcode = frame->bcode = 0;
2399                  for (k = 0; k < pEnc->num_threads; k++) {                  for (k = 0; k < pEnc->num_threads; k++) {
2400                          if (pEnc->motionData[k].minfcode > frame->fcode)                          if (pEnc->smpData[k].minfcode > frame->fcode)
2401                                  frame->fcode = pEnc->motionData[k].minfcode;                                  frame->fcode = pEnc->smpData[k].minfcode;
2402                          if (pEnc->motionData[k].minbcode > frame->bcode)                          if (pEnc->smpData[k].minbcode > frame->bcode)
2403                                  frame->bcode = pEnc->motionData[k].minbcode;                                  frame->bcode = pEnc->smpData[k].minbcode;
2404                  }                  }
2405          } else {          } else {
2406    
2407                  MotionEstimationBVOP(&pEnc->mbParam, frame,                  MotionEstimationBVOP(&pEnc->mbParam, frame,
2408                                                           ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */                                                           ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */
2409                                                           ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */                                                           ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */
2410                                                           pEnc->reference->mbs, f_ref,                                                           pEnc->reference->mbs, f_ref,
2411                                                           &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                           &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
2412                                                           pEnc->current, b_ref, &pEnc->vInterH,                                                           pEnc->current, b_ref, &pEnc->vInterH,
2413                                                           &pEnc->vInterV, &pEnc->vInterHV);                                                           &pEnc->vInterV, &pEnc->vInterHV,
2414                                                             pEnc->num_slices);
2415          }          }
2416          stop_motion_timer();          stop_motion_timer();
2417    
2418          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
2419          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant);
2420    
2421            /* reset stats */
2422          frame->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
2423          frame->sStat.iMVBits = 0;          frame->sStat.iMVBits = 0;
2424          frame->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
# Line 2075  Line 2427 
2427          frame->sStat.mblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;          frame->sStat.mblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
2428          frame->sStat.kblks = frame->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.ublks = 0;
2429    
2430          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          /* multithreaded inter coding - dispatch threads */
2431                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {          bound = 0;
2432                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];          slices_per_thread = (num_slices*1024 / num_threads);
2433    
2434            for (k = 0; k < num_threads; k++) {
2435                    int add = ((slices_per_thread + 512) >> 10);
2436    
2437                    slices_per_thread += ((num_slices*1024 / num_threads) - add*1024);
2438    
2439                    pEnc->smpData[k].pEnc = (void *) pEnc;
2440                    pEnc->smpData[k].current = frame;
2441                    pEnc->smpData[k].stop_y = (((bound+add) * mb_height + (num_slices-1)) / num_slices);
2442                    pEnc->smpData[k].start_y = ((bound * mb_height + (num_slices-1)) / num_slices);
2443                    bound += add;
2444    
2445                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */                  /* todo: sort out temp space once and for all */
2446                          if (mb->mode == MODE_NOT_CODED) {                  pEnc->smpData[k].RefQ = ((k&1) ? pEnc->vInterV.u : pEnc->vInterV.v) + 16*(k>>1)*pParam->edged_width;
2447                                  if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {  
2448                                          MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image,                  if (k > 0) {
2449                                                                                          NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0);                          BitstreamReset(pEnc->smpData[k].bs);
2450                            pEnc->smpData[k].sStat->iTextBits = pEnc->smpData[k].sStat->kblks =
2451                            pEnc->smpData[k].sStat->mblks = pEnc->smpData[k].sStat->ublks = pEnc->smpData[k].sStat->iMVBits = 0;
2452                                  }                                  }
                                 continue;  
2453                          }                          }
2454    
2455                          mb->quant = frame->quant;          for (k = 1; k < num_threads; k++) {
2456                    pthread_create(&pEnc->smpData[k].handle, NULL,
2457                          if (mb->cbp != 0 || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {                          (void*)SliceCodeB, (void*)&pEnc->smpData[k]);
                                 /* we have to motion-compensate, transfer etc,  
                                         because there might be blocks to code */  
   
                                 MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,  
                                                                                  f_ref, &pEnc->f_refh, &pEnc->f_refv,  
                                                                                  &pEnc->f_refhv, b_ref, &pEnc->vInterH,  
                                                                                  &pEnc->vInterV, &pEnc->vInterHV,  
                                                                                  dct_codes);  
   
                                 mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);  
2458                          }                          }
2459    
2460                          if (mb->mode == MODE_DIRECT_NO4V)          pEnc->smpData[0].bs = bs;
2461                                  mb->mode = MODE_DIRECT;          pEnc->smpData[0].sStat = &frame->sStat;
2462            SliceCodeB(&pEnc->smpData[0]);
2463    
2464                          if (mb->mode == MODE_DIRECT && (mb->cbp | mb->pmvs[3].x | mb->pmvs[3].y) == 0)          for (k = 1; k < num_threads; k++) {
2465                                  mb->mode = MODE_DIRECT_NONE_MV; /* skipped */                  pthread_join(pEnc->smpData[k].handle, &status);
                         else  
                                 if (frame->vop_flags & XVID_VOP_GREYSCALE)  
                                         /* keep only bits 5-2 -- Chroma blocks will just be skipped by MBCodingBVOP */  
                                         mb->cbp &= 0x3C;  
   
                         start_timer();  
                         MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs,  
                                                  &frame->sStat);  
                         stop_coding_timer();  
                 }  
2466          }          }
         emms();  
2467    
2468          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */          frame->length = BitstreamLength(bs) - (bits/8);
2469          frame->length = (BitstreamPos(bs) - bits) / 8;  
2470            /* reassemble the pieces together */
2471            SerializeBitstreams(pEnc, frame, bs, num_threads);
2472    
2473  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
2474          if (!first){          if (!first){

Legend:
Removed from v.1.130  
changed lines
  Added in v.1.135

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