[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.27, Thu Apr 25 06:55:00 2002 UTC revision 1.40, Sun Jun 9 23:30:50 2002 UTC
# Line 1  Line 1 
1  // 14.04.2002   added FrameCodeB()  /*****************************************************************************
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     *  08.05.2002 fix some problem in DEBUG mode;
36     *             MinChen <chenm001@163.com>
37     *  14.04.2002 added FrameCodeB()
38     *
39     *  $Id$
40     *
41     ****************************************************************************/
42    
43  #include <stdlib.h>  #include <stdlib.h>
44  #include <stdio.h>  #include <stdio.h>
# Line 9  Line 49 
49  #include "global.h"  #include "global.h"
50  #include "utils/timer.h"  #include "utils/timer.h"
51  #include "image/image.h"  #include "image/image.h"
52    #include "motion/motion.h"
53  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
54  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
55  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
# Line 20  Line 61 
61  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
62  #include "utils/mem_align.h"  #include "utils/mem_align.h"
63    
64    /*****************************************************************************
65     * Local macros
66     ****************************************************************************/
67    
68  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT
69  #define SWAP(A,B)    { void * tmp = A; A = B; B = tmp; }  #define SWAP(A,B)    { void * tmp = A; A = B; B = tmp; }
70    
71    /*****************************************************************************
72     * Local function prototypes
73     ****************************************************************************/
74    
75    static int FrameCodeI(Encoder * pEnc,
76                          Bitstream * bs,
77                          uint32_t *pBits);
78    
79    static int FrameCodeP(Encoder * pEnc,
80                          Bitstream * bs,
81                          uint32_t *pBits,
82                          bool force_inter,
83                          bool vol_header);
84    
85    #ifdef BFRAMES
86    static void FrameCodeB(Encoder * pEnc,
87                           FRAMEINFO * frame,
88                           Bitstream * bs,
89                           uint32_t *pBits);
90    #endif
91    
92  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits);  /*****************************************************************************
93  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header);   * Local data
94     ****************************************************************************/
95    
96  static int DQtab[4] =  static int DQtab[4] =
97  {  {
# Line 38  Line 104 
104  };  };
105    
106    
107  void static image_null(IMAGE * image)  static void __inline image_null(IMAGE * image)
108  {  {
109          image->y = image->u = image->v = NULL;          image->y = image->u = image->v = NULL;
110  }  }
111    
112    
113  int encoder_create(XVID_ENC_PARAM * pParam)  /*****************************************************************************
114     * Encoder creation
115     *
116     * This function creates an Encoder instance, it allocates all necessary
117     * image buffers (reference, current and bframes) and initialize the internal
118     * xvid encoder paremeters according to the XVID_ENC_PARAM input parameter.
119     *
120     * The code seems to be very long but is very basic, mainly memory allocation
121     * and cleaning code.
122     *
123     * Returned values :
124     *    - XVID_ERR_OK     - no errors
125     *    - XVID_ERR_MEMORY - the libc could not allocate memory, the function
126     *                        cleans the structure before exiting.
127     *                        pParam->handle is also set to NULL.
128     *
129     ****************************************************************************/
130    
131    int
132    encoder_create(XVID_ENC_PARAM * pParam)
133  {  {
134          Encoder *pEnc;          Encoder *pEnc;
135          uint32_t i;          uint32_t i;
# Line 58  Line 143 
143          ENC_CHECK(!(pParam->width % 2));          ENC_CHECK(!(pParam->width % 2));
144          ENC_CHECK(!(pParam->height % 2));          ENC_CHECK(!(pParam->height % 2));
145    
146            /* Fps */
147    
148          if (pParam->fincr <= 0 || pParam->fbase <= 0)          if (pParam->fincr <= 0 || pParam->fbase <= 0)
149          {          {
150                  pParam->fincr = 1;                  pParam->fincr = 1;
151                  pParam->fbase = 25;                  pParam->fbase = 25;
152          }          }
153    
154          // simplify the "fincr/fbase" fraction          /*
155          // (neccessary, since windows supplies us with huge numbers)           * Simplify the "fincr/fbase" fraction
156             * (neccessary, since windows supplies us with huge numbers)
157             */
158    
159          i = pParam->fincr;          i = pParam->fincr;
160          while (i > 1)          while (i > 1)
# Line 87  Line 176 
176                  pParam->fincr = (int)(pParam->fincr / div);                  pParam->fincr = (int)(pParam->fincr / div);
177          }          }
178    
179            /* Bitrate allocator defaults */
180    
181          if (pParam->rc_bitrate <= 0)          if (pParam->rc_bitrate <= 0)
182                  pParam->rc_bitrate = 900000;                  pParam->rc_bitrate = 900000;
183    
# Line 99  Line 190 
190          if (pParam->rc_buffer <= 0)          if (pParam->rc_buffer <= 0)
191                  pParam->rc_buffer = 100;                  pParam->rc_buffer = 100;
192    
193            /* Max and min quantizers */
194    
195          if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))          if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))
196                  pParam->min_quantizer = 1;                  pParam->min_quantizer = 1;
197    
198          if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31))          if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31))
199                  pParam->max_quantizer = 31;                  pParam->max_quantizer = 31;
200    
         if (pParam->max_key_interval == 0)              /* 1 keyframe each 10 seconds */  
                 pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;  
   
