[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.7, Wed Mar 20 00:27:36 2002 UTC revision 1.71, Sun Aug 4 22:34:49 2002 UTC
# Line 1  Line 1 
1    /*****************************************************************************
2     *
3     *  XVID MPEG-4 VIDEO CODEC
4     *  -  Encoder main module  -
5     *
6     *  This program is an implementation of a part of one or more MPEG-4
7     *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending
8     *  to use this software module in hardware or software products are
9     *  advised that its use may infringe existing patents or copyrights, and
10     *  any such use would be at such party's own risk.  The original
11     *  developer of this software module and his/her company, and subsequent
12     *  editors and their companies, will have no liability for use of this
13     *  software or modifications or derivatives thereof.
14     *
15     *  This program is free software; you can redistribute it and/or modify
16     *  it under the terms of the GNU General Public License as published by
17     *  the Free Software Foundation; either version 2 of the License, or
18     *  (at your option) any later version.
19     *
20     *  This program is distributed in the hope that it will be useful,
21     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     *  GNU General Public License for more details.
24     *
25     *  You should have received a copy of the GNU General Public License
26     *  along with this program; if not, write to the Free Software
27     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
28     *
29     ****************************************************************************/
30    
31    /*****************************************************************************
32     *
33     *  History
34     *
35     *  10.07.2002  added BFRAMES_DEC_DEBUG support
36     *              MinChen <chenm001@163.com>
37     *  20.06.2002 bframe patch
38     *  08.05.2002 fix some problem in DEBUG mode;
39     *             MinChen <chenm001@163.com>
40     *  14.04.2002 added FrameCodeB()
41     *
42     *  $Id$
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 18  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  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT  #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
81    #define SWAP(A,B)    { void * tmp = A; A = B; B = tmp; }
82    
83  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits);  /*****************************************************************************
84  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header);   * 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    /*****************************************************************************
105     * 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  int encoder_create(XVID_ENC_PARAM * pParam)  static void __inline
118    image_null(IMAGE * image)
119    {
120            image->y = image->u = image->v = NULL;
121    }
122    
123    
124    /*****************************************************************************
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 49  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 71  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          if (pParam->bitrate <= 0)          /* Bitrate allocator defaults */
188                  pParam->bitrate = 900000;  
189            if (pParam->rc_bitrate <= 0)
190                    pParam->rc_bitrate = 900000;
191    
192            if (pParam->rc_reaction_delay_factor <= 0)
193                    pParam->rc_reaction_delay_factor = 16;
194    
195          if (pParam->rc_buffersize <= 0)          if (pParam->rc_averaging_period <= 0)
196                  pParam->rc_buffersize = pParam->bitrate * pParam->fbase;                  pParam->rc_averaging_period = 100;
197    
198            if (pParam->rc_buffer <= 0)
199                    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;
# Line 90  Line 206 
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    
         if (pParam->max_key_interval == 0)              /* 1 keyframe each 10 seconds */  
                 pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;  
   
209          if (pParam->max_quantizer < pParam->min_quantizer)          if (pParam->max_quantizer < pParam->min_quantizer)
210                  pParam->max_quantizer = pParam->min_quantizer;                  pParam->max_quantizer = pParam->min_quantizer;
211    
212          if ((pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), 16)) == NULL)          /* 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;                  return XVID_ERR_MEMORY;
220    
221            /* Zero the Encoder Structure */
222    
223            memset(pEnc, 0, sizeof(Encoder));
224    
225          /* Fill members of Encoder structure */          /* Fill members of Encoder structure */
226    
227          pEnc->mbParam.width = pParam->width;          pEnc->mbParam.width = pParam->width;
# Line 110  Line 233 
233          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;          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;          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;          pEnc->sStat.fMvPrevSigma = -1;
246    
247          /* Fill rate control parameters */          /* Fill rate control parameters */
248    
249          pEnc->mbParam.quant = 4;          pEnc->bitrate = pParam->rc_bitrate;
   
         pEnc->bitrate = pParam->bitrate;  
