[svn] / branches / dev-api-3 / xvidcore / src / encoder.c Repository:
ViewVC logotype

Diff of /branches/dev-api-3/xvidcore/src/encoder.c

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

revision 4, Fri Mar 8 19:17:46 2002 UTC revision 236, Sun Jun 23 19:48:06 2002 UTC
# Line 1  Line 1 
1    /*****************************************************************************
2     *
3     *  XVID MPEG-4 VIDEO CODEC
4     *  -  Encoder main module  -
5     *
6     *  This program is an implementation of a part of one or more MPEG-4
7     *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending
8     *  to use this software module in hardware or software products are
9     *  advised that its use may infringe existing patents or copyrights, and
10     *  any such use would be at such party's own risk.  The original
11     *  developer of this software module and his/her company, and subsequent
12     *  editors and their companies, will have no liability for use of this
13     *  software or modifications or derivatives thereof.
14     *
15     *  This program is free software; you can redistribute it and/or modify
16     *  it under the terms of the GNU General Public License as published by
17     *  the Free Software Foundation; either version 2 of the License, or
18     *  (at your option) any later version.
19     *
20     *  This program is distributed in the hope that it will be useful,
21     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     *  GNU General Public License for more details.
24     *
25     *  You should have received a copy of the GNU General Public License
26     *  along with this program; if not, write to the Free Software
27     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
28     *
29     ****************************************************************************/
30    
31    /*****************************************************************************
32     *
33     *  History
34     *
35     *      20.06.2002 bframe patch
36     *  08.05.2002 fix some problem in DEBUG mode;
37     *             MinChen <chenm001@163.com>
38     *  14.04.2002 added FrameCodeB()
39     *
40     *  $Id: encoder.c,v 1.47 2002-06-23 19:48:06 edgomez Exp $
41     *
42     ****************************************************************************/
43    
44  #include <stdlib.h>  #include <stdlib.h>
45  #include <stdio.h>  #include <stdio.h>
46  #include <math.h>  #include <math.h>
47    #include <string.h>
48    
49  #include "encoder.h"  #include "encoder.h"
50  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
51  #include "global.h"  #include "global.h"
52  #include "utils/timer.h"  #include "utils/timer.h"
53  #include "image/image.h"  #include "image/image.h"
54    #include "motion/motion.h"
55  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
56  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
57  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
# Line 16  Line 61 
61  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
62  #include "quant/adapt_quant.h"  #include "quant/adapt_quant.h"
63  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
64    #include "utils/mem_align.h"
65    
66  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT  /*****************************************************************************
67     * Local macros
68     ****************************************************************************/
69    
70    #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT
71    #define SWAP(A,B)    { void * tmp = A; A = B; B = tmp; }
72    
73  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits);  /*****************************************************************************
74  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header);   * Local function prototypes
75     ****************************************************************************/
76    
77    static int FrameCodeI(Encoder * pEnc,
78                                              Bitstream * bs,
79                                              uint32_t * pBits);
80    
81    static int FrameCodeP(Encoder * pEnc,
82                                              Bitstream * bs,
83                                              uint32_t * pBits,
84                                              bool force_inter,
85                                              bool vol_header);
86    
87    #ifdef BFRAMES
88    static void FrameCodeB(Encoder * pEnc,
89                                               FRAMEINFO * frame,
90                                               Bitstream * bs,
91                                               uint32_t * pBits);
92    #endif
93    
94    /*****************************************************************************
95     * Local data
96     ****************************************************************************/
97    
98  static int DQtab[4] =  static int DQtab[4] = {
 {  
99          -1, -2, 1, 2          -1, -2, 1, 2
100  };  };
101    
102  static int iDQtab[5] =  static int iDQtab[5] = {
 {  
103          1, 0, NO_CHANGE, 2, 3          1, 0, NO_CHANGE, 2, 3
104  };  };
105    
106    
107  int encoder_create(XVID_ENC_PARAM * pParam)  static void __inline
108    image_null(IMAGE * image)
109    {
110            image->y = image->u = image->v = NULL;
111    }
112    
113    
114    /*****************************************************************************
115     * Encoder creation
116     *
117     * This function creates an Encoder instance, it allocates all necessary
118     * image buffers (reference, current and bframes) and initialize the internal
119     * xvid encoder paremeters according to the XVID_ENC_PARAM input parameter.
120     *
121     * The code seems to be very long but is very basic, mainly memory allocation
122     * and cleaning code.
123     *
124     * Returned values :
125     *    - XVID_ERR_OK     - no errors
126     *    - XVID_ERR_MEMORY - the libc could not allocate memory, the function
127     *                        cleans the structure before exiting.
128     *                        pParam->handle is also set to NULL.
129     *
130     ****************************************************************************/
131    
132    int
133    encoder_create(XVID_ENC_PARAM * pParam)
134  {  {
135      Encoder *pEnc;      Encoder *pEnc;
136          uint32_t i;          int i;
137    
138      pParam->handle = NULL;      pParam->handle = NULL;
139    
# Line 48  Line 144 
144      ENC_CHECK(!(pParam->width % 2));      ENC_CHECK(!(pParam->width % 2));
145      ENC_CHECK(!(pParam->height % 2));      ENC_CHECK(!(pParam->height % 2));
146    
147          if (pParam->fincr <= 0 || pParam->fbase <= 0)          /* Fps */
148          {  
149            if (pParam->fincr <= 0 || pParam->fbase <= 0) {
150                  pParam->fincr = 1;                  pParam->fincr = 1;
151                  pParam->fbase = 25;                  pParam->fbase = 25;
152          }          }
153    
154          // simplify the "fincr/fbase" fraction          /*
155          // (neccessary, since windows supplies us with huge numbers)           * Simplify the "fincr/fbase" fraction
156             * (neccessary, since windows supplies us with huge numbers)
157             */
158    
159          i = pParam->fincr;          i = pParam->fincr;
160          while (i > 1)          while (i > 1) {
161          {                  if (pParam->fincr % i == 0 && pParam->fbase % i == 0) {
                 if (pParam->fincr % i == 0 && pParam->fbase % i == 0)  
                 {  
162                          pParam->fincr /= i;                          pParam->fincr /= i;
163                          pParam->fbase /= i;                          pParam->fbase /= i;
164                          i = pParam->fincr;                          i = pParam->fincr;
# Line 70  Line 167 
167                  i--;                  i--;
168          }          }
169    
170          if (pParam->fbase > 65535)          if (pParam->fbase > 65535) {
         {  
171                  float div = (float)pParam->fbase / 65535;                  float div = (float)pParam->fbase / 65535;
172    
173                  pParam->fbase = (int)(pParam->fbase / div);                  pParam->fbase = (int)(pParam->fbase / div);
174                  pParam->fincr = (int)(pParam->fincr / div);                  pParam->fincr = (int)(pParam->fincr / div);
175          }          }
176    
177          if (pParam->bitrate <= 0)          /* Bitrate allocator defaults */
178                  pParam->bitrate = 900000;  
179            if (pParam->rc_bitrate <= 0)
180                    pParam->rc_bitrate = 900000;
181    
182            if (pParam->rc_reaction_delay_factor <= 0)
183                    pParam->rc_reaction_delay_factor = 16;
184    
185      if (pParam->rc_buffersize <= 0)          if (pParam->rc_averaging_period <= 0)
186                  pParam->rc_buffersize = pParam->bitrate * pParam->fbase;                  pParam->rc_averaging_period = 100;
187    
188            if (pParam->rc_buffer <= 0)
189                    pParam->rc_buffer = 100;
190    
191            /* Max and min quantizers */
192    
193      if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))      if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))
194                  pParam->min_quantizer = 1;                  pParam->min_quantizer = 1;
# Line 89  Line 196 
196      if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31))      if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31))
197                  pParam->max_quantizer = 31;                  pParam->max_quantizer = 31;
198    
     if (pParam->max_key_interval == 0)          /* 1 keyframe each 10 seconds */  
                 pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;  
   