201          if (pParam->max_quantizer < pParam->min_quantizer)          if (pParam->max_quantizer < pParam->min_quantizer)
202                  pParam->max_quantizer = pParam->min_quantizer;                  pParam->max_quantizer = pParam->min_quantizer;
203    
204          if ((pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE)) == NULL)          /* 1 keyframe each 10 seconds */
205    
206            if (pParam->max_key_interval == 0)
207                    pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
208    
209    
210            pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
211            if (pEnc == NULL)
212                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
213    
214          /* Fill members of Encoder structure */          /* Fill members of Encoder structure */
# Line 125  Line 222 
222          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;
223          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;
224    
225            pEnc->mbParam.fbase = pParam->fbase;
226            pEnc->mbParam.fincr = pParam->fincr;
227    
228          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
229    
230          /* Fill rate control parameters */          /* Fill rate control parameters */
# Line 136  Line 236 
236    
237          /* try to allocate frame memory */          /* try to allocate frame memory */
238    
239          pEnc->current = NULL;          pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
240          pEnc->reference = NULL;          pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
241          if ( (pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL ||  
242                   (pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL)          if ( pEnc->current == NULL || pEnc->reference == NULL)
243          {                  goto xvid_err_memory1;
                 if (pEnc->current) xvid_free(pEnc->current);  
                 xvid_free(pEnc);  
                 return XVID_ERR_MEMORY;  
         }  
244    
245          /* try to allocate mb memory */          /* try to allocate mb memory */
246    
247          pEnc->current->mbs = NULL;          pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * \
248          pEnc->reference->mbs = NULL;                                           pEnc->mbParam.mb_width * \
249                                             pEnc->mbParam.mb_height,
250                                             CACHE_LINE);
251            pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * \
252                                               pEnc->mbParam.mb_width * \
253                                               pEnc->mbParam.mb_height,
254                                               CACHE_LINE);
255    
256  OutputDebugString("malloc mbs");          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
257          if ((pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL ||                  goto xvid_err_memory2;
                 (pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL)  
         {  
                 if (pEnc->current->mbs) xvid_free(pEnc->current->mbs);  
                 xvid_free(pEnc->current);  
                 xvid_free(pEnc->reference);  
                 xvid_free(pEnc);  
         }  
258    
259          /* try to allocate image memory */          /* try to allocate image memory */
260    
261  #ifdef _DEBUG  #ifdef _DEBUG
262          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
263  #endif  #endif
264    #ifdef BFRAMES
265            image_null(&pEnc->f_refh);
266            image_null(&pEnc->f_refv);
267            image_null(&pEnc->f_refhv);
268    #endif
269          image_null(&pEnc->current->image);          image_null(&pEnc->current->image);
270          image_null(&pEnc->reference->image);          image_null(&pEnc->reference->image);
271          image_null(&pEnc->vInterH);          image_null(&pEnc->vInterH);
# Line 174  Line 274 
274          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
275          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
276    
   
 OutputDebugString("malloc images");  
         if (  
277  #ifdef _DEBUG  #ifdef _DEBUG
278                  image_create(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||          if (image_create(&pEnc->sOriginal,
279                             pEnc->mbParam.edged_width,
280                             pEnc->mbParam.edged_height) < 0)
281                    goto xvid_err_memory3;
282  #endif  #endif
283                  image_create(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  #ifdef BFRAMES
284                  image_create(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||          if (image_create(&pEnc->f_refh,
285                  image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                           pEnc->mbParam.edged_width,
286                  image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                           pEnc->mbParam.edged_height) < 0)
287                  image_create(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                  goto xvid_err_memory3;
288                  image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||          if (image_create(&pEnc->f_refv,
289                  image_create(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_width,
290          {                           pEnc->mbParam.edged_height) < 0)
291  #ifdef _DEBUG                  goto xvid_err_memory3;
292                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          if (image_create(&pEnc->f_refhv,
293                             pEnc->mbParam.edged_width,
294                             pEnc->mbParam.edged_height) < 0)
295                    goto xvid_err_memory3;
296  #endif  #endif
297                  image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          if (image_create(&pEnc->current->image,
298                  image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                           pEnc->mbParam.edged_width,
299                  image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                           pEnc->mbParam.edged_height) < 0)
300                  image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  goto xvid_err_memory3;
301                  image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          if (image_create(&pEnc->reference->image,
302                  image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                           pEnc->mbParam.edged_width,
303                  image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                           pEnc->mbParam.edged_height) < 0)
304                    goto xvid_err_memory3;
305            if (image_create(&pEnc->vInterH,
306                             pEnc->mbParam.edged_width,
307                             pEnc->mbParam.edged_height) < 0)
308                    goto xvid_err_memory3;
309            if (image_create(&pEnc->vInterV,
310                             pEnc->mbParam.edged_width,
311                             pEnc->mbParam.edged_height) < 0)
312                    goto xvid_err_memory3;
313            if (image_create(&pEnc->vInterVf,
314                             pEnc->mbParam.edged_width,
315                             pEnc->mbParam.edged_height) < 0)
316                    goto xvid_err_memory3;
317            if (image_create(&pEnc->vInterHV,
318                             pEnc->mbParam.edged_width,
319                             pEnc->mbParam.edged_height) < 0)
320                    goto xvid_err_memory3;
321            if (image_create(&pEnc->vInterHVf,
322                             pEnc->mbParam.edged_width,
323                             pEnc->mbParam.edged_height) < 0)
324                    goto xvid_err_memory3;
325    
326    
327    
328            /* B Frames specific init */
329    #ifdef BFRAMES
330    
331            pEnc->mbParam.max_bframes = pParam->max_bframes;
332            pEnc->bquant_ratio = pParam->bquant_ratio;
333            pEnc->bframes = NULL;
334    
335            if (pEnc->mbParam.max_bframes > 0)
336            {
337                    int n;
338    
339                    pEnc->bframes = xvid_malloc(pEnc->mbParam.max_bframes * \
340                                                sizeof(FRAMEINFO *),
341                                                CACHE_LINE);
342    
343                    if (pEnc->bframes == NULL)
344                            goto xvid_err_memory3;
345    
346                    for (n=0; n<pEnc->mbParam.max_bframes; n++)
347                            pEnc->bframes[n] = NULL;
348    
349    
350                    for (n = 0; n < pEnc->mbParam.max_bframes; n++)
351                    {
352                            pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO),
353                                                           CACHE_LINE);
354    
355                            if (pEnc->bframes[n] == NULL)
356                                    goto xvid_err_memory4;
357    
358                            pEnc->bframes[n]->mbs = xvid_malloc(sizeof(MACROBLOCK) * \
359                                                                pEnc->mbParam.mb_width * \
360                                                                pEnc->mbParam.mb_height,
361                                                                CACHE_LINE);
362    
363                            if (pEnc->bframes[n]->mbs == NULL)
364                                    goto xvid_err_memory4;
365    
366                            image_null(&pEnc->bframes[n]->image);
367    
368                            if (image_create(&pEnc->bframes[n]->image,
369                                             pEnc->mbParam.edged_width,
370                                             pEnc->mbParam.edged_height) < 0)
371                                    goto xvid_err_memory4;
372    
                 xvid_free(pEnc->current);  
                 xvid_free(pEnc->reference);  
                 xvid_free(pEnc);  
                 return XVID_ERR_MEMORY;  
373          }          }
374            }
375    
376            pEnc->bframenum_head = 0;
377            pEnc->bframenum_tail = 0;
378            pEnc->flush_bframes = 0;
379    
380            pEnc->mbParam.m_seconds = 0;
381            pEnc->mbParam.m_ticks = 0;
382    #endif
383    
384          pParam->handle = (void *)pEnc;          pParam->handle = (void *)pEnc;
385    
386          if (pParam->rc_bitrate)          if (pParam->rc_bitrate)
387          {          {
388                  RateControlInit(pParam->rc_bitrate, pParam->rc_reaction_delay_factor,                  RateControlInit(&pEnc->rate_control,
389                          pParam->rc_averaging_period, pParam->rc_buffer, pParam->fbase * 1000 / pParam->fincr,                                  pParam->rc_bitrate,
390                          pParam->max_quantizer, pParam->min_quantizer);                                  pParam->rc_reaction_delay_factor,
391                                    pParam->rc_averaging_period,
392                                    pParam->rc_buffer,
393                                    pParam->fbase * 1000 / pParam->fincr,
394                                    pParam->max_quantizer,
395                                    pParam->min_quantizer);
396          }          }
397    
398          init_timer();          init_timer();
399    
400          return XVID_ERR_OK;          return XVID_ERR_OK;
401    
402            /*
403             * We handle all XVID_ERR_MEMORY here, this makes the code lighter
404             */
405    #ifdef BFRAMES
406     xvid_err_memory4:
407            for (i=0; i<pEnc->mbParam.max_bframes; i++)
408            {
409    
410                    if (pEnc->bframes[i] == NULL) continue;
411    
412                    image_destroy(&pEnc->bframes[i]->image,
413                                  pEnc->mbParam.edged_width,
414                                  pEnc->mbParam.edged_height);
415    
416                    xvid_free(pEnc->bframes[i]->mbs);
417    
418                    xvid_free(pEnc->bframes[i]);
419    
420            }
421    
422            xvid_free(pEnc->bframes);
423    
424    #endif
425    
426     xvid_err_memory3:
427    #ifdef _DEBUG
428            image_destroy(&pEnc->sOriginal,
429                          pEnc->mbParam.edged_width,
430                          pEnc->mbParam.edged_height);
431    #endif
432    
433    #ifdef BFRAMES
434            image_destroy(&pEnc->f_refh,
435                          pEnc->mbParam.edged_width,
436                          pEnc->mbParam.edged_height);
437            image_destroy(&pEnc->f_refv,
438                          pEnc->mbParam.edged_width,
439                          pEnc->mbParam.edged_height);
440            image_destroy(&pEnc->f_refhv,
441                          pEnc->mbParam.edged_width,
442                          pEnc->mbParam.edged_height);
443    #endif
444    
445            image_destroy(&pEnc->current->image,
446                          pEnc->mbParam.edged_width,
447                          pEnc->mbParam.edged_height);
448            image_destroy(&pEnc->reference->image,
449                          pEnc->mbParam.edged_width,
450                          pEnc->mbParam.edged_height);
451            image_destroy(&pEnc->vInterH,
452                          pEnc->mbParam.edged_width,
453                          pEnc->mbParam.edged_height);
454            image_destroy(&pEnc->vInterV,
455                          pEnc->mbParam.edged_width,
456                          pEnc->mbParam.edged_height);
457            image_destroy(&pEnc->vInterVf,
458                          pEnc->mbParam.edged_width,
459                          pEnc->mbParam.edged_height);
460            image_destroy(&pEnc->vInterHV,
461                          pEnc->mbParam.edged_width,
462                          pEnc->mbParam.edged_height);
463            image_destroy(&pEnc->vInterHVf,
464                          pEnc->mbParam.edged_width,
465                          pEnc->mbParam.edged_height);
466    
467     xvid_err_memory2:
468            xvid_free(pEnc->current->mbs);
469            xvid_free(pEnc->reference->mbs);
470    
471     xvid_err_memory1:
472            xvid_free(pEnc->current);
473            xvid_free(pEnc->reference);
474            xvid_free(pEnc);
475    
476            pParam->handle = NULL;
477    
478            return XVID_ERR_MEMORY;
479  }  }
480    
481    /*****************************************************************************
482     * Encoder destruction
483     *
484     * This function destroy the entire encoder structure created by a previous
485     * successful encoder_create call.
486     *
487     * Returned values (for now only one returned value) :
488     *    - XVID_ERR_OK     - no errors
489     *
490     ****************************************************************************/
491    
492  int encoder_destroy(Encoder * pEnc)  int
493    encoder_destroy(Encoder * pEnc)
494  {  {
495          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
496    
497          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          /* B Frames specific */
498          image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  #ifdef BFRAMES
499          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          int i;
500          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
501          image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          for (i=0; i<pEnc->mbParam.max_bframes; i++)
502          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          {
503          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
504                    if (pEnc->bframes[i] == NULL) continue;
505    
506                    image_destroy(&pEnc->bframes[i]->image,
507                                  pEnc->mbParam.edged_width,
508                                  pEnc->mbParam.edged_height);
509    
510                    xvid_free(pEnc->bframes[i]->mbs);
511    
512                    xvid_free(pEnc->bframes[i]);
513    
514            }
515    
516            xvid_free(pEnc->bframes);
517    
518    #endif
519    
520            /* All images, reference, current etc ... */
521    
522            image_destroy(&pEnc->current->image,
523                          pEnc->mbParam.edged_width,
524                          pEnc->mbParam.edged_height);
525            image_destroy(&pEnc->reference->image,
526                          pEnc->mbParam.edged_width,
527                          pEnc->mbParam.edged_height);
528            image_destroy(&pEnc->vInterH,
529                          pEnc->mbParam.edged_width,
530                          pEnc->mbParam.edged_height);
531            image_destroy(&pEnc->vInterV,
532                          pEnc->mbParam.edged_width,
533                          pEnc->mbParam.edged_height);
534            image_destroy(&pEnc->vInterVf,
535                          pEnc->mbParam.edged_width,
536                          pEnc->mbParam.edged_height);
537            image_destroy(&pEnc->vInterHV,
538                          pEnc->mbParam.edged_width,
539                          pEnc->mbParam.edged_height);
540            image_destroy(&pEnc->vInterHVf,
541                          pEnc->mbParam.edged_width,
542                          pEnc->mbParam.edged_height);
543    #ifdef BFRAMES
544            image_destroy(&pEnc->f_refh,
545                          pEnc->mbParam.edged_width,
546                          pEnc->mbParam.edged_height);
547            image_destroy(&pEnc->f_refv,
548                          pEnc->mbParam.edged_width,
549                          pEnc->mbParam.edged_height);
550            image_destroy(&pEnc->f_refhv,
551                          pEnc->mbParam.edged_width,
552                          pEnc->mbParam.edged_height);
553    #endif
554  #ifdef _DEBUG  #ifdef _DEBUG
555                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          image_destroy(&pEnc->sOriginal,
556                          pEnc->mbParam.edged_width,
557                          pEnc->mbParam.edged_height);
558  #endif  #endif
559    
560            /* Encoder structure */
561    
562          xvid_free(pEnc->current->mbs);          xvid_free(pEnc->current->mbs);
563          xvid_free(pEnc->current);          xvid_free(pEnc->current);
564    
# Line 241  Line 566 
566          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
567    
568          xvid_free(pEnc);          xvid_free(pEnc);
569    
570            return XVID_ERR_OK;
571    }
572    
573    /*****************************************************************************
574     * Frame encoder entry point
575     *
576     * At this moment 2 versions coexist : one for IPB compatible encoder,
577     *                                     another one for the old IP encoder.
578     *
579     * Returned values :
580     *    - XVID_ERR_OK     - no errors
581     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
582     *                        format
583     ****************************************************************************/
584    
585    
586    #ifdef BFRAMES
587    /*****************************************************************************
588     * Frame encoder entry point for IPB capable encoder
589     ****************************************************************************/
590    int
591    encoder_encode(Encoder * pEnc,
592                   XVID_ENC_FRAME * pFrame,
593                   XVID_ENC_STATS * pResult)
594    {
595            uint16_t x, y;
596            Bitstream bs;
597            uint32_t bits;
598    
599    #ifdef _DEBUG
600            float psnr;
601            char temp[128];
602    #endif
603    
604            ENC_CHECK(pEnc);
605            ENC_CHECK(pFrame);
606    
607            start_global_timer();
608    
609            BitstreamInit(&bs, pFrame->bitstream, 0);
610    
611            /*
612             * bframe "flush" code
613             */
614    
615            if ( (pFrame->image == NULL || pEnc->flush_bframes) &&
616                 (pEnc->bframenum_head < pEnc->bframenum_tail))
617            {
618    
619                    if (pEnc->flush_bframes == 0)
620                    {
621                            /*
622                             * we have reached the end of stream without getting
623                             * a future reference frame... so encode last final
624                             * frame as a pframe
625                             */
626    
627                            /* ToDo : remove dprintf calls */
628                            /*
629                              dprintf("--- PFRAME (final frame correction) --- ");
630                            */
631                            pEnc->bframenum_tail--;
632                            SWAP(pEnc->current, pEnc->reference);
633    
634                            SWAP(pEnc->current,
635                                 pEnc->bframes[pEnc->bframenum_tail]);
636    
637                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
638    
639                            BitstreamPad(&bs);
640                            pFrame->length = BitstreamLength(&bs);
641                            pFrame->input_consumed = 0;
642                            pFrame->intra = 0;
643    
644                            return XVID_ERR_OK;
645                    }
646    
647                    /* ToDo : remove dprintf calls */
648                    /*
649                      dprintf("--- BFRAME (flush) --- ");
650                    */
651                    FrameCodeB(pEnc,
652                               pEnc->bframes[pEnc->bframenum_head],
653                               &bs,
654                               &bits);
655                    pEnc->bframenum_head++;
656    
657    
658                    BitstreamPad(&bs);
659                    pFrame->length = BitstreamLength(&bs);
660                    pFrame->input_consumed = 0;
661                    pFrame->intra = 0;
662    
663                    return XVID_ERR_OK;
664            }
665    
666            if (pFrame->image == NULL)
667            {
668                    pFrame->length = 0;
669                    pFrame->input_consumed = 1;
670                    pFrame->intra = 0;
671    
672                    return XVID_ERR_OK;
673            }
674    
675            if (pEnc->bframenum_head > 0)
676            {
677                    pEnc->bframenum_head = pEnc->bframenum_tail = 0;
678            }
679    
680            pEnc->flush_bframes = 0;
681    
682            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
683             * Well there was a separation here so i put it in ANSI C
684             * comment style :-)
685             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
686    
687            SWAP(pEnc->current, pEnc->reference);
688    
689            EMMS();
690    
691            if (pFrame->quant == 0)
692                    pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
693            else
694                    pEnc->current->quant = pFrame->quant;
695    
696            if(pEnc->current->quant < 1)
697                    pEnc->current->quant = 1;
698    
699            if(pEnc->current->quant > 31)
700                    pEnc->current->quant = 31;
701    
702            pEnc->current->global_flags = pFrame->general;
703            pEnc->current->motion_flags = pFrame->motion;
704            pEnc->current->seconds = pEnc->mbParam.m_seconds;
705            pEnc->current->ticks = pEnc->mbParam.m_ticks;
706            /* ToDo : dynamic fcode (in both directions) */
707            pEnc->current->fcode = pEnc->mbParam.m_fcode;
708            pEnc->current->bcode = pEnc->mbParam.m_fcode;
709    
710            start_timer();
711            if (image_input(&pEnc->current->image,
712                            pEnc->mbParam.width,
713                            pEnc->mbParam.height,
714                            pEnc->mbParam.edged_width,
715                            pFrame->image,
716                            pFrame->colorspace))
717                    return XVID_ERR_FORMAT;
718            stop_conv_timer();
719    
720    #ifdef _DEBUG
721            image_copy(&pEnc->sOriginal,
722                       &pEnc->current->image,
723                       pEnc->mbParam.edged_width,
724                       pEnc->mbParam.height);
725    #endif
726    
727            EMMS();
728    
729            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
730             * Luminance masking
731             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
732    
733            if ((pEnc->current->global_flags & XVID_LUMIMASKING))
734            {
735                    int *temp_dquants =
736                            (int *) xvid_malloc(pEnc->mbParam.mb_width * \
737                                                pEnc->mbParam.mb_height * \
738                                                sizeof(int),
739                                                CACHE_LINE);
740    
741                    pEnc->current->quant =
742                            adaptive_quantization(pEnc->current->image.y,
743                                                  pEnc->mbParam.edged_width,
744                                                  temp_dquants,
745                                                  pEnc->current->quant,
746                                                  pEnc->current->quant,
747                                                  2*pEnc->current->quant,
748                                                  pEnc->mbParam.mb_width,
749                                                  pEnc->mbParam.mb_height);
750    
751                    for (y = 0; y < pEnc->mbParam.mb_height; y++)
752                    {
753    
754                            #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
755    
756                            for (x = 0; x < pEnc->mbParam.mb_width; x++)
757                            {
758                                    MACROBLOCK *pMB =
759                                            &pEnc->current->mbs[OFFSET(x,y)];
760                                    pMB->dquant =
761                                            iDQtab[temp_dquants[OFFSET(x,y)] + 2];
762                            }
763    
764                            #undef OFFSET
765    
766                    }
767    
768                    xvid_free(temp_dquants);
769            }
770    
771            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
772             * ivop/pvop/bvop selection
773             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
774    
775    
776            if (pEnc->iFrameNum == 0 ||
777                pFrame->intra   == 1 ||
778    
779                (pFrame->intra < 0  &&
780                 pEnc->iMaxKeyInterval > 0 &&
781                 pEnc->iFrameNum >= pEnc->iMaxKeyInterval) ||
782    
783                image_mad(&pEnc->reference->image,
784                          &pEnc->current->image,
785                          pEnc->mbParam.edged_width,
786                          pEnc->mbParam.width,
787                          pEnc->mbParam.height) > 30)
788            {
789                    /*
790                     * This will be coded as an Intra Frame
791                     */
792    
793                    /* ToDo : Remove dprintf calls */
794                    /*
795                      dprintf("--- IFRAME ---");
796                    */
797    
798                    FrameCodeI(pEnc, &bs, &bits);
799    
800                    pFrame->intra = 1;
801                    pEnc->flush_bframes = 1;
802    
803                    /*
804                     * NB : sequences like "IIBB" decode fine with msfdam but,
805                     *      go screwy with divx 5.00
806                     */
807            }
808            else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes)
809            {
810                    /*
811                     * This will be coded as a Predicted Frame
812                     */
813    
814                    /* ToDo : Remove dprintf calls */
815                    /*
816                      dprintf("--- PFRAME ---");
817                    */
818    
819                    FrameCodeP(pEnc, &bs, &bits, 1, 0);
820                    pFrame->intra = 0;
821                    pEnc->flush_bframes = 1;
822            }
823            else
824            {
825                    /*
826                     * This will be coded as a Bidirectional Frame
827                     */
828    
829                    /* ToDo : Remove dprintf calls */
830                    /*
831                      dprintf("--- BFRAME (store) ---  head=%i tail=%i",
832                      pEnc->bframenum_head,
833                      pEnc->bframenum_tail);
834                    */
835    
836                    if (pFrame->bquant < 1)
837                    {
838                            pEnc->current->quant =
839                                    ((pEnc->reference->quant+pEnc->current->quant)*\
840                                     pEnc->bquant_ratio) / 200;
841                    }
842                    else
843                    {
844                            pEnc->current->quant = pFrame->bquant;
845                    }
846    
847                    /* store frame into bframe buffer & swap ref back to current */
848                    SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
849                    SWAP(pEnc->current, pEnc->reference);
850    
851                    pEnc->bframenum_tail++;
852    
853                    pFrame->intra = 0;
854                    pFrame->length = 0;
855                    pFrame->input_consumed = 1;
856    
857                    pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
858                    if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase)
859                    {
860                            pEnc->mbParam.m_seconds++;
861                            pEnc->mbParam.m_ticks = 0;
862                    }
863    
864          return XVID_ERR_OK;          return XVID_ERR_OK;
865  }  }
866    
867  int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult)          BitstreamPad(&bs);
868            pFrame->length = BitstreamLength(&bs);
869    
870            if (pResult)
871            {
872                    pResult->quant = pEnc->current->quant;
873                    pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits/8);
874                    pResult->kblks = pEnc->sStat.kblks;
875                    pResult->mblks = pEnc->sStat.mblks;
876                    pResult->ublks = pEnc->sStat.ublks;
877            }
878    
879            EMMS();
880    
881    #ifdef _DEBUG
882            psnr = image_psnr(&pEnc->sOriginal,
883                              &pEnc->current->image,
884                              pEnc->mbParam.edged_width,
885                              pEnc->mbParam.width,
886                              pEnc->mbParam.height);
887    
888            snprintf(temp, 127, "PSNR: %f\n", psnr);
889            DEBUG(temp);
890    #endif
891    
892            if (pFrame->quant == 0)
893            {
894                    RateControlUpdate(&pEnc->rate_control,
895                                      pEnc->current->quant,
896                                      pFrame->length,
897                                      pFrame->intra);
898            }
899    
900            pEnc->iFrameNum++;
901            pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
902            if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase)
903            {
904                    pEnc->mbParam.m_seconds++;
905                    pEnc->mbParam.m_ticks = 0;
906            }
907            pFrame->input_consumed = 1;
908    
909            stop_global_timer();
910            write_timer();
911    
912            return XVID_ERR_OK;
913    }
914    #else
915    /*****************************************************************************
916     * Frame encoder entry point for IP capable encoder
917     ****************************************************************************/
918    int
919    encoder_encode(Encoder * pEnc,
920                   XVID_ENC_FRAME * pFrame,
921                   XVID_ENC_STATS * pResult)
922  {  {
923          uint16_t x, y;          uint16_t x, y;
924          Bitstream bs;          Bitstream bs;
# Line 252  Line 926 
926          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
927  #ifdef _DEBUG  #ifdef _DEBUG
928          float psnr;          float psnr;
929          uint8_t temp[100];          uint8_t temp[128];
930  #endif  #endif
931    
932          start_global_timer();          start_global_timer();
# Line 269  Line 943 
943          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
944    
945          start_timer();          start_timer();
946          if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width,          if (image_input(&pEnc->current->image,
947                          pFrame->image, pFrame->colorspace))                          pEnc->mbParam.width,
948          {                          pEnc->mbParam.height,
949                            pEnc->mbParam.edged_width,
950                            pFrame->image,
951                            pFrame->colorspace) < 0)
952                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
         }  