250    
251          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
252          pEnc->iMaxKeyInterval = pParam->max_key_interval;          pEnc->iMaxKeyInterval = pParam->max_key_interval;
253    
254          if (image_create(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)          /* 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    #endif
429    
430            pParam->handle = (void *) pEnc;
431    
432            if (pParam->rc_bitrate) {
433                    RateControlInit(&pEnc->rate_control, pParam->rc_bitrate,
434                                                    pParam->rc_reaction_delay_factor,
435                                                    pParam->rc_averaging_period, pParam->rc_buffer,
436                                                    pParam->fbase * 1000 / pParam->fincr,
437                                                    pParam->max_quantizer, pParam->min_quantizer);
438            }
439    
440            init_timer();
441    
442            return XVID_ERR_OK;
443    
444            /*
445             * We handle all XVID_ERR_MEMORY here, this makes the code lighter
446             */
447    #ifdef BFRAMES
448      xvid_err_memory5:
449    
450    
451            if (pEnc->mbParam.max_bframes > 0) {
452    
453                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
454                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
455                                                      pEnc->mbParam.edged_height);
456                    }
457                    xvid_free(pEnc->queue);
458            }
459    
460      xvid_err_memory4:
461    
462            if (pEnc->mbParam.max_bframes > 0) {
463    
464                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
465    
466                            if (pEnc->bframes[i] == NULL)
467                                    continue;
468    
469                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
470                                                      pEnc->mbParam.edged_height);
471    
472                            xvid_free(pEnc->bframes[i]->mbs);
473    
474                            xvid_free(pEnc->bframes[i]);
475    
476                    }
477    
478                    xvid_free(pEnc->bframes);
479            }
480    
481    #endif
482    
483      xvid_err_memory3:
484    #ifdef _DEBUG_PSNR
485            image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
486                                      pEnc->mbParam.edged_height);
487    #endif
488    
489    #ifdef BFRAMES
490            image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
491                                      pEnc->mbParam.edged_height);
492            image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
493                                      pEnc->mbParam.edged_height);
494            image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
495                                      pEnc->mbParam.edged_height);
496    #endif
497    
498            image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
499                                      pEnc->mbParam.edged_height);
500            image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
501                                      pEnc->mbParam.edged_height);
502            image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
503                                      pEnc->mbParam.edged_height);
504            image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
505                                      pEnc->mbParam.edged_height);
506            image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
507                                      pEnc->mbParam.edged_height);
508            image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
509                                      pEnc->mbParam.edged_height);
510            image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
511                                      pEnc->mbParam.edged_height);
512    
513      xvid_err_memory2:
514            xvid_free(pEnc->current->mbs);
515            xvid_free(pEnc->reference->mbs);
516    
517      xvid_err_memory1:
518            xvid_free(pEnc->current);
519            xvid_free(pEnc->reference);
520                  xvid_free(pEnc);                  xvid_free(pEnc);
521    
522            pParam->handle = NULL;
523    
524                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
525          }          }
526    
527          if (image_create(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)  /*****************************************************************************
528          {   * Encoder destruction
529                  image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);   *
530     * This function destroy the entire encoder structure created by a previous
531     * successful encoder_create call.
532     *
533     * Returned values (for now only one returned value) :
534     *    - XVID_ERR_OK     - no errors
535     *
536     ****************************************************************************/
537    
538    int
539    encoder_destroy(Encoder * pEnc)
540    {
541    #ifdef BFRAMES
542            int i;
543    #endif
544    
545            ENC_CHECK(pEnc);
546    
547            /* B Frames specific */
548    #ifdef BFRAMES
549            if (pEnc->mbParam.max_bframes > 0) {
550    
551                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
552    
553                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
554                                              pEnc->mbParam.edged_height);
555                    }
556                    xvid_free(pEnc->queue);
557            }
558    
559    
560            if (pEnc->mbParam.max_bframes > 0) {
561    
562                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
563    
564                            if (pEnc->bframes[i] == NULL)
565                                    continue;
566    
567                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
568                                              pEnc->mbParam.edged_height);
569    
570                            xvid_free(pEnc->bframes[i]->mbs);
571    
572                            xvid_free(pEnc->bframes[i]);
573                    }
574    
575                    xvid_free(pEnc->bframes);
576    
577            }
578    #endif
579    
580            /* All images, reference, current etc ... */
581    
582            image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
583                                      pEnc->mbParam.edged_height);
584            image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
585                                      pEnc->mbParam.edged_height);
586            image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
587                                      pEnc->mbParam.edged_height);
588            image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
589                                      pEnc->mbParam.edged_height);
590            image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
591                                      pEnc->mbParam.edged_height);
592            image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
593                                      pEnc->mbParam.edged_height);
594            image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
595                                      pEnc->mbParam.edged_height);
596    #ifdef BFRAMES
597            image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
598                                      pEnc->mbParam.edged_height);
599            image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
600                                      pEnc->mbParam.edged_height);
601            image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
602                                      pEnc->mbParam.edged_height);
603    #endif
604    #ifdef _DEBUG_PSNR
605            image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
606                                      pEnc->mbParam.edged_height);
607    #endif
608    
609            /* Encoder structure */
610    
611            xvid_free(pEnc->current->mbs);
612            xvid_free(pEnc->current);
613    
614            xvid_free(pEnc->reference->mbs);
615            xvid_free(pEnc->reference);
616    
617                  xvid_free(pEnc);                  xvid_free(pEnc);
618                  return XVID_ERR_MEMORY;  
619            return XVID_ERR_OK;
620          }          }
621    
622          if (image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)  
623    #ifdef BFRAMES
624    void inc_frame_num(Encoder * pEnc)
625          {          {
626                  image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
627                  image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
628                  xvid_free(pEnc);          pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase;
629                  return XVID_ERR_MEMORY;          pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;
630          }          }
631    #endif
632    
633    
634          if (image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)  #ifdef BFRAMES
635    void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
636          {          {
637                  image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
638                  image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          {
639                  image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL");
640                  xvid_free(pEnc);                  return;
                 return XVID_ERR_MEMORY;  
641          }          }
642    
643          if (image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)          DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
644                                    pEnc->bframenum_head, pEnc->bframenum_tail,
645                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
646    
647    
648            start_timer();
649            if (image_input
650                    (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
651                     pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
652                    return;
653            stop_conv_timer();
654    
655            pEnc->queue_size++;
656            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
657    }
658    #endif
659    
660    
661    #ifdef BFRAMES
662    /*****************************************************************************
663     * IPB frame encoder entry point
664     *
665     * Returned values :
666     *    - XVID_ERR_OK     - no errors
667     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
668     *                        format
669     ****************************************************************************/
670    
671    int
672    encoder_encode_bframes(Encoder * pEnc,
673                               XVID_ENC_FRAME * pFrame,
674                               XVID_ENC_STATS * pResult)
675          {          {
676                  image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          uint16_t x, y;
677                  image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          Bitstream bs;
678                  image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          uint32_t bits;
679                  image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
680                  xvid_free(pEnc);          int input_valid = 1;
681                  return XVID_ERR_MEMORY;  
682    #ifdef _DEBUG_PSNR
683            float psnr;
684            char temp[128];
685    #endif
686    
687            ENC_CHECK(pEnc);
688            ENC_CHECK(pFrame);
689            ENC_CHECK(pFrame->image);
690    
691            start_global_timer();
692    
693            BitstreamInit(&bs, pFrame->bitstream, 0);
694    
695    ipvop_loop:
696    
697            /*
698             * bframe "flush" code
699             */
700    
701            if ((pFrame->image == NULL || pEnc->flush_bframes)
702                    && (pEnc->bframenum_head < pEnc->bframenum_tail)) {
703    
704                    if (pEnc->flush_bframes == 0) {
705                            /*
706                             * we have reached the end of stream without getting
707                             * a future reference frame... so encode last final
708                             * frame as a pframe
709                             */
710    
711                            DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
712                                    pEnc->bframenum_head, pEnc->bframenum_tail,
713                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
714    
715                            pEnc->bframenum_tail--;
716                            SWAP(pEnc->current, pEnc->reference);
717    
718                            SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
719    
720                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
721    
722                            BitstreamPad(&bs);
723                            pFrame->length = BitstreamLength(&bs);
724                            pFrame->intra = 0;
725    
726                            return XVID_ERR_OK;
727                    }
728    
729    
730                    DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
731                                    pEnc->bframenum_head, pEnc->bframenum_tail,
732                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
733    
734                    FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
735                    pEnc->bframenum_head++;
736    
737                    BitstreamPad(&bs);
738                    pFrame->length = BitstreamLength(&bs);
739                    pFrame->intra = 0;
740    
741                    if (input_valid)
742                            queue_image(pEnc, pFrame);
743    
744                    return XVID_ERR_OK;
745            }
746    
747            if (pEnc->bframenum_head > 0) {
748                    pEnc->bframenum_head = pEnc->bframenum_tail = 0;
749    
750                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
751    
752                            DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
753                                    pEnc->bframenum_head, pEnc->bframenum_tail,
754                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
755    
756                            BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
757                            BitstreamPad(&bs);
758                            BitstreamPutBits(&bs, 0x7f, 8);
759    
760                            pFrame->length = BitstreamLength(&bs);
761                            pFrame->intra = 0;
762    
763                            if (input_valid)
764                                    queue_image(pEnc, pFrame);
765    
766                            return XVID_ERR_OK;
767          }          }
768            }
769    
770    
771          pEnc->pMBs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, 16);  bvop_loop:
772          if (pEnc->pMBs == NULL)  
773            if (pEnc->bframenum_dx50bvop != -1)
774          {          {
775                  image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
776                  image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  SWAP(pEnc->current, pEnc->reference);
777                  image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
778                  image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
779                  image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
780                  xvid_free(pEnc);                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
                 return XVID_ERR_MEMORY;  
781          }          }
782    
783          // init macroblock array                  if (input_valid)
         for (i = 0; i < pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; i++)  
784          {          {
785                  pEnc->pMBs[i].dquant = NO_CHANGE;                          queue_image(pEnc, pFrame);
786                            input_valid = 0;
787          }          }
788    
789          pParam->handle = (void *)pEnc;          } else if (input_valid) {
790    
791                    SWAP(pEnc->current, pEnc->reference);
792    
793                    start_timer();
794                    if (image_input
795                            (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
796                            pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
797                            return XVID_ERR_FORMAT;
798                    stop_conv_timer();
799    
800          if (pParam->bitrate)                  // queue input frame, and dequue next image
801                    if (pEnc->queue_size > 0)
802          {          {
803                  RateControlInit(pParam->bitrate, pParam->rc_buffersize, pParam->fbase, pParam->width,                          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
804                                  pParam->height, pParam->max_quantizer, pParam->min_quantizer);                          if (pEnc->queue_head != pEnc->queue_tail)
805                            {
806                                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
807                            }
808                            pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
809                            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
810          }          }
811    
812          create_vlc_tables();          } else if (pEnc->queue_size > 0) {
813          init_timer();  
814                    SWAP(pEnc->current, pEnc->reference);
815    
816                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
817                    pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
818                    pEnc->queue_size--;
819    
820            } else if (BitstreamPos(&bs) == 0) {
821    
822                    DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
823                                    pEnc->bframenum_head, pEnc->bframenum_tail,
824                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
825    
826                    pFrame->intra = 0;
827    
828                    BitstreamPutBits(&bs, 0x7f, 8);
829                    BitstreamPad(&bs);
830                    pFrame->length = BitstreamLength(&bs);
831    
832                    return XVID_ERR_OK;
833    
834            } else {
835    
836                    pFrame->length = BitstreamLength(&bs);
837          return XVID_ERR_OK;          return XVID_ERR_OK;
838  }  }
839    
840            pEnc->flush_bframes = 0;
841    
842            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
843             * Well there was a separation here so i put it in ANSI C
844             * comment style :-)
845             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
846    
847            emms();
848    
849  int encoder_destroy(Encoder * pEnc)          // only inc frame num, adapt quant, etc. if we havent seen it before
850            if (pEnc->bframenum_dx50bvop < 0 )
851  {  {
852          ENC_CHECK(pEnc);                  if (pFrame->quant == 0)
853          ENC_CHECK(pEnc->sCurrent.y);                          pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
854          ENC_CHECK(pEnc->sReference.y);                  else
855                            pEnc->current->quant = pFrame->quant;
856    
857          xvid_free(pEnc->pMBs);  /*              if (pEnc->current->quant < 1)
858          image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                          pEnc->current->quant = 1;
859          image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
860          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  if (pEnc->current->quant > 31)
861          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                          pEnc->current->quant = 31;
862          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  */
863          xvid_free(pEnc);                  pEnc->current->global_flags = pFrame->general;
864                    pEnc->current->motion_flags = pFrame->motion;
865    
866                    /* ToDo : dynamic fcode (in both directions) */
867                    pEnc->current->fcode = pEnc->mbParam.m_fcode;
868                    pEnc->current->bcode = pEnc->mbParam.m_fcode;
869    
870                    pEnc->current->seconds = pEnc->mbParam.m_seconds;
871                    pEnc->current->ticks = pEnc->mbParam.m_ticks;
872    
873                    inc_frame_num(pEnc);
874    
875    #ifdef _DEBUG_PSNR
876                    image_copy(&pEnc->sOriginal, &pEnc->current->image,
877                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
878    #endif
879    
880                    emms();
881    
882                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
883                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
884                                    "%i  if:%i  st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks);
885                    }
886    
887            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
888             * Luminance masking
889             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
890    
891                    if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
892                            int *temp_dquants =
893                                    (int *) xvid_malloc(pEnc->mbParam.mb_width *
894                                                                    pEnc->mbParam.mb_height * sizeof(int),
895                                                                    CACHE_LINE);
896    
897                            pEnc->current->quant =
898                                    adaptive_quantization(pEnc->current->image.y,
899                                                                      pEnc->mbParam.edged_width, temp_dquants,
900                                                                      pEnc->current->quant, pEnc->current->quant,
901                                                                      2 * pEnc->current->quant,
902                                                                      pEnc->mbParam.mb_width,
903                                                                      pEnc->mbParam.mb_height);
904    
905                            for (y = 0; y < pEnc->mbParam.mb_height; y++) {
906    
907    #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
908    
909                                    for (x = 0; x < pEnc->mbParam.mb_width; x++) {
910                                            MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
911    
912                                            pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
913                                    }
914    
915    #undef OFFSET
916                            }
917    
918                            xvid_free(temp_dquants);
919                    }
920    
921            }
922    
923            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
924             * ivop/pvop/bvop selection
925             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
926    
927          destroy_vlc_tables();  
928            if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||
929                    (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&
930                     pEnc->iFrameNum >= pEnc->iMaxKeyInterval)
931                    || image_mad(&pEnc->reference->image, &pEnc->current->image,
932                                             pEnc->mbParam.edged_width, pEnc->mbParam.width,
933                                             pEnc->mbParam.height) > 30) {
934                    /*
935                     * This will be coded as an Intra Frame
936                     */
937    
938                    DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
939                                    pEnc->bframenum_head, pEnc->bframenum_tail,
940                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
941    
942                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
943                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
944                    }
945    
946                    // when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe
947    
948                    if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
949    
950                            pEnc->bframenum_tail--;
951                            pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
952    
953                            SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
954                            if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
955                                    image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
956                            }
957                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
958    
959                            pFrame->intra = 0;
960    
961                    } else {
962    
963                            FrameCodeI(pEnc, &bs, &bits);
964                            pFrame->intra = 1;
965    
966                            pEnc->bframenum_dx50bvop = -1;
967                    }
968    
969                    pEnc->flush_bframes = 1;
970    
971                    if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
972                            BitstreamPad(&bs);
973                            input_valid = 0;
974                            goto ipvop_loop;
975                    }
976    
977                    /*
978                     * NB : sequences like "IIBB" decode fine with msfdam but,
979                     *      go screwy with divx 5.00
980                     */
981            } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
982                    /*
983                     * This will be coded as a Predicted Frame
984                     */
985    
986                    DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
987                                    pEnc->bframenum_head, pEnc->bframenum_tail,
988                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
989    
990                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
991                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
992                    }
993    
994                    FrameCodeP(pEnc, &bs, &bits, 1, 0);
995                    pFrame->intra = 0;
996                    pEnc->flush_bframes = 1;
997    
998                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
999                            BitstreamPad(&bs);
1000                            input_valid = 0;
1001                            goto ipvop_loop;
1002                    }
1003    
1004            } else {
1005                    /*
1006                     * This will be coded as a Bidirectional Frame
1007                     */
1008    
1009                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
1010                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1011                    }
1012    
1013                    if (pFrame->bquant < 1) {
1014                            pEnc->current->quant =
1015                                    ((pEnc->reference->quant +
1016                                      pEnc->current->quant) * pEnc->bquant_ratio) / 200;
1017                    } else {
1018                            pEnc->current->quant = pFrame->bquant;
1019                    }
1020                    if (pEnc->current->quant < 1)
1021                            pEnc->current->quant = 1;
1022    
1023                    if (pEnc->current->quant > 31)
1024                            pEnc->current->quant = 31;
1025    
1026    
1027                            DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1028                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1029                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1030    
1031    
1032    
1033                    /* store frame into bframe buffer & swap ref back to current */
1034                    SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1035                    SWAP(pEnc->current, pEnc->reference);
1036    
1037                    pEnc->bframenum_tail++;
1038    
1039                    pFrame->intra = 0;
1040                    pFrame->length = 0;
1041    
1042                    input_valid = 0;
1043                    goto bvop_loop;
1044            }
1045    
1046            pEnc->iFrameNum++;
1047    
1048            BitstreamPad(&bs);
1049            pFrame->length = BitstreamLength(&bs);
1050    
1051            if (pResult) {
1052                    pResult->quant = pEnc->current->quant;
1053                    pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
1054                    pResult->kblks = pEnc->sStat.kblks;
1055                    pResult->mblks = pEnc->sStat.mblks;
1056                    pResult->ublks = pEnc->sStat.ublks;
1057            }
1058    
1059            emms();
1060    
1061    #ifdef _DEBUG_PSNR
1062            psnr =
1063                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1064                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
1065                                       pEnc->mbParam.height);
1066    
1067            snprintf(temp, 127, "PSNR: %f\n", psnr);
1068            DEBUG(temp);
1069    #endif
1070    
1071            if (pFrame->quant == 0) {
1072                    RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1073                                                      pFrame->length, pFrame->intra);
1074            }
1075    
1076    
1077            stop_global_timer();
1078            write_timer();
1079    
1080          return XVID_ERR_OK;          return XVID_ERR_OK;
1081  }  }
1082    
1083  int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult)  #endif
1084    
1085    
1086    
1087    /*****************************************************************************
1088     * "original" IP frame encoder entry point
1089     *
1090     * Returned values :
1091     *    - XVID_ERR_OK     - no errors
1092     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
1093     *                        format
1094     ****************************************************************************/
1095    
1096    int
1097    encoder_encode(Encoder * pEnc,
1098                               XVID_ENC_FRAME * pFrame,
1099                               XVID_ENC_STATS * pResult)
1100  {  {
1101          uint16_t x, y;          uint16_t x, y;
1102          Bitstream bs;          Bitstream bs;
1103          uint32_t bits;          uint32_t bits;
1104          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
1105    
1106    #ifdef _DEBUG_PSNR
1107            float psnr;
1108            uint8_t temp[128];
1109    #endif
1110    
1111          start_global_timer();          start_global_timer();
1112    
1113          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
# Line 227  Line 1115 
1115          ENC_CHECK(pFrame->bitstream);          ENC_CHECK(pFrame->bitstream);
1116          ENC_CHECK(pFrame->image);          ENC_CHECK(pFrame->image);
1117    
1118          pEnc->mbParam.global_flags = pFrame->general;          SWAP(pEnc->current, pEnc->reference);
1119          pEnc->mbParam.motion_flags = pFrame->motion;  
1120            pEnc->current->global_flags = pFrame->general;
1121            pEnc->current->motion_flags = pFrame->motion;
1122    #ifdef BFRAMES
1123            pEnc->current->seconds = pEnc->mbParam.m_seconds;
1124            pEnc->current->ticks = pEnc->mbParam.m_ticks;
1125    #endif
1126            pEnc->mbParam.hint = &pFrame->hint;
1127    
1128          start_timer();          start_timer();
1129          if (image_input(&pEnc->sCurrent, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width,          if (image_input
1130                          pFrame->image, pFrame->colorspace))                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
1131          {                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0)
1132                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
         }  
1133          stop_conv_timer();          stop_conv_timer();
1134    
1135          EMMS();  #ifdef _DEBUG_PSNR
1136            image_copy(&pEnc->sOriginal, &pEnc->current->image,
1137                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
1138    #endif
1139    
1140            emms();
1141    
1142          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
1143    
1144          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
1145          {                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
1146                  pEnc->mbParam.quant = RateControlGetQ(0);          } else {
1147          }                  pEnc->current->quant = pFrame->quant;
         else  
         {  
                 pEnc->mbParam.quant = pFrame->quant;  
1148          }          }
1149    
1150          if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0)          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1151          {                  int *temp_dquants =
1152                  int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), 16);                          (int *) xvid_malloc(pEnc->mbParam.mb_width *
1153                                                                    pEnc->mbParam.mb_height * sizeof(int),
1154                                                                    CACHE_LINE);
1155    
1156                  pEnc->mbParam.quant = adaptive_quantization(pEnc->sCurrent.y, pEnc->mbParam.width,                  pEnc->current->quant =
1157                                                              temp_dquants, pFrame->quant, pFrame->quant,                          adaptive_quantization(pEnc->current->image.y,
1158                                                              2*pFrame->quant, pEnc->mbParam.mb_width, pEnc->mbParam.mb_height);                                                                    pEnc->mbParam.edged_width, temp_dquants,
1159                                                                      pEnc->current->quant, pEnc->current->quant,
1160                                                                      2 * pEnc->current->quant,
1161                                                                      pEnc->mbParam.mb_width,
1162                                                                      pEnc->mbParam.mb_height);
1163    
1164                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                  for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1165                          for (x = 0; x < pEnc->mbParam.mb_width; x++)  
1166                          {  #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
1167                                  MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];  
1168                                  pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)];                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1169    
1170    
1171                                    MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
1172    
1173                                    pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
1174                          }                          }
1175    
1176    #undef OFFSET
1177                    }
1178    
1179                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
1180          }          }
1181    
1182          if(pEnc->mbParam.global_flags & XVID_H263QUANT) {          if (pEnc->current->global_flags & XVID_H263QUANT) {
1183                  if(pEnc->mbParam.quant_type != H263_QUANT)                  if (pEnc->mbParam.m_quant_type != H263_QUANT)
1184                          write_vol_header = 1;                          write_vol_header = 1;
1185                  pEnc->mbParam.quant_type = H263_QUANT;                  pEnc->mbParam.m_quant_type = H263_QUANT;
1186          }          } else if (pEnc->current->global_flags & XVID_MPEGQUANT) {
1187          else if(pEnc->mbParam.global_flags & XVID_MPEGQUANT) {                  int matrix1_changed, matrix2_changed;
1188                  int ret1, ret2;  
1189                    matrix1_changed = matrix2_changed = 0;
1190    
1191                  if(pEnc->mbParam.quant_type != MPEG4_QUANT)                  if (pEnc->mbParam.m_quant_type != MPEG4_QUANT)
1192                          write_vol_header = 1;                          write_vol_header = 1;
1193    
1194                  pEnc->mbParam.quant_type = MPEG4_QUANT;                  pEnc->mbParam.m_quant_type = MPEG4_QUANT;
1195    
1196                  if ((pEnc->mbParam.global_flags & XVID_CUSTOM_QMATRIX) > 0) {                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1197                          if(pFrame->quant_intra_matrix != NULL)                          if(pFrame->quant_intra_matrix != NULL)
1198                                  ret1 = set_intra_matrix(pFrame->quant_intra_matrix);                                  matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix);
1199                          if(pFrame->quant_inter_matrix != NULL)                          if(pFrame->quant_inter_matrix != NULL)
1200                                  ret2 = set_inter_matrix(pFrame->quant_inter_matrix);                                  matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix);
1201                  }                  } else {
1202                  else {                          matrix1_changed = set_intra_matrix(get_default_intra_matrix());
1203                          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());  
1204                  }                  }
1205                  if(write_vol_header == 0)                  if(write_vol_header == 0)
1206                          write_vol_header = ret1 | ret2;                          write_vol_header = matrix1_changed | matrix2_changed;
1207          }          }
1208    
1209          if (pFrame->intra < 0)          if (pFrame->intra < 0) {
1210          {                  if ((pEnc->iFrameNum == 0)
1211                  if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0)                          || ((pEnc->iMaxKeyInterval > 0)
1212                                                 && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval)))                                  && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {
   
1213                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1214                  else                  } else {
1215                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
1216          }          }
1217          else          } else {
1218          {                  if (pFrame->intra == 1) {
                 if (pFrame->intra == 1)  
1219                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1220                  else                  } else {
1221                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);
1222          }          }
1223    
1224            }
1225    
1226          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1227          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1228          BitstreamPad(&bs);          BitstreamPad(&bs);
1229          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1230    
1231          if (pResult)          if (pResult) {
1232          {                  pResult->quant = pEnc->current->quant;
                 pResult->quant = pEnc->mbParam.quant;  
1233                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
1234                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->sStat.kblks;
1235                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->sStat.mblks;
1236                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
1237          }          }
1238    
1239          EMMS();          emms();
   
         if (pFrame->quant == 0)  
         {  
                 RateControlUpdate(pEnc->mbParam.quant, pFrame->length, pFrame->intra);  
         }  