199      if (pParam->max_quantizer < pParam->min_quantizer)      if (pParam->max_quantizer < pParam->min_quantizer)
200                  pParam->max_quantizer = pParam->min_quantizer;                  pParam->max_quantizer = pParam->min_quantizer;
201    
202      if ((pEnc = (Encoder *) malloc(sizeof(Encoder))) == NULL)          /* 1 keyframe each 10 seconds */
203    
204            if (pParam->max_key_interval == 0)
205                    pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
206    
207    
208            pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
209            if (pEnc == NULL)
210                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
211    
212            /* Zero the Encoder Structure */
213    
214            memset(pEnc, 0, sizeof(Encoder));
215    
216          /* Fill members of Encoder structure */          /* Fill members of Encoder structure */
217    
218      pEnc->mbParam.width = pParam->width;      pEnc->mbParam.width = pParam->width;
# Line 109  Line 224 
224          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;
225          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;
226    
227            pEnc->mbParam.fbase = pParam->fbase;
228            pEnc->mbParam.fincr = pParam->fincr;
229    
230            pEnc->mbParam.m_quant_type = H263_QUANT;
231    
232      pEnc->sStat.fMvPrevSigma = -1;      pEnc->sStat.fMvPrevSigma = -1;
233    
234          /* Fill rate control parameters */          /* Fill rate control parameters */
235    
236      pEnc->mbParam.quant = 4;          pEnc->bitrate = pParam->rc_bitrate;
   
         pEnc->bitrate = pParam->bitrate;  
