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

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.73

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