1240    
1241            if (pFrame->quant == 0) {
1242                    RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1243                                                      pFrame->length, pFrame->intra);
1244            }
1245    #ifdef _DEBUG_PSNR
1246            psnr =
1247                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1248                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
1249                                       pEnc->mbParam.height);
1250    
1251            snprintf(temp, 127, "PSNR: %f\n", psnr);
1252            DEBUG(temp);
1253    #endif
1254    
1255    #ifdef BFRAMES
1256            inc_frame_num(pEnc);
1257    #endif
1258          pEnc->iFrameNum++;          pEnc->iFrameNum++;
         image_swap(&pEnc->sCurrent, &pEnc->sReference);  
1259    
1260          stop_global_timer();          stop_global_timer();
1261          write_timer();          write_timer();
# Line 343  Line 1264 
1264  }  }
1265    
1266    
1267  static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) {  static __inline void
1268    CodeIntraMB(Encoder * pEnc,
1269                            MACROBLOCK * pMB)
1270    {
1271    
1272          pMB->mode = MODE_INTRA;          pMB->mode = MODE_INTRA;
1273    
1274          if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) {          /* zero mv statistics */
1275                  if(pMB->dquant != NO_CHANGE)          pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0;
1276                  {          pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0;
1277            pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;
1278            pMB->sad16 = 0;
1279    
1280            if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1281                    if (pMB->dquant != NO_CHANGE) {
1282                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
1283                          pEnc->mbParam.quant += DQtab[pMB->dquant];                          pEnc->current->quant += DQtab[pMB->dquant];
1284    
1285                          if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31;                          if (pEnc->current->quant > 31)
1286                          if (pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1;                                  pEnc->current->quant = 31;
1287                            if (pEnc->current->quant < 1)
1288                                    pEnc->current->quant = 1;
1289                    }
1290                  }                  }
1291    
1292            pMB->quant = pEnc->current->quant;
1293          }          }
1294    
1295          pMB->quant = pEnc->mbParam.quant;  
1296    #define FCODEBITS       3
1297    #define MODEBITS        5
1298    
1299    void
1300    HintedMESet(Encoder * pEnc,
1301                            int *intra)
1302    {
1303            HINTINFO *hint;
1304            Bitstream bs;
1305            int length, high;
1306            uint32_t x, y;
1307    
1308            hint = pEnc->mbParam.hint;
1309    
1310            if (hint->rawhints) {
1311                    *intra = hint->mvhint.intra;
1312            } else {
1313                    BitstreamInit(&bs, hint->hintstream, hint->hintlength);
1314                    *intra = BitstreamGetBit(&bs);
1315            }
1316    
1317            if (*intra) {
1318                    return;
1319            }
1320    
1321            pEnc->current->fcode =
1322                    (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs,
1323                                                                                                                                     FCODEBITS);
1324    
1325            length = pEnc->current->fcode + 5;
1326            high = 1 << (length - 1);
1327    
1328            for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1329                    for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1330                            MACROBLOCK *pMB =
1331                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1332                            MVBLOCKHINT *bhint =
1333                                    &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1334                            VECTOR pred;
1335                            VECTOR tmp;
1336                            int vec;
1337    
1338                            pMB->mode =
1339                                    (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs,
1340                                                                                                                                      MODEBITS);
1341    
1342                            pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
1343                            pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
1344    
1345                            if (pMB->mode == MODE_INTER) {
1346                                    tmp.x =
1347                                            (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs,
1348                                                                                                                                                      length);
1349                                    tmp.y =
1350                                            (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs,
1351                                                                                                                                                      length);
1352                                    tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1353                                    tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1354    
1355                                    pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,0);
1356    
1357                                    for (vec = 0; vec < 4; ++vec) {
1358                                            pMB->mvs[vec].x = tmp.x;
1359                                            pMB->mvs[vec].y = tmp.y;
1360                                            pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
1361                                            pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
1362                                    }
1363                            } else if (pMB->mode == MODE_INTER4V) {
1364                                    for (vec = 0; vec < 4; ++vec) {
1365                                            tmp.x =
1366                                                    (hint->rawhints) ? bhint->mvs[vec].
1367                                                    x : BitstreamGetBits(&bs, length);
1368                                            tmp.y =
1369                                                    (hint->rawhints) ? bhint->mvs[vec].
1370                                                    y : BitstreamGetBits(&bs, length);
1371                                            tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1372                                            tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1373    
1374                                            pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,vec);
1375    
1376                                            pMB->mvs[vec].x = tmp.x;
1377                                            pMB->mvs[vec].y = tmp.y;
1378                                            pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
1379                                            pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
1380                                    }
1381                            } else                          // intra / stuffing / not_coded
1382                            {
1383                                    for (vec = 0; vec < 4; ++vec) {
1384                                            pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
1385                                    }
1386                            }
1387    
1388                            if (pMB->mode == MODE_INTER4V &&
1389                                    (pEnc->current->global_flags & XVID_LUMIMASKING)
1390                                    && pMB->dquant != NO_CHANGE) {
1391                                    pMB->mode = MODE_INTRA;
1392    
1393                                    for (vec = 0; vec < 4; ++vec) {
1394                                            pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
1395                                    }
1396                            }
1397                    }
1398            }
1399  }  }
1400    
1401    
1402  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits)  void
1403    HintedMEGet(Encoder * pEnc,
1404                            int intra)
1405  {  {
1406          int16_t dct_codes[6][64];          HINTINFO *hint;
1407          int16_t qcoeff[6][64];          Bitstream bs;
1408            uint32_t x, y;
1409            int length, high;
1410    
1411            hint = pEnc->mbParam.hint;
1412    
1413            if (hint->rawhints) {
1414                    hint->mvhint.intra = intra;
1415            } else {
1416                    BitstreamInit(&bs, hint->hintstream, 0);
1417                    BitstreamPutBit(&bs, intra);
1418            }
1419    
1420            if (intra) {
1421                    if (!hint->rawhints) {
1422                            BitstreamPad(&bs);
1423                            hint->hintlength = BitstreamLength(&bs);
1424                    }
1425                    return;
1426            }
1427    
1428            length = pEnc->current->fcode + 5;
1429            high = 1 << (length - 1);
1430    
1431            if (hint->rawhints) {
1432                    hint->mvhint.fcode = pEnc->current->fcode;
1433            } else {
1434                    BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);
1435            }
1436    
1437            for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1438                    for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1439                            MACROBLOCK *pMB =
1440                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1441                            MVBLOCKHINT *bhint =
1442                                    &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1443                            VECTOR tmp;
1444    
1445                            if (hint->rawhints) {
1446                                    bhint->mode = pMB->mode;
1447                            } else {
1448                                    BitstreamPutBits(&bs, pMB->mode, MODEBITS);
1449                            }
1450    
1451                            if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
1452                                    tmp.x = pMB->mvs[0].x;
1453                                    tmp.y = pMB->mvs[0].y;
1454                                    tmp.x += (tmp.x < 0) ? high * 2 : 0;
1455                                    tmp.y += (tmp.y < 0) ? high * 2 : 0;
1456    
1457                                    if (hint->rawhints) {
1458                                            bhint->mvs[0].x = tmp.x;
1459                                            bhint->mvs[0].y = tmp.y;
1460                                    } else {
1461                                            BitstreamPutBits(&bs, tmp.x, length);
1462                                            BitstreamPutBits(&bs, tmp.y, length);
1463                                    }
1464                            } else if (pMB->mode == MODE_INTER4V) {
1465                                    int vec;
1466    
1467                                    for (vec = 0; vec < 4; ++vec) {
1468                                            tmp.x = pMB->mvs[vec].x;
1469                                            tmp.y = pMB->mvs[vec].y;
1470                                            tmp.x += (tmp.x < 0) ? high * 2 : 0;
1471                                            tmp.y += (tmp.y < 0) ? high * 2 : 0;
1472    
1473                                            if (hint->rawhints) {
1474                                                    bhint->mvs[vec].x = tmp.x;
1475                                                    bhint->mvs[vec].y = tmp.y;
1476                                            } else {
1477                                                    BitstreamPutBits(&bs, tmp.x, length);
1478                                                    BitstreamPutBits(&bs, tmp.y, length);
1479                                            }
1480                                    }
1481                            }
1482                    }
1483            }
1484    
1485            if (!hint->rawhints) {
1486                    BitstreamPad(&bs);
1487                    hint->hintlength = BitstreamLength(&bs);
1488            }
1489    }
1490    
1491    
1492    static int
1493    FrameCodeI(Encoder * pEnc,
1494                       Bitstream * bs,
1495                       uint32_t * pBits)
1496    {
1497    
1498            DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1499            DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1500    
1501          uint16_t x, y;          uint16_t x, y;
1502    
1503          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
1504          pEnc->mbParam.rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1505          pEnc->mbParam.coding_type = I_VOP;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1506            pEnc->current->coding_type = I_VOP;
1507          BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);  
1508          BitstreamWriteVopHeader(bs, I_VOP, pEnc->mbParam.rounding_type,          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1509                                  pEnc->mbParam.quant,  #ifdef BFRAMES
1510                                  pEnc->mbParam.fixed_code);  #define DIVX501B481P "DivX501b481p"
1511            if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1512                    BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));
1513            }
1514    #endif
1515            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1516    
1517          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1518    
# Line 384  Line 1521 
1521          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1522    
1523          for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++)
1524                  for (x = 0; x < pEnc->mbParam.mb_width; x++)                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1525                  {                          MACROBLOCK *pMB =
1526                          MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1527    
1528                          CodeIntraMB(pEnc, pMB);                          CodeIntraMB(pEnc, pMB);
1529    
1530                          MBTransQuantIntra(&pEnc->mbParam, x, y, dct_codes, qcoeff, &pEnc->sCurrent);                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1531                                                              dct_codes, qcoeff);
1532    
1533                          start_timer();                          start_timer();
1534                          MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1535                          stop_prediction_timer();                          stop_prediction_timer();
1536    
1537                          start_timer();                          start_timer();
1538                          MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat);                          if (pEnc->current->global_flags & XVID_GREYSCALE)
1539                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1540                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1541                                    qcoeff[5*64+0]=0;
1542                            }
1543                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1544                          stop_coding_timer();                          stop_coding_timer();
1545                  }                  }
1546    
# Line 407  Line 1550 
1550          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
1551          pEnc->sStat.iMvSum = 0;          pEnc->sStat.iMvSum = 0;
1552          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
1553          pEnc->mbParam.fixed_code = 2;          pEnc->mbParam.m_fcode = 2;
1554    
1555            if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1556                    HintedMEGet(pEnc, 1);
1557            }
1558    
1559          return 1;                                        // intra          return 1;                                        // intra
1560  }  }
1561    
1562    
1563  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1564    #define BFRAME_SKIP_THRESHHOLD 16
1565    
1566  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header)  static int
1567    FrameCodeP(Encoder * pEnc,
1568                       Bitstream * bs,
1569                       uint32_t * pBits,
1570                       bool force_inter,
1571                       bool vol_header)
1572  {  {
1573          float fSigma;          float fSigma;
1574          int16_t dct_codes[6][64];  
1575          int16_t qcoeff[6][64];          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1576            DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1577    
1578          int iLimit;          int iLimit;
1579          uint32_t x, y;          int x, y, k;
1580          int iSearchRange;          int iSearchRange;
1581          bool bIntra;          int bIntra;
1582    
1583          IMAGE *pCurrent = &pEnc->sCurrent;          /* IMAGE *pCurrent = &pEnc->current->image; */
1584          IMAGE *pRef = &pEnc->sReference;          IMAGE *pRef = &pEnc->reference->image;
1585    
1586          image_setedges(pRef,pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height);          start_timer();
1587            image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1588          pEnc->mbParam.rounding_type = 1 - pEnc->mbParam.rounding_type;                                     pEnc->mbParam.width, pEnc->mbParam.height,
1589                                       pEnc->current->global_flags & XVID_INTERLACING);
1590            stop_edges_timer();
1591    
1592            pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
1593            pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1594            pEnc->current->fcode = pEnc->mbParam.m_fcode;
1595    
1596          if (!force_inter)          if (!force_inter)
1597                  iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD);                  iLimit =
1598                            (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height *
1599                                       INTRA_THRESHOLD);
1600          else          else
1601                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;
1602    
1603          if ((pEnc->mbParam.global_flags & XVID_HALFPEL) > 0) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
1604                  start_timer();                  start_timer();
1605                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1606                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1607                                    pEnc->mbParam.rounding_type);                                                    pEnc->mbParam.edged_height,
1608                                                      pEnc->current->rounding_type);
1609                  stop_inter_timer();                  stop_inter_timer();
1610          }          }
1611    
1612          start_timer();          start_timer();
1613          bIntra = MotionEstimation(pEnc->pMBs, &pEnc->mbParam, &pEnc->sReference,          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
1614                                    &pEnc->vInterH, &pEnc->vInterV,                  HintedMESet(pEnc, &bIntra);
1615                                    &pEnc->vInterHV, &pEnc->sCurrent, iLimit);          } else {
1616    
1617    #ifdef _SMP
1618            if (pEnc->mbParam.num_threads > 1)
1619                    bIntra =
1620                            SMP_MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1621                                                     &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1622                                                     iLimit);
1623            else
1624    #endif
1625                    bIntra =
1626                            MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1627                             &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1628                             iLimit);
1629    
1630            }
1631          stop_motion_timer();          stop_motion_timer();
1632    
1633          if (bIntra == 1)          if (bIntra == 1) {
1634                  return FrameCodeI(pEnc, bs, pBits);                  return FrameCodeI(pEnc, bs, pBits);
1635            }
1636    
1637          pEnc->mbParam.coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
1638    
1639          if(vol_header)          if(vol_header)
1640                  BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1641    
1642          BitstreamWriteVopHeader(bs, P_VOP, pEnc->mbParam.rounding_type,          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
                                 pEnc->mbParam.quant,  
                                 pEnc->mbParam.fixed_code);  