953          stop_conv_timer();          stop_conv_timer();
954    
955  #ifdef _DEBUG  #ifdef _DEBUG
956          image_copy(&pEnc->sOriginal, &pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.height);          image_copy(&pEnc->sOriginal,
957                       &pEnc->current->image,
958                       pEnc->mbParam.edged_width,
959                       pEnc->mbParam.height);
960  #endif  #endif
961    
962          EMMS();          EMMS();
# Line 286  Line 965 
965    
966          if (pFrame->quant == 0)          if (pFrame->quant == 0)
967          {          {
968                  pEnc->current->quant = RateControlGetQ(0);                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control,0);
969          }          }
970          else          else
971          {          {
# Line 295  Line 974 
974    
975          if ((pEnc->current->global_flags & XVID_LUMIMASKING))          if ((pEnc->current->global_flags & XVID_LUMIMASKING))
976          {          {
977                  int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);                  int * temp_dquants = (int *)
978                            xvid_malloc(pEnc->mbParam.mb_width * \
979                                        pEnc->mbParam.mb_height * \
980                                        sizeof(int),
981                                        CACHE_LINE);
982    
983                  pEnc->current->quant = adaptive_quantization(pEnc->current->image.y,                  pEnc->current->quant =
984                                                              pEnc->mbParam.edged_width,  // stride                          adaptive_quantization(pEnc->current->image.y,
985                                                  pEnc->mbParam.edged_width,
986                                                              temp_dquants,                                                              temp_dquants,
987                                                              pEnc->current->quant,                                                              pEnc->current->quant,
988                                                              pEnc->current->quant,       // min_quant                                                pEnc->current->quant,
989                                                              2*pEnc->current->quant,     // max_quant                                                2*pEnc->current->quant,
990                                                              pEnc->mbParam.mb_width,                                                              pEnc->mbParam.mb_width,
991                                                              pEnc->mbParam.mb_height);                                                              pEnc->mbParam.mb_height);
992    
993                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                  for (y = 0; y < pEnc->mbParam.mb_height; y++)
994                    {
995    
996                            #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
997    
998                          for (x = 0; x < pEnc->mbParam.mb_width; x++)                          for (x = 0; x < pEnc->mbParam.mb_width; x++)
999                          {                          {
1000                                  MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
1001                                  pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)];  
1002                                    MACROBLOCK *pMB =
1003                                            &pEnc->current->mbs[OFFSET(x,y)];
1004                                    pMB->dquant =
1005                                            iDQtab[temp_dquants[OFFSET(x,y)] + 2];
1006                            }
1007    
1008                            #undef OFFSET
1009                          }                          }
1010    
1011                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
1012          }          }
1013    
1014          if (pEnc->current->global_flags & XVID_H263QUANT) {          if (pEnc->current->global_flags & XVID_H263QUANT)
1015            {
1016                  if(pEnc->mbParam.m_quant_type != H263_QUANT)                  if(pEnc->mbParam.m_quant_type != H263_QUANT)
1017                          write_vol_header = 1;                          write_vol_header = 1;
1018                  pEnc->mbParam.m_quant_type = H263_QUANT;                  pEnc->mbParam.m_quant_type = H263_QUANT;
1019          }          }
1020          else if(pEnc->current->global_flags & XVID_MPEGQUANT) {          else if (pEnc->current->global_flags & XVID_MPEGQUANT)
1021                  int ret1, ret2;          {
1022                    int matrix1_changed, matrix2_changed;
1023    
1024                  ret1 = ret2 = 0;                  matrix1_changed = matrix2_changed = 0;
1025    
1026                  if(pEnc->mbParam.m_quant_type != MPEG4_QUANT)                  if(pEnc->mbParam.m_quant_type != MPEG4_QUANT)
1027                          write_vol_header = 1;                          write_vol_header = 1;
# Line 332  Line 1030 
1030    
1031                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1032                          if(pFrame->quant_intra_matrix != NULL)                          if(pFrame->quant_intra_matrix != NULL)
1033                                  ret1 = set_intra_matrix(pFrame->quant_intra_matrix);                                  matrix1_changed =
1034                                            set_intra_matrix(pFrame->quant_intra_matrix);
1035                          if(pFrame->quant_inter_matrix != NULL)                          if(pFrame->quant_inter_matrix != NULL)
1036                                  ret2 = set_inter_matrix(pFrame->quant_inter_matrix);                                  matrix2_changed
1037                                            = set_inter_matrix(pFrame->quant_inter_matrix);
1038                  }                  }
1039                  else {                  else {
1040                          ret1 = set_intra_matrix(get_default_intra_matrix());                          matrix1_changed = set_intra_matrix(get_default_intra_matrix());
1041                          ret2 = set_inter_matrix(get_default_inter_matrix());                          matrix2_changed = set_inter_matrix(get_default_inter_matrix());
1042                  }                  }
1043                  if(write_vol_header == 0)                  if(write_vol_header == 0)
1044                          write_vol_header = ret1 | ret2;                          write_vol_header = matrix1_changed | matrix2_changed;
1045          }          }
1046    
1047          if (pFrame->intra < 0)          if (pFrame->intra < 0)
1048          {          {
1049                  if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0)                  if ((pEnc->iFrameNum == 0) ||
                                                && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval)))  
