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

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

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

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

Legend:
Removed from v.78  
changed lines
  Added in v.619

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