1643    
1644          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1645    
# Line 470  Line 1648 
1648          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
1649          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1650    
1651          for(y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1652          {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1653                  for(x = 0; x < pEnc->mbParam.mb_width; x++)                          MACROBLOCK *pMB =
1654                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
                         MACROBLOCK * pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];  
1655    
1656                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1657    
1658                          if (!bIntra)                          if (!bIntra) {
                         {  
1659                                  start_timer();                                  start_timer();
1660                                  MBMotionCompensation(pMB, x, y, &pEnc->sReference,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1661                                                       &pEnc->vInterH, &pEnc->vInterV,                                                       &pEnc->vInterH, &pEnc->vInterV,
1662                                                       &pEnc->vInterHV, &pEnc->sCurrent, dct_codes,                                                                           &pEnc->vInterHV, &pEnc->current->image,
1663                                                       pEnc->mbParam.width,                                                                           dct_codes, pEnc->mbParam.width,
1664                                                       pEnc->mbParam.height,                                                       pEnc->mbParam.height,
1665                                                       pEnc->mbParam.edged_width,                                                       pEnc->mbParam.edged_width,
1666                                                       pEnc->mbParam.rounding_type);                                                                           pEnc->current->rounding_type);
1667                                  stop_comp_timer();                                  stop_comp_timer();
1668    
1669                                  if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) {                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1670                                          if(pMB->dquant != NO_CHANGE) {                                          if(pMB->dquant != NO_CHANGE) {
1671                                                  pMB->mode = MODE_INTER_Q;                                                  pMB->mode = MODE_INTER_Q;
1672                                                  pEnc->mbParam.quant += DQtab[pMB->dquant];                                                  pEnc->current->quant += DQtab[pMB->dquant];
1673                                                  if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31;                                                  if (pEnc->current->quant > 31)
1674                                                  else if(pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1;                                                          pEnc->current->quant = 31;
1675                                                    else if (pEnc->current->quant < 1)
1676                                                            pEnc->current->quant = 1;
1677                                          }                                          }
1678                                  }                                  }
1679                                  pMB->quant = pEnc->mbParam.quant;                                  pMB->quant = pEnc->current->quant;
1680    
1681                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, x, y, dct_codes, qcoeff, pCurrent);                                  pMB->field_pred = 0;
1682                          }  
1683                          else                                  pMB->cbp =
1684                          {                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1685                                                                              dct_codes, qcoeff);
1686                            } else {
1687                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
1688                                  MBTransQuantIntra(&pEnc->mbParam, x, y, dct_codes, qcoeff, pCurrent);                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1689                                                                      dct_codes, qcoeff);
1690                          }                          }
1691    
1692                          start_timer();                          start_timer();
1693                          MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1694                          stop_prediction_timer();                          stop_prediction_timer();
1695    
1696                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
                         {  
1697                                  pEnc->sStat.kblks++;                                  pEnc->sStat.kblks++;
1698                            } else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1699                                               pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1700                                               pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1701                                    pEnc->sStat.mblks++;
1702                            }  else {
1703                                    pEnc->sStat.ublks++;
1704                          }                          }
1705                          else if (pMB->cbp ||  
1706                                   pMB->mvs[0].x || pMB->mvs[0].y ||                          start_timer();
1707                                   pMB->mvs[1].x || pMB->mvs[1].y ||  
1708                                   pMB->mvs[2].x || pMB->mvs[2].y ||                          /* Finished processing the MB, now check if to CODE or SKIP */
1709                                   pMB->mvs[3].x || pMB->mvs[3].y)  
1710                            if (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 &&
1711                                    pMB->mvs[0].y == 0) {
1712    
1713    /* This is a candidate for SKIPping, but check intermediate B-frames first */
1714    
1715    #ifdef BFRAMES
1716                                    int iSAD=BFRAME_SKIP_THRESHHOLD;
1717                                    int bSkip=1;
1718    
1719                                    for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1720                          {                          {
1721                                  pEnc->sStat.mblks++;                                          iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1722                                                                    pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1723                                                                    pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
1724                                            if (iSAD >= BFRAME_SKIP_THRESHHOLD)
1725                                            {       bSkip = 0;
1726                                                    break;
1727                          }                          }
1728                          else                                  }
1729                                    if (!bSkip)
1730                          {                          {
1731                                  pEnc->sStat.ublks++;                                          if (pEnc->current->global_flags & XVID_GREYSCALE)
1732                                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1733                                                    qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1734                                                    qcoeff[5*64+0]=0;
1735                                            }
1736                                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1737                                            pMB->cbp = 0x80;                /* trick! so cbp!=0, but still nothing is written to bs */
1738                                    }
1739                                    else
1740                                            MBSkip(bs);
1741    
1742    #else
1743                                            MBSkip(bs);     /* without B-frames, no precautions are needed */
1744    
1745    #endif
1746    
1747                            } else {
1748                                    if (pEnc->current->global_flags & XVID_GREYSCALE)
1749                                    {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1750                                            qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1751                                            qcoeff[5*64+0]=0;
1752                                    }
1753                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1754                          }                          }
1755    
                         start_timer();  
                         MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat);  