1050    
1051                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                      ((pEnc->iMaxKeyInterval > 0) &&
1052                         (pEnc->iFrameNum >= pEnc->iMaxKeyInterval)))
1053                    {
1054                            pFrame->intra = FrameCodeI(pEnc,
1055                                                       &bs,
1056                                                       &bits);
1057                    }
1058                  else                  else
1059                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);                  {
1060                            pFrame->intra = FrameCodeP(pEnc,
1061                                                       &bs,
1062                                                       &bits,
1063                                                       0,
1064                                                       write_vol_header);
1065                    }
1066          }          }
1067          else          else
1068          {          {
1069                  if (pFrame->intra == 1)                  if (pFrame->intra == 1)
1070                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                  {
1071                            pFrame->intra = FrameCodeI(pEnc,
1072                                                       &bs,
1073                                                       &bits);
1074                    }
1075                  else                  else
1076                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);                  {
1077                            pFrame->intra = FrameCodeP(pEnc,
1078                                                       &bs,
1079                                                       &bits,
1080                                                       1,
1081                                                       write_vol_header);
1082                    }
1083    
1084          }          }
1085    
1086          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
# Line 379  Line 1101 
1101    
1102          if (pFrame->quant == 0)          if (pFrame->quant == 0)
1103          {          {
1104                  RateControlUpdate(pEnc->current->quant, pFrame->length, pFrame->intra);                  RateControlUpdate(&pEnc->rate_control,
1105                                      pEnc->current->quant,
1106                                      pFrame->length,
1107                                      pFrame->intra);
1108          }          }
1109    
1110  #ifdef _DEBUG  #ifdef _DEBUG
1111          psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width,          psnr = image_psnr(&pEnc->sOriginal,
1112                                  pEnc->mbParam.width, pEnc->mbParam.height);                            &pEnc->current->image,
1113                              pEnc->mbParam.edged_width,
1114                              pEnc->mbParam.width,
1115                              pEnc->mbParam.height);
1116    
1117          sprintf(temp, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1118          DEBUG(temp);          DEBUG(temp);
1119  #endif  #endif
1120    
# Line 397  Line 1125 
1125    
1126          return XVID_ERR_OK;          return XVID_ERR_OK;
1127  }  }
1128    #endif
1129    
1130    
1131  static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) {  static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) {
# Line 464  Line 1193 
1193                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1194                          VECTOR pred[4];                          VECTOR pred[4];
1195                          VECTOR tmp;                          VECTOR tmp;
1196                          int dummy[4];                          int32_t dummy[4];
1197                          int vec;                          int vec;
1198    
1199                          pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS);                          pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS);
# Line 708  Line 1437 
1437          int iLimit;          int iLimit;
1438          uint32_t x, y;          uint32_t x, y;
1439          int iSearchRange;          int iSearchRange;
1440          bool bIntra;          int bIntra;
1441    
1442          IMAGE *pCurrent = &pEnc->current->image;          /* IMAGE *pCurrent = &pEnc->current->image; */
1443          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
1444    
1445          start_timer();          start_timer();
# Line 885  Line 1614 
1614  }  }
1615    
1616    
1617    #ifdef BFRAMES
 /*  
1618  static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits)  static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits)
1619  {  {
1620      int16_t dct_codes[6][64];      int16_t dct_codes[6*64];
1621      int16_t qcoeff[6][64];      int16_t qcoeff[6*64];
1622      uint32_t x, y;      uint32_t x, y;
1623          VECTOR forward;          VECTOR forward;
1624          VECTOR backward;          VECTOR backward;
# Line 899  Line 1627 
1627          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
1628    
1629          // forward          // forward
1630          image_setedges(f_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height);          image_setedges(f_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height, frame->global_flags & XVID_INTERLACING);
1631          start_timer();          start_timer();
1632          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1633                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);
1634          stop_inter_timer();          stop_inter_timer();
1635    
1636          // backward          // backward
1637          image_setedges(b_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height);          image_setedges(b_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height, frame->global_flags & XVID_INTERLACING);
1638      start_timer();      start_timer();
1639          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1640                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);
# Line 917  Line 1645 
1645                  pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                  pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1646                  pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);                  pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);
1647    
1648    
1649          stop_motion_timer();          stop_motion_timer();
1650    
1651          if (test_quant_type(&pEnc->mbParam, pEnc->current))          /*if (test_quant_type(&pEnc->mbParam, pEnc->current))
1652          {          {
1653                  BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);                  BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);
1654          }          }*/
1655    
1656      frame->coding_type = B_VOP;      frame->coding_type = B_VOP;
1657      BitstreamWriteVopHeader(bs, B_VOP, frame->tick, 0,      BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame);
                         frame->quant, frame->fcode, frame->bcode);  