237    
238      pEnc->iFrameNum = 0;      pEnc->iFrameNum = 0;
239      pEnc->iMaxKeyInterval = pParam->max_key_interval;      pEnc->iMaxKeyInterval = pParam->max_key_interval;
240    
241      if (image_create(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)          /* try to allocate frame memory */
242      {  
243                  free(pEnc);          pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
244                  return XVID_ERR_MEMORY;          pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
245    
246            if (pEnc->current == NULL || pEnc->reference == NULL)
247                    goto xvid_err_memory1;
248    
249            /* try to allocate mb memory */
250    
251            pEnc->current->mbs =
252                    xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
253                                            pEnc->mbParam.mb_height, CACHE_LINE);
254            pEnc->reference->mbs =
255                    xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
256                                            pEnc->mbParam.mb_height, CACHE_LINE);
257    
258            if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
259                    goto xvid_err_memory2;
260    
261            /* try to allocate image memory */
262    
263    #ifdef _DEBUG_PSNR
264            image_null(&pEnc->sOriginal);
265    #endif
266    #ifdef BFRAMES
267            image_null(&pEnc->f_refh);
268            image_null(&pEnc->f_refv);
269            image_null(&pEnc->f_refhv);
270    #endif
271            image_null(&pEnc->current->image);
272            image_null(&pEnc->reference->image);
273            image_null(&pEnc->vInterH);
274            image_null(&pEnc->vInterV);
275            image_null(&pEnc->vInterVf);
276            image_null(&pEnc->vInterHV);
277            image_null(&pEnc->vInterHVf);
278    
279    #ifdef _DEBUG_PSNR
280            if (image_create
281                    (&pEnc->sOriginal, pEnc->mbParam.edged_width,
282                     pEnc->mbParam.edged_height) < 0)
283                    goto xvid_err_memory3;
284    #endif
285    #ifdef BFRAMES
286            if (image_create
287                    (&pEnc->f_refh, pEnc->mbParam.edged_width,
288                     pEnc->mbParam.edged_height) < 0)
289                    goto xvid_err_memory3;
290            if (image_create
291                    (&pEnc->f_refv, pEnc->mbParam.edged_width,
292                     pEnc->mbParam.edged_height) < 0)
293                    goto xvid_err_memory3;
294            if (image_create
295                    (&pEnc->f_refhv, pEnc->mbParam.edged_width,
296                     pEnc->mbParam.edged_height) < 0)
297                    goto xvid_err_memory3;
298    #endif
299            if (image_create
300                    (&pEnc->current->image, pEnc->mbParam.edged_width,
301                     pEnc->mbParam.edged_height) < 0)
302                    goto xvid_err_memory3;
303            if (image_create
304                    (&pEnc->reference->image, pEnc->mbParam.edged_width,
305                     pEnc->mbParam.edged_height) < 0)
306                    goto xvid_err_memory3;
307            if (image_create
308                    (&pEnc->vInterH, pEnc->mbParam.edged_width,
309                     pEnc->mbParam.edged_height) < 0)
310                    goto xvid_err_memory3;
311            if (image_create
312                    (&pEnc->vInterV, pEnc->mbParam.edged_width,
313                     pEnc->mbParam.edged_height) < 0)
314                    goto xvid_err_memory3;
315            if (image_create
316                    (&pEnc->vInterVf, pEnc->mbParam.edged_width,
317                     pEnc->mbParam.edged_height) < 0)
318                    goto xvid_err_memory3;
319            if (image_create
320                    (&pEnc->vInterHV, pEnc->mbParam.edged_width,
321                     pEnc->mbParam.edged_height) < 0)
322                    goto xvid_err_memory3;
323            if (image_create
324                    (&pEnc->vInterHVf, pEnc->mbParam.edged_width,
325                     pEnc->mbParam.edged_height) < 0)
326                    goto xvid_err_memory3;
327    
328    
329    
330            /* B Frames specific init */
331    #ifdef BFRAMES
332    
333            pEnc->global = pParam->global;
334            pEnc->mbParam.max_bframes = pParam->max_bframes;
335            pEnc->bquant_ratio = pParam->bquant_ratio;
336            pEnc->bframes = NULL;
337    
338            if (pEnc->mbParam.max_bframes > 0) {
339                    int n;
340    
341                    pEnc->bframes =
342                            xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),
343                                                    CACHE_LINE);
344    
345                    if (pEnc->bframes == NULL)
346                            goto xvid_err_memory3;
347    
348                    for (n = 0; n < pEnc->mbParam.max_bframes; n++)
349                            pEnc->bframes[n] = NULL;
350    
351    
352                    for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
353                            pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
354    
355                            if (pEnc->bframes[n] == NULL)
356                                    goto xvid_err_memory4;
357    
358                            pEnc->bframes[n]->mbs =
359                                    xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
360                                                            pEnc->mbParam.mb_height, CACHE_LINE);
361    
362                            if (pEnc->bframes[n]->mbs == NULL)
363                                    goto xvid_err_memory4;
364    
365                            image_null(&pEnc->bframes[n]->image);
366    
367                            if (image_create
368                                    (&pEnc->bframes[n]->image, pEnc->mbParam.edged_width,
369                                     pEnc->mbParam.edged_height) < 0)
370                                    goto xvid_err_memory4;
371    
372                    }
373            }
374    
375            pEnc->bframenum_head = 0;
376            pEnc->bframenum_tail = 0;
377            pEnc->flush_bframes = 0;
378    
379            pEnc->queue = NULL;
380    
381    
382            if (pEnc->mbParam.max_bframes > 0) {
383                    int n;
384    
385                    pEnc->queue =
386                            xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE),
387                                                    CACHE_LINE);
388    
389                    if (pEnc->queue == NULL)
390                            goto xvid_err_memory4;
391    
392                    for (n = 0; n < pEnc->mbParam.max_bframes; n++)
393                            image_null(&pEnc->queue[n]);
394    
395                    for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
396                            if (image_create
397                                    (&pEnc->queue[n], pEnc->mbParam.edged_width,
398                                     pEnc->mbParam.edged_height) < 0)
399                                    goto xvid_err_memory5;
400    
401                    }
402            }
403    
404            pEnc->queue_head = 0;
405            pEnc->queue_tail = 0;
406            pEnc->queue_size = 0;
407    
408    
409            pEnc->mbParam.m_seconds = 0;
410            pEnc->mbParam.m_ticks = 0;
411            pEnc->m_framenum = 0;
412    #endif
413    
414            pParam->handle = (void *) pEnc;
415    
416            if (pParam->rc_bitrate) {
417                    RateControlInit(&pEnc->rate_control, pParam->rc_bitrate,
418                                                    pParam->rc_reaction_delay_factor,
419                                                    pParam->rc_averaging_period, pParam->rc_buffer,
420                                                    pParam->fbase * 1000 / pParam->fincr,
421                                                    pParam->max_quantizer, pParam->min_quantizer);
422      }      }
423    
424          if (image_create(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)          init_timer();
425      {  
426                  image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          return XVID_ERR_OK;
427                  free(pEnc);  
428                  return XVID_ERR_MEMORY;          /*
429             * We handle all XVID_ERR_MEMORY here, this makes the code lighter
430             */
431    #ifdef BFRAMES
432      xvid_err_memory5:
433    
434    
435            if (pEnc->mbParam.max_bframes > 0) {
436    
437                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
438                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
439                                                      pEnc->mbParam.edged_height);
440                    }
441                    xvid_free(pEnc->queue);
442      }      }
443    
444      if (image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)    xvid_err_memory4:
445      {  
446                  image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          if (pEnc->mbParam.max_bframes > 0) {
447                  image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
448                  free(pEnc);                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
449                  return XVID_ERR_MEMORY;  
450                            if (pEnc->bframes[i] == NULL)
451                                    continue;
452    
453                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
454                                                      pEnc->mbParam.edged_height);
455    
456                            xvid_free(pEnc->bframes[i]->mbs);
457    
458                            xvid_free(pEnc->bframes[i]);
459    
460      }      }
461    
462      if (image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)                  xvid_free(pEnc->bframes);
463      {          }
464                  image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
465                  image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  #endif
466                  image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
467                  free(pEnc);    xvid_err_memory3:
468    #ifdef _DEBUG_PSNR
469            image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
470                                      pEnc->mbParam.edged_height);
471    #endif
472    
473    #ifdef BFRAMES
474            image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
475                                      pEnc->mbParam.edged_height);
476            image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
477                                      pEnc->mbParam.edged_height);
478            image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
479                                      pEnc->mbParam.edged_height);
480    #endif
481    
482            image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
483                                      pEnc->mbParam.edged_height);
484            image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
485                                      pEnc->mbParam.edged_height);
486            image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
487                                      pEnc->mbParam.edged_height);
488            image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
489                                      pEnc->mbParam.edged_height);
490            image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
491                                      pEnc->mbParam.edged_height);
492            image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
493                                      pEnc->mbParam.edged_height);
494            image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
495                                      pEnc->mbParam.edged_height);
496    
497      xvid_err_memory2:
498            xvid_free(pEnc->current->mbs);
499            xvid_free(pEnc->reference->mbs);
500    
501      xvid_err_memory1:
502            xvid_free(pEnc->current);
503            xvid_free(pEnc->reference);
504            xvid_free(pEnc);
505    
506            pParam->handle = NULL;
507    
508                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
509      }      }
510    
511      if (image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)  /*****************************************************************************
512      {   * Encoder destruction
513                  image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);   *
514                  image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);   * This function destroy the entire encoder structure created by a previous
515                  image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);   * successful encoder_create call.
516                  image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);   *
517                  free(pEnc);   * Returned values (for now only one returned value) :
518                  return XVID_ERR_MEMORY;   *    - XVID_ERR_OK     - no errors
519     *
520     ****************************************************************************/
521    
522    int
523    encoder_destroy(Encoder * pEnc)
524    {
525    #ifdef BFRAMES
526            int i;
527    #endif
528    
529            ENC_CHECK(pEnc);
530    
531            /* B Frames specific */
532    #ifdef BFRAMES
533            if (pEnc->mbParam.max_bframes > 0) {
534    
535                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
536    
537                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
538                                              pEnc->mbParam.edged_height);
539                    }
540                    xvid_free(pEnc->queue);
541            }
542    
543    
544            if (pEnc->mbParam.max_bframes > 0) {
545    
546                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
547    
548                            if (pEnc->bframes[i] == NULL)
549                                    continue;
550    
551                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
552                                              pEnc->mbParam.edged_height);
553    
554                            xvid_free(pEnc->bframes[i]->mbs);
555    
556                            xvid_free(pEnc->bframes[i]);
557                    }
558    
559                    xvid_free(pEnc->bframes);
560    
561            }
562    #endif
563    
564            /* All images, reference, current etc ... */
565    
566            image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
567                                      pEnc->mbParam.edged_height);
568            image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
569                                      pEnc->mbParam.edged_height);
570            image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
571                                      pEnc->mbParam.edged_height);
572            image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
573                                      pEnc->mbParam.edged_height);
574            image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
575                                      pEnc->mbParam.edged_height);
576            image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
577                                      pEnc->mbParam.edged_height);
578            image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
579                                      pEnc->mbParam.edged_height);
580    #ifdef BFRAMES
581            image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
582                                      pEnc->mbParam.edged_height);
583            image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
584                                      pEnc->mbParam.edged_height);
585            image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
586                                      pEnc->mbParam.edged_height);
587    #endif
588    #ifdef _DEBUG_PSNR
589            image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
590                                      pEnc->mbParam.edged_height);
591    #endif
592    
593            /* Encoder structure */
594    
595            xvid_free(pEnc->current->mbs);
596            xvid_free(pEnc->current);
597    
598            xvid_free(pEnc->reference->mbs);
599            xvid_free(pEnc->reference);
600    
601            xvid_free(pEnc);
602    
603            return XVID_ERR_OK;
604      }      }
605    
606          pEnc->pMBs = malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);  
607          if (pEnc->pMBs == NULL)  #ifdef BFRAMES
608    void inc_frame_num(Encoder * pEnc)
609          {          {
610                  image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          pEnc->iFrameNum++;
611                  image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
612                  image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
613                  image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase;
614                  image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;
                 free(pEnc);  
                 return XVID_ERR_MEMORY;  
615          }          }
616    #endif
617    
618          // init macroblock array  
619          for (i = 0; i < pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; i++)  #ifdef BFRAMES
620    void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
621    {
622            if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
623          {          {
624                  pEnc->pMBs[i].dquant = NO_CHANGE;                  DPRINTF("FATAL: QUEUE FULL");
625                    return;
626          }          }
627    
628      pParam->handle = (void *)pEnc;          DPRINTF("*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
629                                    pEnc->bframenum_head, pEnc->bframenum_tail,
630                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
631    
632    
633          if (pParam->bitrate)          start_timer();
634            if (image_input
635                    (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
636                     pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
637                    return;
638            stop_conv_timer();
639    
640            pEnc->queue_size++;
641            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
642    }
643    #endif
644    
645    
646    #ifdef BFRAMES
647    /*****************************************************************************
648     * IPB frame encoder entry point
649     *
650     * Returned values :
651     *    - XVID_ERR_OK     - no errors
652     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
653     *                        format
654     ****************************************************************************/
655    
656    int
657    encoder_encode_bframes(Encoder * pEnc,
658                               XVID_ENC_FRAME * pFrame,
659                               XVID_ENC_STATS * pResult)
660          {          {
661                  RateControlInit(pParam->bitrate, pParam->rc_buffersize, pParam->fbase, pParam->width,          uint16_t x, y;
662                                                  pParam->height, pParam->max_quantizer, pParam->min_quantizer);          Bitstream bs;
663            uint32_t bits;
664    
665            int input_valid = 1;
666    
667    #ifdef _DEBUG_PSNR
668            float psnr;
669            char temp[128];
670    #endif
671    
672            ENC_CHECK(pEnc);
673            ENC_CHECK(pFrame);
674            ENC_CHECK(pFrame->image);
675    
676            start_global_timer();
677    
678            BitstreamInit(&bs, pFrame->bitstream, 0);
679    
680    ipvop_loop:
681    
682            /*
683             * bframe "flush" code
684             */
685    
686            if ((pFrame->image == NULL || pEnc->flush_bframes)
687                    && (pEnc->bframenum_head < pEnc->bframenum_tail)) {
688    
689                    if (pEnc->flush_bframes == 0) {
690                            /*
691                             * we have reached the end of stream without getting
692                             * a future reference frame... so encode last final
693                             * frame as a pframe
694                             */
695    
696                            DPRINTF("*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
697                                    pEnc->bframenum_head, pEnc->bframenum_tail,
698                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
699    
700                            pEnc->bframenum_tail--;
701                            SWAP(pEnc->current, pEnc->reference);
702    
703                            SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
704    
705                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
706    
707                            BitstreamPad(&bs);
708                            pFrame->length = BitstreamLength(&bs);
709                            pFrame->intra = 0;
710    
711                            return XVID_ERR_OK;
712                    }
713    
714    
715                    DPRINTF("*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
716                                    pEnc->bframenum_head, pEnc->bframenum_tail,
717                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
718    
719                    FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
720                    pEnc->bframenum_head++;
721    
722                    BitstreamPad(&bs);
723                    pFrame->length = BitstreamLength(&bs);
724                    pFrame->intra = 0;
725    
726                    if (input_valid)
727                            queue_image(pEnc, pFrame);
728    
729                    return XVID_ERR_OK;
730          }          }
731    
732          create_vlc_tables();          if (pEnc->bframenum_head > 0) {
733                    pEnc->bframenum_head = pEnc->bframenum_tail = 0;
734    
735                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
736    
737                            DPRINTF("*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
738                                    pEnc->bframenum_head, pEnc->bframenum_tail,
739                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
740    
741    
742                            BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
743                            BitstreamPad(&bs);
744                            BitstreamPutBits(&bs, 0x7f, 8);
745    
746                            pFrame->length = BitstreamLength(&bs);
747                            pFrame->intra = 0;
748    
749                            if (input_valid)
750                                    queue_image(pEnc, pFrame);
751    
752          return XVID_ERR_OK;          return XVID_ERR_OK;
753  }  }
754            }
755    
756    
757    bvop_loop:
758    
759  int encoder_destroy(Encoder * pEnc)          if (input_valid) {
760    
761                    SWAP(pEnc->current, pEnc->reference);
762    
763                    start_timer();
764                    if (image_input
765                            (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
766                            pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
767                            return XVID_ERR_FORMAT;
768                    stop_conv_timer();
769    
770                    // queue input frame, and dequue next image
771                    if (pEnc->queue_size > 0)
772  {  {
773      ENC_CHECK(pEnc);                          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
774      ENC_CHECK(pEnc->sCurrent.y);                          if (pEnc->queue_head != pEnc->queue_tail)
775      ENC_CHECK(pEnc->sReference.y);                          {
776                                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
777                            }
778                            pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
779                            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
780                    }
781    
782          free(pEnc->pMBs);          } else if (pEnc->queue_size > 0) {
     image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
     image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
     image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
     image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
     image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
     free(pEnc);  
783    
784          destroy_vlc_tables();                  SWAP(pEnc->current, pEnc->reference);
785    
786                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
787                    pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
788                    pEnc->queue_size--;
789    
790            } else if (BitstreamPos(&bs) == 0) {
791    
792                    DPRINTF("*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
793                                    pEnc->bframenum_head, pEnc->bframenum_tail,
794                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
795    
796    
797                    pFrame->intra = 0;
798    
799                    BitstreamPutBits(&bs, 0x7f, 8);
800                    BitstreamPad(&bs);
801                    pFrame->length = BitstreamLength(&bs);
802    
803      return XVID_ERR_OK;      return XVID_ERR_OK;
804    
805            } else {
806    
807                    pFrame->length = BitstreamLength(&bs);
808                    return XVID_ERR_OK;
809  }  }
810    
811  int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult)          pEnc->flush_bframes = 0;
812    
813            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
814             * Well there was a separation here so i put it in ANSI C
815             * comment style :-)
816             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
817    
818    //$$    SWAP(pEnc->current, pEnc->reference);
819    
820            emms();
821    
822            if (pFrame->quant == 0)
823                    pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
824            else
825                    pEnc->current->quant = pFrame->quant;
826    
827            if (pEnc->current->quant < 1)
828                    pEnc->current->quant = 1;
829    
830            if (pEnc->current->quant > 31)
831                    pEnc->current->quant = 31;
832    
833            pEnc->current->global_flags = pFrame->general;
834            pEnc->current->motion_flags = pFrame->motion;
835            pEnc->current->seconds = pEnc->mbParam.m_seconds;
836            pEnc->current->ticks = pEnc->mbParam.m_ticks;
837            /* ToDo : dynamic fcode (in both directions) */
838            pEnc->current->fcode = pEnc->mbParam.m_fcode;
839            pEnc->current->bcode = pEnc->mbParam.m_fcode;
840    
841    //$$$   start_timer();
842    //$$$   if (image_input
843    //$$$           (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
844    //$$$            pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
845    //$$$           return XVID_ERR_FORMAT;
846    //$$$   stop_conv_timer();
847    
848    #ifdef _DEBUG_PSNR
849            image_copy(&pEnc->sOriginal, &pEnc->current->image,
850                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
851    #endif
852    
853            emms();
854    
855            if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
856                    image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
857                            "%i  if:%i  st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks);
858            }
859    
860    
861            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
862             * Luminance masking
863             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
864    
865            if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
866                    int *temp_dquants =
867                            (int *) xvid_malloc(pEnc->mbParam.mb_width *
868                                                                    pEnc->mbParam.mb_height * sizeof(int),
869                                                                    CACHE_LINE);
870    
871                    pEnc->current->quant =
872                            adaptive_quantization(pEnc->current->image.y,
873                                                                      pEnc->mbParam.edged_width, temp_dquants,
874                                                                      pEnc->current->quant, pEnc->current->quant,
875                                                                      2 * pEnc->current->quant,
876                                                                      pEnc->mbParam.mb_width,
877                                                                      pEnc->mbParam.mb_height);
878    
879                    for (y = 0; y < pEnc->mbParam.mb_height; y++) {
880    
881    #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
882    
883                            for (x = 0; x < pEnc->mbParam.mb_width; x++) {
884                                    MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
885    
886                                    pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
887                            }
888    
889    #undef OFFSET
890    
891                    }
892    
893                    xvid_free(temp_dquants);
894            }
895    
896            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
897             * ivop/pvop/bvop selection
898             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
899    
900    
901            if (pEnc->iFrameNum == 0 || pFrame->intra == 1 ||
902                    (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&
903                     pEnc->iFrameNum >= pEnc->iMaxKeyInterval)
904                    || image_mad(&pEnc->reference->image, &pEnc->current->image,
905                                             pEnc->mbParam.edged_width, pEnc->mbParam.width,
906                                             pEnc->mbParam.height) > 30) {
907                    /*
908                     * This will be coded as an Intra Frame
909                     */
910    
911                    DPRINTF("*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
912                                    pEnc->bframenum_head, pEnc->bframenum_tail,
913                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
914    
915                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
916                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
917                    }
918    
919                    FrameCodeI(pEnc, &bs, &bits);
920    
921                    pFrame->intra = 1;
922                    pEnc->flush_bframes = 1;
923    
924                    inc_frame_num(pEnc);
925    
926                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
927                            BitstreamPad(&bs);
928                            input_valid = 0;
929                            goto ipvop_loop;
930                    }
931    
932                    /*
933                     * NB : sequences like "IIBB" decode fine with msfdam but,
934                     *      go screwy with divx 5.00
935                     */
936            } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
937                    /*
938                     * This will be coded as a Predicted Frame
939                     */
940    
941                    DPRINTF("*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
942                                    pEnc->bframenum_head, pEnc->bframenum_tail,
943                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
944    
945                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
946                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
947                    }
948    
949                    FrameCodeP(pEnc, &bs, &bits, 1, 0);
950                    pFrame->intra = 0;
951                    pEnc->flush_bframes = 1;
952    
953                    inc_frame_num(pEnc);
954    
955                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
956                            BitstreamPad(&bs);
957                            input_valid = 0;
958                            goto ipvop_loop;
959                    }
960    
961            } else {
962                    /*
963                     * This will be coded as a Bidirectional Frame
964                     */
965    
966                    DPRINTF("*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
967                                    pEnc->bframenum_head, pEnc->bframenum_tail,
968                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
969    
970                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
971                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
972                    }
973    
974                    if (pFrame->bquant < 1) {
975                            pEnc->current->quant =
976                                    ((pEnc->reference->quant +
977                                      pEnc->current->quant) * pEnc->bquant_ratio) / 200;
978                    } else {
979                            pEnc->current->quant = pFrame->bquant;
980                    }
981    
982                    /* store frame into bframe buffer & swap ref back to current */
983                    SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
984                    SWAP(pEnc->current, pEnc->reference);
985    
986                    pEnc->bframenum_tail++;
987    
988                    pFrame->intra = 0;
989                    pFrame->length = 0;
990    
991                    inc_frame_num(pEnc);
992    
993                    input_valid = 0;
994                    goto bvop_loop;
995            }
996    
997            BitstreamPad(&bs);
998            pFrame->length = BitstreamLength(&bs);
999    
1000            if (pResult) {
1001                    pResult->quant = pEnc->current->quant;
1002                    pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
1003                    pResult->kblks = pEnc->sStat.kblks;
1004                    pResult->mblks = pEnc->sStat.mblks;
1005                    pResult->ublks = pEnc->sStat.ublks;
1006            }
1007    
1008            emms();
1009    
1010    #ifdef _DEBUG_PSNR
1011            psnr =
1012                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1013                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
1014                                       pEnc->mbParam.height);
1015    
1016            snprintf(temp, 127, "PSNR: %f\n", psnr);
1017            DEBUG(temp);
1018    #endif
1019    
1020            if (pFrame->quant == 0) {
1021                    RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1022                                                      pFrame->length, pFrame->intra);
1023            }
1024    
1025    
1026            stop_global_timer();
1027            write_timer();
1028    
1029            return XVID_ERR_OK;
1030    }
1031    
1032    #endif
1033    
1034    
1035    
1036    /*****************************************************************************
1037     * "original" IP frame encoder entry point
1038     *
1039     * Returned values :
1040     *    - XVID_ERR_OK     - no errors
1041     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
1042     *                        format
1043     ****************************************************************************/
1044    
1045    int
1046    encoder_encode(Encoder * pEnc,
1047                               XVID_ENC_FRAME * pFrame,
1048                               XVID_ENC_STATS * pResult)
1049  {  {
1050      uint16_t x, y;      uint16_t x, y;
1051      Bitstream bs;      Bitstream bs;
1052      uint32_t bits;      uint32_t bits;
         uint16_t quant_type = 0;  
1053          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
1054    
1055      IMAGE *pCurrent = &(pEnc->sCurrent);  #ifdef _DEBUG_PSNR
1056            float psnr;
1057            uint8_t temp[128];
1058    #endif
1059    
1060          start_global_timer();          start_global_timer();
1061    
# Line 228  Line 1064 
1064      ENC_CHECK(pFrame->bitstream);      ENC_CHECK(pFrame->bitstream);
1065      ENC_CHECK(pFrame->image);      ENC_CHECK(pFrame->image);
1066    
1067          pEnc->mbParam.global_flags = pFrame->general;          SWAP(pEnc->current, pEnc->reference);
1068          pEnc->mbParam.motion_flags = pFrame->motion;  
1069            pEnc->current->global_flags = pFrame->general;
1070            pEnc->current->motion_flags = pFrame->motion;
1071    #ifdef BFRAMES
1072            pEnc->current->seconds = pEnc->mbParam.m_seconds;
1073            pEnc->current->ticks = pEnc->mbParam.m_ticks;
1074    #endif
1075            pEnc->mbParam.hint = &pFrame->hint;
1076    
1077          start_timer();          start_timer();
1078          if (image_input(&pEnc->sCurrent, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width,          if (image_input
1079                                          pFrame->image, pFrame->colorspace))                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
1080          {                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0)
1081                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
         }  
1082          stop_conv_timer();          stop_conv_timer();
1083    
1084    #ifdef _DEBUG_PSNR
1085            image_copy(&pEnc->sOriginal, &pEnc->current->image,
1086                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
1087    #endif
1088    
1089            emms();
1090    
1091      BitstreamInit(&bs, pFrame->bitstream, 0);      BitstreamInit(&bs, pFrame->bitstream, 0);
1092    
1093          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
1094          {                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
1095                  pEnc->mbParam.quant = RateControlGetQ(0);          } else {
1096          }                  pEnc->current->quant = pFrame->quant;
         else  
         {  
                 pEnc->mbParam.quant = pFrame->quant;  
1097          }          }
1098    
1099          if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0)          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1100          {                  int *temp_dquants =
1101                  int * temp_dquants = (int *) malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int));                          (int *) xvid_malloc(pEnc->mbParam.mb_width *
1102                                                                    pEnc->mbParam.mb_height * sizeof(int),
1103                                                                    CACHE_LINE);
1104    
1105                  pEnc->mbParam.quant = adaptive_quantization(pEnc->sCurrent.y, pEnc->mbParam.width,                  pEnc->current->quant =
1106                          temp_dquants, pFrame->quant, pFrame->quant,                          adaptive_quantization(pEnc->current->image.y,
1107                          2*pFrame->quant, pEnc->mbParam.mb_width, pEnc->mbParam.mb_height);                                                                    pEnc->mbParam.edged_width, temp_dquants,
1108                                                                      pEnc->current->quant, pEnc->current->quant,
1109                                                                      2 * pEnc->current->quant,
1110                                                                      pEnc->mbParam.mb_width,
1111                                                                      pEnc->mbParam.mb_height);
1112    
1113                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                  for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1114                          for (x = 0; x < pEnc->mbParam.mb_width; x++)  
1115                          {  #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
1116                                  MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];  
1117                                  pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)];                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1118    
1119    
1120                                    MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
1121    
1122                                    pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
1123                          }                          }
1124                  free(temp_dquants);  
1125    #undef OFFSET
1126          }          }
1127    
1128          if(pEnc->mbParam.global_flags & XVID_H263QUANT)                  xvid_free(temp_dquants);
1129                  quant_type = H263_QUANT;          }
         else if(pEnc->mbParam.global_flags & XVID_MPEGQUANT)  
                 quant_type = MPEG4_QUANT;  