1756                          stop_coding_timer();                          stop_coding_timer();
1757                  }                  }
1758          }          }
1759    
1760          emms();          emms();
1761    
1762            if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1763                    HintedMEGet(pEnc, 0);
1764            }
1765    
1766          if (pEnc->sStat.iMvCount == 0)          if (pEnc->sStat.iMvCount == 0)
1767                  pEnc->sStat.iMvCount = 1;                  pEnc->sStat.iMvCount = 1;
1768    
1769          fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);          fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);
1770    
1771          iSearchRange = 1 << (3 + pEnc->mbParam.fixed_code);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
1772    
1773          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
1774              && (pEnc->mbParam.fixed_code <= 3)) // maximum search range 128                  && (pEnc->mbParam.m_fcode <= 3))        // maximum search range 128
1775          {          {
1776                  pEnc->mbParam.fixed_code++;                  pEnc->mbParam.m_fcode++;
1777                  iSearchRange *= 2;                  iSearchRange *= 2;
1778          }          } else if ((fSigma < iSearchRange / 6)
         else if ((fSigma < iSearchRange / 6)  
1779                   && (pEnc->sStat.fMvPrevSigma >= 0)                   && (pEnc->sStat.fMvPrevSigma >= 0)
1780                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)
1781                   && (pEnc->mbParam.fixed_code >= 2))    // minimum search range 16                             && (pEnc->mbParam.m_fcode >= 2))     // minimum search range 16
1782          {          {
1783                  pEnc->mbParam.fixed_code--;                  pEnc->mbParam.m_fcode--;
1784                  iSearchRange /= 2;                  iSearchRange /= 2;
1785          }          }
1786    
1787          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->sStat.fMvPrevSigma = fSigma;
1788    
1789    #ifdef BFRAMES
1790            /* frame drop code */
1791            // DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks);
1792            if (pEnc->sStat.kblks + pEnc->sStat.mblks <
1793                    (pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100)
1794            {
1795                    pEnc->sStat.kblks = pEnc->sStat.mblks = 0;
1796                    pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1797    
1798                    BitstreamReset(bs);
1799                    BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
1800    
1801                    // copy reference frame details into the current frame
1802                    pEnc->current->quant = pEnc->reference->quant;
1803                    pEnc->current->motion_flags = pEnc->reference->motion_flags;
1804                    pEnc->current->rounding_type = pEnc->reference->rounding_type;
1805                    pEnc->current->fcode = pEnc->reference->fcode;
1806                    pEnc->current->bcode = pEnc->reference->bcode;
1807                    image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
1808                    memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);
1809    
1810            }
1811    #endif
1812    
1813          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1814    
1815    #ifdef BFRAMES
1816            pEnc->time_pp = ((int32_t)pEnc->mbParam.fbase - (int32_t)pEnc->last_pframe + (int32_t)pEnc->current->ticks) %
1817                            (int32_t)pEnc->mbParam.fbase;
1818            pEnc->last_pframe = pEnc->current->ticks;
1819    #endif
1820    
1821          return 0;                                        // inter          return 0;                                        // inter
1822  }  }
1823    
1824    
1825    #ifdef BFRAMES
1826    static void
1827    FrameCodeB(Encoder * pEnc,
1828                       FRAMEINFO * frame,
1829                       Bitstream * bs,
1830                       uint32_t * pBits)
1831    {
1832            int16_t dct_codes[6 * 64];
1833            int16_t qcoeff[6 * 64];
1834            uint32_t x, y;
1835            VECTOR forward;
1836            VECTOR backward;
1837    
1838            IMAGE *f_ref = &pEnc->reference->image;
1839            IMAGE *b_ref = &pEnc->current->image;
1840    
1841    #ifdef BFRAMES_DEC_DEBUG
1842            FILE *fp;
1843            static char first=0;
1844    #define BFRAME_DEBUG    if (!first && fp){ \
1845                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
1846            }
1847    
1848            if (!first){
1849                    fp=fopen("C:\\XVIDDBGE.TXT","w");
1850            }
1851    #endif
1852    
1853            // forward
1854            image_setedges(f_ref, pEnc->mbParam.edged_width,
1855                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1856                                       pEnc->mbParam.height,
1857                                       frame->global_flags & XVID_INTERLACING);
1858            start_timer();
1859            image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1860                                              pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1861                                              0);
1862            stop_inter_timer();
1863    
1864            // backward
1865            image_setedges(b_ref, pEnc->mbParam.edged_width,
1866                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1867                                       pEnc->mbParam.height,
1868                                       frame->global_flags & XVID_INTERLACING);
1869            start_timer();
1870            image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1871                                              pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1872                                              0);
1873            stop_inter_timer();
1874    
1875            start_timer();
1876            MotionEstimationBVOP(&pEnc->mbParam, frame,
1877                    ((int32_t)pEnc->mbParam.fbase + pEnc->last_pframe - frame->ticks) % pEnc->mbParam.fbase,
1878                                                    pEnc->time_pp,
1879                                                    pEnc->reference->mbs, f_ref,
1880                                                     &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1881                                                     pEnc->current->mbs, b_ref, &pEnc->vInterH,
1882                                                     &pEnc->vInterV, &pEnc->vInterHV);
1883    
1884    
1885            stop_motion_timer();
1886    
1887            /*if (test_quant_type(&pEnc->mbParam, pEnc->current))
1888               {
1889               BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);
1890               } */
1891    
1892            frame->coding_type = B_VOP;
1893            BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1894    
1895            *pBits = BitstreamPos(bs);
1896    
1897            pEnc->sStat.iTextBits = 0;
1898            pEnc->sStat.iMvSum = 0;
1899            pEnc->sStat.iMvCount = 0;
1900            pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1901    
1902    
1903            for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1904                    // reset prediction
1905    
1906                    forward.x = 0;
1907                    forward.y = 0;
1908                    backward.x = 0;
1909                    backward.y = 0;
1910    
1911                    for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1912                            MACROBLOCK *f_mb =
1913                                    &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];
1914                            MACROBLOCK *b_mb =
1915                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1916                            MACROBLOCK *mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1917    
1918                            // decoder ignores mb when refence block is INTER(0,0), CBP=0
1919                            if (mb->mode == MODE_NOT_CODED) {
1920                                    mb->mvs[0].x = 0;
1921                                    mb->mvs[0].y = 0;
1922    
1923                                    mb->cbp = 0;
1924    #ifdef BFRAMES_DEC_DEBUG
1925            BFRAME_DEBUG
1926    #endif
1927                                    continue;
1928                            }
1929    
1930                            MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1931                                                                             f_ref, &pEnc->f_refh, &pEnc->f_refv,
1932                                                                             &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1933                                                                             &pEnc->vInterV, &pEnc->vInterHV,
1934                                                                             dct_codes);
1935    
1936                            mb->quant = frame->quant;
1937                            mb->cbp =
1938                                    MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes,
1939                                                                      qcoeff);
1940                            //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant);
1941    
1942                            if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1943                                    && (mb->deltamv.x == 0) && (mb->deltamv.y == 0) ) {
1944                                    mb->mode = MODE_DIRECT_NONE_MV; // skipped
1945                            }
1946    
1947    /* update predictors for forward and backward vectors */
1948                            if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {
1949                                    mb->pmvs[0].x = mb->mvs[0].x - forward.x;
1950                                    mb->pmvs[0].y = mb->mvs[0].y - forward.y;
1951                                    forward.x = mb->mvs[0].x;
1952                                    forward.y = mb->mvs[0].y;
1953                            }
1954    
1955                            if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) {
1956                                    mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x;
1957                                    mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y;
1958                                    backward.x = mb->b_mvs[0].x;
1959                                    backward.y = mb->b_mvs[0].y;
1960                            }
1961    
1962    //                      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);
1963    
1964    #ifdef BFRAMES_DEC_DEBUG
1965            BFRAME_DEBUG
1966    #endif
1967                            start_timer();
1968                            MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1969                                                     &pEnc->sStat);
1970                            stop_coding_timer();
1971                    }
1972            }
1973    
1974            emms();
1975    
1976            // TODO: dynamic fcode/bcode ???
1977    
1978            *pBits = BitstreamPos(bs) - *pBits;
1979    
1980    #ifdef BFRAMES_DEC_DEBUG
1981            if (!first){
1982                    first=1;
1983                    if (fp)
1984                            fclose(fp);
1985            }
1986    #endif
1987    }
1988    #endif
1989    
1990    
1991    /*      in case internal output is needed somewhere... */
1992    /*      {
1993            FILE *filehandle;
1994            filehandle=fopen("last-b.pgm","wb");
1995            if (filehandle)
1996            {
1997                    fprintf(filehandle,"P5\n\n");           //
1998                    fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height);
1999                    fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle);
2000                    fclose(filehandle);
2001                    }
2002            }
2003    */

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.71

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