1658    
1659      *pBits = BitstreamPos(bs);      *pBits = BitstreamPos(bs);
1660    
# Line 965  Line 1693 
1693                                          dct_codes);                                          dct_codes);
1694    
1695                          mb->quant = frame->quant;                          mb->quant = frame->quant;
1696                          mb->cbp = MBTransQuantInter(&pEnc->mbParam, frame, x, y, dct_codes, qcoeff);                          mb->cbp = MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff);
1697                          //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant);                          //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant);
1698    
1699    
# Line 996  Line 1724 
1724  //                      printf("[%i %i] M=%i CBP=%i MVX=%i MVY=%i %i,%i  %i,%i\n", x, y, pMB->mode, pMB->cbp, pMB->mvs[0].x, bmb->pmvs[0].x, bmb->pmvs[0].y, forward.x, forward.y);  //                      printf("[%i %i] M=%i CBP=%i MVX=%i MVY=%i %i,%i  %i,%i\n", x, y, pMB->mode, pMB->cbp, pMB->mvs[0].x, bmb->pmvs[0].x, bmb->pmvs[0].y, forward.x, forward.y);
1725    
1726                          start_timer();                          start_timer();
1727                          MBCodingBVOP(frame, mb, qcoeff, bs, &pEnc->sStat);                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, &pEnc->sStat);
1728                          stop_coding_timer();                          stop_coding_timer();
1729                  }                  }
1730          }          }
# Line 1006  Line 1734 
1734          // TODO: dynamic fcode/bcode ???          // TODO: dynamic fcode/bcode ???
1735    
1736          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
   
1737  }  }
   
 */  
1738    #endif

Legend:
Removed from v.1.27  
changed lines
  Added in v.1.40

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