[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.26, Wed Apr 17 14:05:54 2002 UTC revision 1.28, Thu Apr 25 19:27:49 2002 UTC
# Line 21  Line 21 
21  #include "utils/mem_align.h"  #include "utils/mem_align.h"
22    
23  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT
24    #define SWAP(A,B)    { void * tmp = A; A = B; B = tmp; }
25    
26    
27  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits);  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits);
# Line 37  Line 38 
38  };  };
39    
40    
41    void static image_null(IMAGE * image)
42    {
43            image->y = image->u = image->v = NULL;
44    }
45    
46    
47  int encoder_create(XVID_ENC_PARAM * pParam)  int encoder_create(XVID_ENC_PARAM * pParam)
48  {  {
49          Encoder *pEnc;          Encoder *pEnc;
# Line 122  Line 129 
129    
130          /* Fill rate control parameters */          /* Fill rate control parameters */
131    
         pEnc->mbParam.quant = 4;  
   
132          pEnc->bitrate = pParam->rc_bitrate;          pEnc->bitrate = pParam->rc_bitrate;
133    
134          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
135          pEnc->iMaxKeyInterval = pParam->max_key_interval;          pEnc->iMaxKeyInterval = pParam->max_key_interval;
136    
137          /* try to allocate memory */          /* try to allocate frame memory */
138    
139            pEnc->current = NULL;
140            pEnc->reference = NULL;
141            if ( (pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL ||
142                     (pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL)
143            {
144                    if (pEnc->current) xvid_free(pEnc->current);
145                    xvid_free(pEnc);
146                    return XVID_ERR_MEMORY;
147            }
148    
149            /* try to allocate mb memory */
150    
151          pEnc->sCurrent.y        =       pEnc->sCurrent.u        =       pEnc->sCurrent.v        = NULL;          pEnc->current->mbs = NULL;
152          pEnc->sReference.y      =       pEnc->sReference.u      =       pEnc->sReference.v      = NULL;          pEnc->reference->mbs = NULL;
         pEnc->vInterH.y         =       pEnc->vInterH.u         =       pEnc->vInterH.v         = NULL;  
         pEnc->vInterV.y         =       pEnc->vInterV.u         =       pEnc->vInterV.v         = NULL;  
         pEnc->vInterVf.y        =       pEnc->vInterVf.u        =       pEnc->vInterVf.v        = NULL;  
         pEnc->vInterHV.y        =       pEnc->vInterHV.u        =       pEnc->vInterHV.v        = NULL;  
         pEnc->vInterHVf.y       =       pEnc->vInterHVf.u       =       pEnc->vInterHVf.v       = NULL;  
153    
154          pEnc->pMBs = NULL;  #ifdef _DEBUG
155    #ifdef WIN32
156    OutputDebugString("malloc mbs");
157    #endif
158    #endif
159    
160          if (image_create(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||          if ((pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL ||
161                  image_create(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                  (pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL)
162            {
163                    if (pEnc->current->mbs) xvid_free(pEnc->current->mbs);
164                    xvid_free(pEnc->current);
165                    xvid_free(pEnc->reference);
166                    xvid_free(pEnc);
167            }
168    
169            /* try to allocate image memory */
170    
171    #ifdef _DEBUG
172            image_null(&pEnc->sOriginal);
173    #endif
174            image_null(&pEnc->current->image);
175            image_null(&pEnc->reference->image);
176            image_null(&pEnc->vInterH);
177            image_null(&pEnc->vInterV);
178            image_null(&pEnc->vInterVf);
179            image_null(&pEnc->vInterHV);
180            image_null(&pEnc->vInterHVf);
181    
182    #ifdef _DEBUG
183    #ifdef WIN32
184    OutputDebugString("malloc images");
185    #endif
186    #endif
187            if (
188    #ifdef _DEBUG
189                    image_create(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||
190    #endif
191                    image_create(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||
192                    image_create(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||
193                  image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                  image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||
194                  image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                  image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||
195                  image_create(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                  image_create(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||
196                  image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                  image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||
197                  image_create(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                  image_create(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)
198            {
199  #ifdef _DEBUG  #ifdef _DEBUG
200                  image_create(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);
201  #endif  #endif
202                  (pEnc->pMBs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL)                  image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);
203          {                  image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);
                 image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
204                  image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);
205                  image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);
206                  image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);
207                  image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);
208                  image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);
209  #ifdef _DEBUG  
210                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  xvid_free(pEnc->current);
211  #endif                  xvid_free(pEnc->reference);
                 if (pEnc)  
                 {  
212                          xvid_free(pEnc);                          xvid_free(pEnc);
                 }  
213                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
214          }          }
215    
         // init macroblock array  
         for (i = 0; i < pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; i++)  
         {  
                 pEnc->pMBs[i].dquant = NO_CHANGE;  
         }  
   
216          pParam->handle = (void *)pEnc;          pParam->handle = (void *)pEnc;
217    
218          if (pParam->rc_bitrate)          if (pParam->rc_bitrate)
# Line 194  Line 231 
231  int encoder_destroy(Encoder * pEnc)  int encoder_destroy(Encoder * pEnc)
232  {  {
233          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
         ENC_CHECK(pEnc->sCurrent.y);  
         ENC_CHECK(pEnc->sReference.y);  
234    
235          xvid_free(pEnc->pMBs);          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);
236          image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);
         image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
237          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);
238          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);
239          image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);
# Line 208  Line 242 
242  #ifdef _DEBUG  #ifdef _DEBUG
243                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);
244  #endif  #endif
245          xvid_free(pEnc);          xvid_free(pEnc->current->mbs);
246            xvid_free(pEnc->current);
247    
248            xvid_free(pEnc->reference->mbs);
249            xvid_free(pEnc->reference);
250    
251            xvid_free(pEnc);
252          return XVID_ERR_OK;          return XVID_ERR_OK;
253  }  }
254    
# Line 231  Line 270 
270          ENC_CHECK(pFrame->bitstream);          ENC_CHECK(pFrame->bitstream);
271          ENC_CHECK(pFrame->image);          ENC_CHECK(pFrame->image);
272    
273          pEnc->mbParam.global_flags = pFrame->general;          SWAP(pEnc->current, pEnc->reference);
274          pEnc->mbParam.motion_flags = pFrame->motion;  
275            pEnc->current->global_flags = pFrame->general;
276            pEnc->current->motion_flags = pFrame->motion;
277          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
278    
279          start_timer();          start_timer();
280          if (image_input(&pEnc->sCurrent, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width,          if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width,
281                          pFrame->image, pFrame->colorspace))                          pFrame->image, pFrame->colorspace))
282          {          {
283                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
284          }          }
285          stop_conv_timer();          stop_conv_timer();
286    
         EMMS();  
   
287  #ifdef _DEBUG  #ifdef _DEBUG
288          image_copy(&pEnc->sOriginal, &pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.height);          image_copy(&pEnc->sOriginal, &pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.height);
289  #endif  #endif
290    
291            EMMS();
292    
293          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
294    
295          if (pFrame->quant == 0)          if (pFrame->quant == 0)
296          {          {
297                  pEnc->mbParam.quant = RateControlGetQ(0);                  pEnc->current->quant = RateControlGetQ(0);
298          }          }
299          else          else
300          {          {
301                  pEnc->mbParam.quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
302          }          }
303    
304          if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0)          if ((pEnc->current->global_flags & XVID_LUMIMASKING))
305          {          {
306                  int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);                  int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);
307    
308                  pEnc->mbParam.quant = adaptive_quantization(pEnc->sCurrent.y,                  pEnc->current->quant = adaptive_quantization(pEnc->current->image.y,
309                                                              pEnc->mbParam.width,                                                              pEnc->mbParam.edged_width,  // stride
310                                                              temp_dquants,                                                              temp_dquants,
311                                                              pEnc->mbParam.quant,                                                              pEnc->current->quant,
312                                                              pEnc->mbParam.quant,                                                              pEnc->current->quant,       // min_quant
313                                                              2*pEnc->mbParam.quant,                                                              2*pEnc->current->quant,     // max_quant
314                                                              pEnc->mbParam.mb_width,                                                              pEnc->mbParam.mb_width,
315                                                              pEnc->mbParam.mb_height);                                                              pEnc->mbParam.mb_height);
316    
317                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                  for (y = 0; y < pEnc->mbParam.mb_height; y++)
318                          for (x = 0; x < pEnc->mbParam.mb_width; x++)                          for (x = 0; x < pEnc->mbParam.mb_width; x++)
319                          {                          {
320                                  MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];                                  MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
321                                  pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)];                                  pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)];
322                          }                          }
323                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
324          }          }
325    
326          if(pEnc->mbParam.global_flags & XVID_H263QUANT) {          if (pEnc->current->global_flags & XVID_H263QUANT) {
327                  if(pEnc->mbParam.quant_type != H263_QUANT)                  if(pEnc->mbParam.m_quant_type != H263_QUANT)
328                          write_vol_header = 1;                          write_vol_header = 1;
329                  pEnc->mbParam.quant_type = H263_QUANT;                  pEnc->mbParam.m_quant_type = H263_QUANT;
330          }          }
331          else if(pEnc->mbParam.global_flags & XVID_MPEGQUANT) {          else if(pEnc->current->global_flags & XVID_MPEGQUANT) {
332                  int ret1, ret2;                  int ret1, ret2;
333    
334                  ret1 = ret2 = 0;                  ret1 = ret2 = 0;
335    
336                  if(pEnc->mbParam.quant_type != MPEG4_QUANT)                  if(pEnc->mbParam.m_quant_type != MPEG4_QUANT)
337                          write_vol_header = 1;                          write_vol_header = 1;
338    
339                  pEnc->mbParam.quant_type = MPEG4_QUANT;                  pEnc->mbParam.m_quant_type = MPEG4_QUANT;
340    
341                  if ((pEnc->mbParam.global_flags & XVID_CUSTOM_QMATRIX) > 0) {                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
342                          if(pFrame->quant_intra_matrix != NULL)                          if(pFrame->quant_intra_matrix != NULL)
343                                  ret1 = set_intra_matrix(pFrame->quant_intra_matrix);                                  ret1 = set_intra_matrix(pFrame->quant_intra_matrix);
344                          if(pFrame->quant_inter_matrix != NULL)                          if(pFrame->quant_inter_matrix != NULL)
# Line 335  Line 376 
376    
377          if (pResult)          if (pResult)
378          {          {
379                  pResult->quant = pEnc->mbParam.quant;                  pResult->quant = pEnc->current->quant;
380                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
381                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->sStat.kblks;
382                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->sStat.mblks;
# Line 346  Line 387 
387    
388          if (pFrame->quant == 0)          if (pFrame->quant == 0)
389          {          {
390                  RateControlUpdate(pEnc->mbParam.quant, pFrame->length, pFrame->intra);                  RateControlUpdate(pEnc->current->quant, pFrame->length, pFrame->intra);
391          }          }
392    
393  #ifdef _DEBUG  #ifdef _DEBUG
394          psnr = image_psnr(&pEnc->sOriginal, &pEnc->sCurrent, pEnc->mbParam.edged_width,          psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width,
395                                  pEnc->mbParam.width, pEnc->mbParam.height);                                  pEnc->mbParam.width, pEnc->mbParam.height);
396    
397          sprintf(temp, "PSNR: %f\n", psnr);          sprintf(temp, "PSNR: %f\n", psnr);
# Line 358  Line 399 
399  #endif  #endif
400    
401          pEnc->iFrameNum++;          pEnc->iFrameNum++;
         image_swap(&pEnc->sCurrent, &pEnc->sReference);  
402    
403          stop_global_timer();          stop_global_timer();
404          write_timer();          write_timer();
# Line 371  Line 411 
411    
412          pMB->mode = MODE_INTRA;          pMB->mode = MODE_INTRA;
413    
414          if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) {          /* zero mv statistics */
415            pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0;
416            pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0;
417            pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;
418            pMB->sad16 = 0;
419    
420            if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
421                  if(pMB->dquant != NO_CHANGE)                  if(pMB->dquant != NO_CHANGE)
422                  {                  {
423                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
424                          pEnc->mbParam.quant += DQtab[pMB->dquant];                          pEnc->current->quant += DQtab[pMB->dquant];
425    
426                          if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31;                          if (pEnc->current->quant > 31) pEnc->current->quant = 31;
427                          if (pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1;                          if (pEnc->current->quant < 1) pEnc->current->quant = 1;
428                  }                  }
429          }          }
430    
431          pMB->quant = pEnc->mbParam.quant;          pMB->quant = pEnc->current->quant;
432  }  }
433    
434    
# Line 413  Line 459 
459                  return;                  return;
460          }          }
461    
462          pEnc->mbParam.fixed_code = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);          pEnc->current->fcode = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);
463    
464          length  = pEnc->mbParam.fixed_code + 5;          length  = pEnc->current->fcode + 5;
465          high    = 1 << (length - 1);          high    = 1 << (length - 1);
466    
467          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)
468          {          {
469                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)
470                  {                  {
471                          MACROBLOCK * pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
472                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
473                          VECTOR pred[4];                          VECTOR pred[4];
474                          VECTOR tmp;                          VECTOR tmp;
# Line 441  Line 487 
487                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;
488                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;
489    
490                                  get_pmvdata(pEnc->pMBs, x, y, pEnc->mbParam.mb_width, 0, pred, dummy);                                  get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, 0, pred, dummy);
491    
492                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec=0 ; vec<4 ; ++vec)
493                                  {                                  {
# Line 460  Line 506 
506                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;
507                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;
508    
509                                          get_pmvdata(pEnc->pMBs, x, y, pEnc->mbParam.mb_width, vec, pred, dummy);                                          get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, vec, pred, dummy);
510    
511                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
512                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
# Line 476  Line 522 
522                                  }                                  }
523                          }                          }
524    
525                          if (pMB->dquant != NO_CHANGE && pMB->mode == MODE_INTER4V)                          if (pMB->mode == MODE_INTER4V &&
526                                    (pEnc->current->global_flags & XVID_LUMIMASKING) && pMB->dquant != NO_CHANGE)
527                          {                          {
528                                  pMB->mode = MODE_INTRA;                                  pMB->mode = MODE_INTRA;
529    
# Line 519  Line 566 
566                  return;                  return;
567          }          }
568    
569          length  = pEnc->mbParam.fixed_code + 5;          length  = pEnc->current->fcode + 5;
570          high    = 1 << (length - 1);          high    = 1 << (length - 1);
571    
572          if (hint->rawhints)          if (hint->rawhints)
573          {          {
574                  hint->mvhint.fcode = pEnc->mbParam.fixed_code;                  hint->mvhint.fcode = pEnc->current->fcode;
575          }          }
576          else          else
577          {          {
578                  BitstreamPutBits(&bs, pEnc->mbParam.fixed_code, FCODEBITS);                  BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);
579          }          }
580    
581          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)
582          {          {
583                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)
584                  {                  {
585                          MACROBLOCK * pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
586                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
587                          VECTOR tmp;                          VECTOR tmp;
588    
# Line 609  Line 656 
656          uint16_t x, y;          uint16_t x, y;
657    
658          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
659          pEnc->mbParam.rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
660          pEnc->mbParam.coding_type = I_VOP;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
661            pEnc->current->coding_type = I_VOP;
662    
663          BitstreamWriteVolHeader(bs, &pEnc->mbParam);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
664          BitstreamWriteVopHeader(bs, &pEnc->mbParam);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);
665    
666          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
667    
# Line 624  Line 672 
672          for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++)
673                  for (x = 0; x < pEnc->mbParam.mb_width; x++)                  for (x = 0; x < pEnc->mbParam.mb_width; x++)
674                  {                  {
675                          MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
676    
677                          CodeIntraMB(pEnc, pMB);                          CodeIntraMB(pEnc, pMB);
678    
679                          MBTransQuantIntra(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, &pEnc->sCurrent);                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);
680    
681                          start_timer();                          start_timer();
682                          MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
683                          stop_prediction_timer();                          stop_prediction_timer();
684    
685                          start_timer();                          start_timer();
686                          MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
687                          stop_coding_timer();                          stop_coding_timer();
688                  }                  }
689    
# Line 645  Line 693 
693          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
694          pEnc->sStat.iMvSum = 0;          pEnc->sStat.iMvSum = 0;
695          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
696          pEnc->mbParam.fixed_code = 2;          pEnc->mbParam.m_fcode = 2;
697    
698          if (pEnc->mbParam.global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET)
699          {          {
700                  HintedMEGet(pEnc, 1);                  HintedMEGet(pEnc, 1);
701          }          }
# Line 670  Line 718 
718          int iSearchRange;          int iSearchRange;
719          bool bIntra;          bool bIntra;
720    
721          IMAGE *pCurrent = &pEnc->sCurrent;          IMAGE *pCurrent = &pEnc->current->image;
722          IMAGE *pRef = &pEnc->sReference;          IMAGE *pRef = &pEnc->reference->image;
723    
724          start_timer();          start_timer();
725          image_setedges(pRef,          image_setedges(pRef,
# Line 679  Line 727 
727                         pEnc->mbParam.edged_height,                         pEnc->mbParam.edged_height,
728                         pEnc->mbParam.width,                         pEnc->mbParam.width,
729                         pEnc->mbParam.height,                         pEnc->mbParam.height,
730                         pEnc->mbParam.global_flags & XVID_INTERLACING);                         pEnc->current->global_flags & XVID_INTERLACING);
731          stop_edges_timer();          stop_edges_timer();
732    
733          pEnc->mbParam.rounding_type = 1 - pEnc->mbParam.rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
734            pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
735            pEnc->current->fcode = pEnc->mbParam.m_fcode;
736    
737          if (!force_inter)          if (!force_inter)
738                  iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD);                  iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD);
739          else          else
740                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;
741    
742          if ((pEnc->mbParam.global_flags & XVID_HALFPEL) > 0) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
743                  start_timer();                  start_timer();
744                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
745                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
746                                    pEnc->mbParam.rounding_type);                                    pEnc->current->rounding_type);
747                  stop_inter_timer();                  stop_inter_timer();
748          }          }
749    
750          start_timer();          start_timer();
751          if (pEnc->mbParam.global_flags & XVID_HINTEDME_SET)          if (pEnc->current->global_flags & XVID_HINTEDME_SET)
752          {          {
753                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
754          }          }
755          else          else
756          {          {
757                  bIntra = MotionEstimation(pEnc->pMBs, &pEnc->mbParam, &pEnc->sReference,                  bIntra = MotionEstimation(
758                                            &pEnc->vInterH, &pEnc->vInterV,                          &pEnc->mbParam,
759                                            &pEnc->vInterHV, &pEnc->sCurrent, iLimit);                          pEnc->current,
760                            pEnc->reference,
761                            &pEnc->vInterH,
762                            &pEnc->vInterV,
763                            &pEnc->vInterHV,
764                            iLimit);
765          }          }
766          stop_motion_timer();          stop_motion_timer();
767    
# Line 715  Line 770 
770                  return FrameCodeI(pEnc, bs, pBits);                  return FrameCodeI(pEnc, bs, pBits);
771          }          }
772    
773          pEnc->mbParam.coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
774    
775          if(vol_header)          if(vol_header)
776                  BitstreamWriteVolHeader(bs, &pEnc->mbParam);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
777    
778          BitstreamWriteVopHeader(bs, &pEnc->mbParam);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);
779    
780          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
781    
# Line 733  Line 788 
788          {          {
789                  for(x = 0; x < pEnc->mbParam.mb_width; x++)                  for(x = 0; x < pEnc->mbParam.mb_width; x++)
790                  {                  {
791                          MACROBLOCK * pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
792    
793                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
794    
# Line 742  Line 797 
797                                  start_timer();                                  start_timer();
798                                  MBMotionCompensation(pMB,                                  MBMotionCompensation(pMB,
799                                                       x, y,                                                       x, y,
800                                                       &pEnc->sReference,                                                       &pEnc->reference->image,
801                                                       &pEnc->vInterH,                                                       &pEnc->vInterH,
802                                                       &pEnc->vInterV,                                                       &pEnc->vInterV,
803                                                       &pEnc->vInterHV,                                                       &pEnc->vInterHV,
804                                                       &pEnc->sCurrent,                                                       &pEnc->current->image,
805                                                       dct_codes,                                                       dct_codes,
806                                                       pEnc->mbParam.width,                                                       pEnc->mbParam.width,
807                                                       pEnc->mbParam.height,                                                       pEnc->mbParam.height,
808                                                       pEnc->mbParam.edged_width,                                                       pEnc->mbParam.edged_width,
809                                                       pEnc->mbParam.rounding_type);                                                       pEnc->current->rounding_type);
810                                  stop_comp_timer();                                  stop_comp_timer();
811    
812                                  if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) {                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
813                                          if(pMB->dquant != NO_CHANGE) {                                          if(pMB->dquant != NO_CHANGE) {
814                                                  pMB->mode = MODE_INTER_Q;                                                  pMB->mode = MODE_INTER_Q;
815                                                  pEnc->mbParam.quant += DQtab[pMB->dquant];                                                  pEnc->current->quant += DQtab[pMB->dquant];
816                                                  if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31;                                                  if (pEnc->current->quant > 31) pEnc->current->quant = 31;
817                                                  else if(pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1;                                                  else if(pEnc->current->quant < 1) pEnc->current->quant = 1;
818                                          }                                          }
819                                  }                                  }
820                                  pMB->quant = pEnc->mbParam.quant;                                  pMB->quant = pEnc->current->quant;
821    
822                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
823    
824                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, pCurrent);                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);
825                          }                          }
826                          else                          else
827                          {                          {
828                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
829                                  MBTransQuantIntra(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, pCurrent);                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);
830                          }                          }
831    
832                          start_timer();                          start_timer();
833                          MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
834                          stop_prediction_timer();                          stop_prediction_timer();
835    
836                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)
# Line 796  Line 851 
851                          }                          }
852    
853                          start_timer();                          start_timer();
854                          MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
855                          stop_coding_timer();                          stop_coding_timer();
856                  }                  }
857          }          }
858    
859          emms();          emms();
860    
861          if (pEnc->mbParam.global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET)
862          {          {
863                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
864          }          }
# Line 813  Line 868 
868    
869          fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);          fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);
870    
871          iSearchRange = 1 << (3 + pEnc->mbParam.fixed_code);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
872    
873          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
874              && (pEnc->mbParam.fixed_code <= 3)) // maximum search range 128              && (pEnc->mbParam.m_fcode <= 3))    // maximum search range 128
875          {          {
876                  pEnc->mbParam.fixed_code++;                  pEnc->mbParam.m_fcode++;
877                  iSearchRange *= 2;                  iSearchRange *= 2;
878          }          }
879          else if ((fSigma < iSearchRange / 6)          else if ((fSigma < iSearchRange / 6)
880                   && (pEnc->sStat.fMvPrevSigma >= 0)                   && (pEnc->sStat.fMvPrevSigma >= 0)
881                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)
882                   && (pEnc->mbParam.fixed_code >= 2))    // minimum search range 16                   && (pEnc->mbParam.m_fcode >= 2))       // minimum search range 16
883          {          {
884                  pEnc->mbParam.fixed_code--;                  pEnc->mbParam.m_fcode--;
885                  iSearchRange /= 2;                  iSearchRange /= 2;
886          }          }
887    

Legend:
Removed from v.1.26  
changed lines
  Added in v.1.28

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