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

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

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