1130    
1131          if(pEnc->mbParam.quant_type != quant_type) {          if (pEnc->current->global_flags & XVID_H263QUANT) {
1132                  pEnc->mbParam.quant_type = quant_type;                  if (pEnc->mbParam.m_quant_type != H263_QUANT)
1133                  write_vol_header = 1;                  write_vol_header = 1;
1134          }                  pEnc->mbParam.m_quant_type = H263_QUANT;
1135          else          } else if (pEnc->current->global_flags & XVID_MPEGQUANT) {
1136                  write_vol_header = 0;                  int matrix1_changed, matrix2_changed;
1137    
1138          if ((pEnc->mbParam.global_flags & XVID_CUSTOM_QMATRIX) > 0)                  matrix1_changed = matrix2_changed = 0;
1139          {  
1140                  int ret1, ret2;                  if (pEnc->mbParam.m_quant_type != MPEG4_QUANT)
1141                  ret1 = set_intra_matrix(pFrame->quant_intra_matrix);                          write_vol_header = 1;
1142                  ret2 = set_inter_matrix(pFrame->quant_inter_matrix);  
1143                    pEnc->mbParam.m_quant_type = MPEG4_QUANT;
1144    
1145                    if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1146                            if (pFrame->quant_intra_matrix != NULL)
1147                                    matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix);
1148                            if (pFrame->quant_inter_matrix != NULL)
1149                                    matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix);
1150                    } else {
1151                            matrix1_changed = set_intra_matrix(get_default_intra_matrix());
1152                            matrix2_changed = set_inter_matrix(get_default_inter_matrix());
1153                    }
1154                  if(write_vol_header == 0)                  if(write_vol_header == 0)
1155                          write_vol_header = ret1 | ret2;                          write_vol_header = matrix1_changed | matrix2_changed;
1156          }          }
1157    
1158          if (pFrame->intra < 0)          if (pFrame->intra < 0) {
1159      {                  if ((pEnc->iFrameNum == 0)
1160                  if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0)                          || ((pEnc->iMaxKeyInterval > 0)
1161                          && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval)))                                  && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {
   
1162                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1163                  else                  } else {
1164                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
1165      }      }
1166      else          } else {
1167      {                  if (pFrame->intra == 1) {
                 if (pFrame->intra == 1)  
1168                      pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                      pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1169                  else                  } else {
1170                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);
1171      }      }
1172    
1173            }
1174    
1175          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1176      BitstreamPutBits(&bs, 0xFFFF, 16);      BitstreamPutBits(&bs, 0xFFFF, 16);
1177      BitstreamPad(&bs);      BitstreamPad(&bs);
1178      pFrame->length = BitstreamLength(&bs);      pFrame->length = BitstreamLength(&bs);
1179    
1180          if (pResult)          if (pResult) {
1181      {                  pResult->quant = pEnc->current->quant;
                 pResult->quant = pEnc->mbParam.quant;  
1182                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
1183                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->sStat.kblks;
1184                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->sStat.mblks;
1185                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
1186      }      }
1187    
1188      if (pEnc->bitrate)          emms();
         {  
                 RateControlUpdate(pEnc->mbParam.quant, pFrame->length, pFrame->intra);  
         }  
