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

Diff of /xvidcore/src/encoder.c

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

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

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

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