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

Diff of /xvidcore/src/encoder.c

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

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

Legend:
Removed from v.1.17  
changed lines
  Added in v.1.53

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