1189    
1190            if (pFrame->quant == 0) {
1191                    RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1192                                                      pFrame->length, pFrame->intra);
1193            }
1194    #ifdef _DEBUG_PSNR
1195            psnr =
1196                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1197                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
1198                                       pEnc->mbParam.height);
1199    
1200            snprintf(temp, 127, "PSNR: %f\n", psnr);
1201            DEBUG(temp);
1202    #endif
1203    
1204    #ifdef BFRAMES
1205            inc_frame_num(pEnc);
1206    #else
1207          pEnc->iFrameNum++;          pEnc->iFrameNum++;
1208      image_swap(&pEnc->sCurrent, &pEnc->sReference);  #endif
1209    
1210    
1211          stop_global_timer();          stop_global_timer();
1212          write_timer();          write_timer();
# Line 334  Line 1215 
1215  }  }
1216    
1217    
1218  static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) {  static __inline void
1219    CodeIntraMB(Encoder * pEnc,
1220                            MACROBLOCK * pMB)
1221    {
1222    
1223          pMB->mode = MODE_INTRA;          pMB->mode = MODE_INTRA;
1224    
1225          if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) {          /* zero mv statistics */
1226                  if(pMB->dquant != NO_CHANGE)          pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0;
1227                  {          pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0;
1228            pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;
1229            pMB->sad16 = 0;
1230    
1231            if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1232                    if (pMB->dquant != NO_CHANGE) {
1233                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
1234                          pEnc->mbParam.quant += DQtab[pMB->dquant];                          pEnc->current->quant += DQtab[pMB->dquant];
1235    
1236                            if (pEnc->current->quant > 31)
1237                                    pEnc->current->quant = 31;
1238                            if (pEnc->current->quant < 1)
1239                                    pEnc->current->quant = 1;
1240                    }
1241            }
1242    
1243            pMB->quant = pEnc->current->quant;
1244    }
1245    
1246    
1247    #define FCODEBITS       3
1248    #define MODEBITS        5
1249    
1250    void
1251    HintedMESet(Encoder * pEnc,
1252                            int *intra)
1253    {
1254            HINTINFO *hint;
1255            Bitstream bs;
1256            int length, high;
1257            uint32_t x, y;
1258    
1259            hint = pEnc->mbParam.hint;
1260    
1261            if (hint->rawhints) {
1262                    *intra = hint->mvhint.intra;
1263            } else {
1264                    BitstreamInit(&bs, hint->hintstream, hint->hintlength);
1265                    *intra = BitstreamGetBit(&bs);
1266            }
1267    
1268            if (*intra) {
1269                    return;
1270            }
1271    
1272            pEnc->current->fcode =
1273                    (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs,
1274                                                                                                                                     FCODEBITS);
1275    
1276            length = pEnc->current->fcode + 5;
1277            high = 1 << (length - 1);
1278    
1279            for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1280                    for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1281                            MACROBLOCK *pMB =
1282                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1283                            MVBLOCKHINT *bhint =
1284                                    &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1285                            VECTOR pred[4];
1286                            VECTOR tmp;
1287                            int32_t dummy[4];
1288                            int vec;
1289    
1290                            pMB->mode =
1291                                    (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs,
1292                                                                                                                                      MODEBITS);
1293    
1294                            pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
1295                            pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
1296    
1297                            if (pMB->mode == MODE_INTER) {
1298                                    tmp.x =
1299                                            (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs,
1300                                                                                                                                                      length);
1301                                    tmp.y =
1302                                            (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs,
1303                                                                                                                                                      length);
1304                                    tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1305                                    tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1306    
1307                                    get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width,
1308                                                            0, pred, dummy);
1309    
1310                                    for (vec = 0; vec < 4; ++vec) {
1311                                            pMB->mvs[vec].x = tmp.x;
1312                                            pMB->mvs[vec].y = tmp.y;
1313                                            pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;
1314                                            pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;
1315                                    }
1316                            } else if (pMB->mode == MODE_INTER4V) {
1317                                    for (vec = 0; vec < 4; ++vec) {
1318                                            tmp.x =
1319                                                    (hint->rawhints) ? bhint->mvs[vec].
1320                                                    x : BitstreamGetBits(&bs, length);
1321                                            tmp.y =
1322                                                    (hint->rawhints) ? bhint->mvs[vec].
1323                                                    y : BitstreamGetBits(&bs, length);
1324                                            tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1325                                            tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1326    
1327                                            get_pmvdata(pEnc->current->mbs, x, y,
1328                                                                    pEnc->mbParam.mb_width, vec, pred, dummy);
1329    
1330                                            pMB->mvs[vec].x = tmp.x;
1331                                            pMB->mvs[vec].y = tmp.y;
1332                                            pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;
1333                                            pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;
1334                                    }
1335                            } else                          // intra / stuffing / not_coded
1336                            {
1337                                    for (vec = 0; vec < 4; ++vec) {
1338                                            pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
1339                                    }
1340                            }
1341    
1342                            if (pMB->mode == MODE_INTER4V &&
1343                                    (pEnc->current->global_flags & XVID_LUMIMASKING)
1344                                    && pMB->dquant != NO_CHANGE) {
1345                                    pMB->mode = MODE_INTRA;
1346    
1347                                    for (vec = 0; vec < 4; ++vec) {
1348                                            pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
1349                                    }
1350                            }
1351                    }
1352            }
1353    }
1354    
1355    
1356    void
1357    HintedMEGet(Encoder * pEnc,
1358                            int intra)
1359    {
1360            HINTINFO *hint;
1361            Bitstream bs;
1362            uint32_t x, y;
1363            int length, high;
1364    
1365            hint = pEnc->mbParam.hint;
1366    
1367            if (hint->rawhints) {
1368                    hint->mvhint.intra = intra;
1369            } else {
1370                    BitstreamInit(&bs, hint->hintstream, 0);
1371                    BitstreamPutBit(&bs, intra);
1372            }
1373    
1374            if (intra) {
1375                    if (!hint->rawhints) {
1376                            BitstreamPad(&bs);
1377                            hint->hintlength = BitstreamLength(&bs);
1378                    }
1379                    return;
1380            }
1381    
1382                          if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31;          length = pEnc->current->fcode + 5;
1383                          if (pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1;          high = 1 << (length - 1);
1384    
1385            if (hint->rawhints) {
1386                    hint->mvhint.fcode = pEnc->current->fcode;
1387            } else {
1388                    BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);
1389                  }                  }
1390    
1391            for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1392                    for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1393                            MACROBLOCK *pMB =
1394                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1395                            MVBLOCKHINT *bhint =
1396                                    &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1397                            VECTOR tmp;
1398    
1399                            if (hint->rawhints) {
1400                                    bhint->mode = pMB->mode;
1401                            } else {
1402                                    BitstreamPutBits(&bs, pMB->mode, MODEBITS);
1403          }          }
1404    
1405          pMB->quant = pEnc->mbParam.quant;                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
1406                                    tmp.x = pMB->mvs[0].x;
1407                                    tmp.y = pMB->mvs[0].y;
1408                                    tmp.x += (tmp.x < 0) ? high * 2 : 0;
1409                                    tmp.y += (tmp.y < 0) ? high * 2 : 0;
1410    
1411                                    if (hint->rawhints) {
1412                                            bhint->mvs[0].x = tmp.x;
1413                                            bhint->mvs[0].y = tmp.y;
1414                                    } else {
1415                                            BitstreamPutBits(&bs, tmp.x, length);
1416                                            BitstreamPutBits(&bs, tmp.y, length);
1417  }  }
1418                            } else if (pMB->mode == MODE_INTER4V) {
1419                                    int vec;
1420    
1421                                    for (vec = 0; vec < 4; ++vec) {
1422                                            tmp.x = pMB->mvs[vec].x;
1423                                            tmp.y = pMB->mvs[vec].y;
1424                                            tmp.x += (tmp.x < 0) ? high * 2 : 0;
1425                                            tmp.y += (tmp.y < 0) ? high * 2 : 0;
1426    
1427  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits)                                          if (hint->rawhints) {
1428                                                    bhint->mvs[vec].x = tmp.x;
1429                                                    bhint->mvs[vec].y = tmp.y;
1430                                            } else {
1431                                                    BitstreamPutBits(&bs, tmp.x, length);
1432                                                    BitstreamPutBits(&bs, tmp.y, length);
1433                                            }
1434                                    }
1435                            }
1436                    }
1437            }
1438    
1439            if (!hint->rawhints) {
1440                    BitstreamPad(&bs);
1441                    hint->hintlength = BitstreamLength(&bs);
1442            }
1443    }
1444    
1445    
1446    static int
1447    FrameCodeI(Encoder * pEnc,
1448                       Bitstream * bs,
1449                       uint32_t * pBits)
1450  {  {
1451      int16_t dct_codes[6][64];  
1452      int16_t qcoeff[6][64];          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1453            DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1454    
1455      uint16_t x, y;      uint16_t x, y;
     IMAGE *pCurrent = &pEnc->sCurrent;  
1456    
1457      pEnc->iFrameNum = 0;      pEnc->iFrameNum = 0;
1458      pEnc->mbParam.rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1459      pEnc->mbParam.coding_type = I_VOP;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1460            pEnc->current->coding_type = I_VOP;
1461          BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);  
1462          BitstreamWriteVopHeader(bs, I_VOP, pEnc->mbParam.rounding_type,          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1463                          pEnc->mbParam.quant,  #ifdef BFRAMES
1464                          pEnc->mbParam.fixed_code);  #define DIVX501B481P "DivX501b481p"
1465            if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1466                    BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));
1467            }
1468    #endif
1469            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1470    
1471      *pBits = BitstreamPos(bs);      *pBits = BitstreamPos(bs);
1472    
# Line 376  Line 1475 
1475          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1476    
1477      for (y = 0; y < pEnc->mbParam.mb_height; y++)      for (y = 0; y < pEnc->mbParam.mb_height; y++)
1478                  for (x = 0; x < pEnc->mbParam.mb_width; x++)                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1479                  {                          MACROBLOCK *pMB =
1480                      MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1481    
1482                          CodeIntraMB(pEnc, pMB);                          CodeIntraMB(pEnc, pMB);
1483    
1484                          MBTransQuantIntra(&pEnc->mbParam, x, y, dct_codes, qcoeff, pCurrent);                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1485                                                              dct_codes, qcoeff);
1486    
1487                          start_timer();                          start_timer();
1488                          MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1489                          stop_prediction_timer();                          stop_prediction_timer();
1490    
1491                          start_timer();                          start_timer();
1492                          MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1493                          stop_coding_timer();                          stop_coding_timer();
1494                  }                  }
1495    
# Line 399  Line 1499 
1499      pEnc->sStat.fMvPrevSigma = -1;      pEnc->sStat.fMvPrevSigma = -1;
1500      pEnc->sStat.iMvSum = 0;      pEnc->sStat.iMvSum = 0;
1501      pEnc->sStat.iMvCount = 0;      pEnc->sStat.iMvCount = 0;
1502      pEnc->mbParam.fixed_code = 2;          pEnc->mbParam.m_fcode = 2;
1503    
1504            if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1505                    HintedMEGet(pEnc, 1);
1506            }
1507    
1508      return 1;                                    // intra      return 1;                                    // intra
1509  }  }
# Line 407  Line 1511 
1511    
1512  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1513    
1514  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header)  static int
1515    FrameCodeP(Encoder * pEnc,
1516                       Bitstream * bs,
1517                       uint32_t * pBits,
1518                       bool force_inter,
1519                       bool vol_header)
1520  {  {
1521      float fSigma;      float fSigma;
1522      int16_t dct_codes[6][64];  
1523      int16_t qcoeff[6][64];          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1524            DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1525    
1526          int iLimit;          int iLimit;
1527      uint32_t x, y;      uint32_t x, y;
1528      int iSearchRange;      int iSearchRange;
1529          bool bIntra;          int bIntra;
1530    
1531      IMAGE *pCurrent = &pEnc->sCurrent;          /* IMAGE *pCurrent = &pEnc->current->image; */
1532      IMAGE *pRef = &pEnc->sReference;          IMAGE *pRef = &pEnc->reference->image;
1533    
1534          image_setedges(pRef,pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height);          start_timer();
1535            image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1536      pEnc->mbParam.rounding_type = 1 - pEnc->mbParam.rounding_type;                                     pEnc->mbParam.width, pEnc->mbParam.height,
1537                                       pEnc->current->global_flags & XVID_INTERLACING);
1538            stop_edges_timer();
1539    
1540            pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
1541            pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1542            pEnc->current->fcode = pEnc->mbParam.m_fcode;
1543    
1544          if (!force_inter)          if (!force_inter)
1545                  iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD);                  iLimit =
1546                            (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height *
1547                                       INTRA_THRESHOLD);
1548      else      else
1549                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;
1550    
1551          if ((pEnc->mbParam.global_flags & XVID_HALFPEL) > 0) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
1552                  start_timer();                  start_timer();
1553                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1554                          pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1555                          pEnc->mbParam.rounding_type);                                                    pEnc->mbParam.edged_height,
1556                                                      pEnc->current->rounding_type);
1557                  stop_inter_timer();                  stop_inter_timer();
1558          }          }
1559    
1560          start_timer();          start_timer();
1561          bIntra = MotionEstimation(pEnc->pMBs, &pEnc->mbParam, &pEnc->sReference,          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
1562                                  &pEnc->vInterH, &pEnc->vInterV,                  HintedMESet(pEnc, &bIntra);
1563                                  &pEnc->vInterHV, &pEnc->sCurrent, iLimit);          } else {
1564                    bIntra =
1565                            MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1566                                                             &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1567                                                             iLimit);
1568            }
1569          stop_motion_timer();          stop_motion_timer();
1570    
1571          if (bIntra == 1)          if (bIntra == 1) {
1572                  return FrameCodeI(pEnc, bs, pBits);                  return FrameCodeI(pEnc, bs, pBits);
1573            }
1574    
1575      pEnc->mbParam.coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
1576    
1577          if(vol_header)          if(vol_header)
1578                  BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1579    
1580      BitstreamWriteVopHeader(bs, P_VOP, pEnc->mbParam.rounding_type,          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
                          pEnc->mbParam.quant,  
                          pEnc->mbParam.fixed_code);  
1581    
1582      *pBits = BitstreamPos(bs);      *pBits = BitstreamPos(bs);
1583    
# Line 462  Line 1586 
1586      pEnc->sStat.iMvCount = 0;      pEnc->sStat.iMvCount = 0;
1587          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1588    
1589      for(y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1590          {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1591                  for(x = 0; x < pEnc->mbParam.mb_width; x++)                          MACROBLOCK *pMB =
1592                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
                         MACROBLOCK * pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];  
1593    
1594                      bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                      bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1595    
1596                          if (!bIntra)                          if (!bIntra) {
                     {  
1597                                  start_timer();                                  start_timer();
1598                                  MBMotionCompensation(pMB, x, y, &pEnc->sReference,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1599                                          &pEnc->vInterH, &pEnc->vInterV,                                          &pEnc->vInterH, &pEnc->vInterV,
1600                                          &pEnc->vInterHV, &pEnc->sCurrent, dct_codes,                                                                           &pEnc->vInterHV, &pEnc->current->image,
1601                                          pEnc->mbParam.width,                                                                           dct_codes, pEnc->mbParam.width,
1602                                          pEnc->mbParam.height,                                          pEnc->mbParam.height,
1603                                          pEnc->mbParam.edged_width,                                          pEnc->mbParam.edged_width,
1604                                          pEnc->mbParam.rounding_type);                                                                           pEnc->current->rounding_type);
1605                                  stop_comp_timer();                                  stop_comp_timer();
1606    
1607                                  if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) {                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1608                                          if(pMB->dquant != NO_CHANGE) {                                          if(pMB->dquant != NO_CHANGE) {
1609                                                  pMB->mode = MODE_INTER_Q;                                                  pMB->mode = MODE_INTER_Q;
1610                                                  pEnc->mbParam.quant += DQtab[pMB->dquant];                                                  pEnc->current->quant += DQtab[pMB->dquant];
1611                                                  if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31;                                                  if (pEnc->current->quant > 31)
1612                                                  else if(pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1;                                                          pEnc->current->quant = 31;
1613                                                    else if (pEnc->current->quant < 1)
1614                                                            pEnc->current->quant = 1;
1615                                          }                                          }
1616                                  }                                  }
1617                                  pMB->quant = pEnc->mbParam.quant;                                  pMB->quant = pEnc->current->quant;
1618    
1619                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, x, y, dct_codes, qcoeff, pCurrent);                                  pMB->field_pred = 0;
1620                      }  
1621                          else                                  pMB->cbp =
1622                          {                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1623                                                                              dct_codes, qcoeff);
1624                            } else {
1625                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
1626                                  MBTransQuantIntra(&pEnc->mbParam, x, y, dct_codes, qcoeff, pCurrent);                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1627                                                                      dct_codes, qcoeff);
1628                          }                          }
1629    
1630                      start_timer();                      start_timer();
1631                          MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1632                          stop_prediction_timer();                          stop_prediction_timer();
1633    
1634                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
                         {  
1635                                  pEnc->sStat.kblks++;                                  pEnc->sStat.kblks++;
1636                          }                          } else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1637                          else if (pMB->cbp ||                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1638                                          pMB->mvs[0].x || pMB->mvs[0].y ||                                             pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].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)  
                         {  
1639                                  pEnc->sStat.mblks++;                                  pEnc->sStat.mblks++;
1640                          }                          } else {
                         else  
                         {  
1641                                  pEnc->sStat.ublks++;                                  pEnc->sStat.ublks++;
1642                          }                          }
1643    
1644                          start_timer();                          start_timer();
1645                          MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1646                          stop_coding_timer();                          stop_coding_timer();
1647                  }                  }
1648          }          }
1649    
1650          emms();          emms();
1651    
1652            if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1653                    HintedMEGet(pEnc, 0);
1654            }
1655    
1656          if (pEnc->sStat.iMvCount == 0)          if (pEnc->sStat.iMvCount == 0)
1657                  pEnc->sStat.iMvCount = 1;                  pEnc->sStat.iMvCount = 1;
1658    
1659      fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);      fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);
1660    
1661      iSearchRange = 1 << (3 + pEnc->mbParam.fixed_code);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
1662    
1663      if ((fSigma > iSearchRange / 3)      if ((fSigma > iSearchRange / 3)
1664                  && (pEnc->mbParam.fixed_code <= 3))     // maximum search range 128                  && (pEnc->mbParam.m_fcode <= 3))        // maximum search range 128
1665      {      {
1666                  pEnc->mbParam.fixed_code++;                  pEnc->mbParam.m_fcode++;
1667                  iSearchRange *= 2;                  iSearchRange *= 2;
1668      }          } else if ((fSigma < iSearchRange / 6)
     else if ((fSigma < iSearchRange / 6)  
1669              && (pEnc->sStat.fMvPrevSigma >= 0)              && (pEnc->sStat.fMvPrevSigma >= 0)
1670              && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)              && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)
1671              && (pEnc->mbParam.fixed_code >= 2)) // minimum search range 16                             && (pEnc->mbParam.m_fcode >= 2))     // minimum search range 16
1672      {      {
1673                  pEnc->mbParam.fixed_code--;                  pEnc->mbParam.m_fcode--;
1674                  iSearchRange /= 2;                  iSearchRange /= 2;
1675      }      }
1676    
# Line 557  Line 1680 
1680    
1681      return 0;                                    // inter      return 0;                                    // inter
1682  }  }
1683    
1684    
1685    #ifdef BFRAMES
1686    static void
1687    FrameCodeB(Encoder * pEnc,
1688                       FRAMEINFO * frame,
1689                       Bitstream * bs,
1690                       uint32_t * pBits)
1691    {
1692            int16_t dct_codes[6 * 64];
1693            int16_t qcoeff[6 * 64];
1694            uint32_t x, y;
1695            VECTOR forward;
1696            VECTOR backward;
1697    
1698            IMAGE *f_ref = &pEnc->reference->image;
1699            IMAGE *b_ref = &pEnc->current->image;
1700    
1701            // forward
1702            image_setedges(f_ref, pEnc->mbParam.edged_width,
1703                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1704                                       pEnc->mbParam.height,
1705                                       frame->global_flags & XVID_INTERLACING);
1706            start_timer();
1707            image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1708                                              pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1709                                              0);
1710            stop_inter_timer();
1711    
1712            // backward
1713            image_setedges(b_ref, pEnc->mbParam.edged_width,
1714                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1715                                       pEnc->mbParam.height,
1716                                       frame->global_flags & XVID_INTERLACING);
1717            start_timer();
1718            image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1719                                              pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1720                                              0);
1721            stop_inter_timer();
1722    
1723            start_timer();
1724            MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref,
1725                                                     &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1726                                                     pEnc->current->mbs, b_ref, &pEnc->vInterH,
1727                                                     &pEnc->vInterV, &pEnc->vInterHV);
1728    
1729    
1730            stop_motion_timer();
1731    
1732            /*if (test_quant_type(&pEnc->mbParam, pEnc->current))
1733               {
1734               BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);
1735               } */
1736    
1737            frame->coding_type = B_VOP;
1738            BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1739    
1740            *pBits = BitstreamPos(bs);
1741    
1742            pEnc->sStat.iTextBits = 0;
1743            pEnc->sStat.iMvSum = 0;
1744            pEnc->sStat.iMvCount = 0;
1745            pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1746    
1747    
1748            for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1749                    // reset prediction
1750    
1751                    forward.x = 0;
1752                    forward.y = 0;
1753                    backward.x = 0;
1754                    backward.y = 0;
1755    
1756                    for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1757                            MACROBLOCK *f_mb =
1758                                    &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];
1759                            MACROBLOCK *b_mb =
1760                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1761                            MACROBLOCK *mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1762    
1763                            // decoder ignores mb when refence block is INTER(0,0), CBP=0
1764                            if (mb->mode == MODE_NOT_CODED) {
1765                                    mb->mvs[0].x = 0;
1766                                    mb->mvs[0].y = 0;
1767                                    continue;
1768                            }
1769    
1770                            MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1771                                                                             f_ref, &pEnc->f_refh, &pEnc->f_refv,
1772                                                                             &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1773                                                                             &pEnc->vInterV, &pEnc->vInterHV,
1774                                                                             dct_codes);
1775    
1776                            mb->quant = frame->quant;
1777                            mb->cbp =
1778                                    MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes,
1779                                                                      qcoeff);
1780                            //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant);
1781    
1782    
1783                            if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT)
1784                                    && mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) {
1785                                    mb->mode = 5;   // skipped
1786                            }
1787    
1788                            if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {
1789                                    mb->pmvs[0].x = mb->mvs[0].x - forward.x;
1790                                    mb->pmvs[0].y = mb->mvs[0].y - forward.y;
1791                                    forward.x = mb->mvs[0].x;
1792                                    forward.y = mb->mvs[0].y;
1793                            }
1794    
1795                            if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) {
1796                                    mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x;
1797                                    mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y;
1798                                    backward.x = mb->b_mvs[0].x;
1799                                    backward.y = mb->b_mvs[0].y;
1800                            }
1801    //                      DPRINTF("%05i : [%i %i] M=%i CBP=%i MVS=%i,%i forward=%i,%i", pEnc->m_framenum, x, y, mb->mode, mb->cbp, mb->mvs[0].x, mb->mvs[0].y, forward.x, forward.y);
1802    
1803                            start_timer();
1804                            MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1805                                                     &pEnc->sStat);
1806                            stop_coding_timer();
1807                    }
1808            }
1809    
1810            emms();
1811    
1812            // TODO: dynamic fcode/bcode ???
1813    
1814            *pBits = BitstreamPos(bs) - *pBits;
1815    }
1816    #endif

Legend:
Removed from v.4  
changed lines
  Added in v.236

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