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

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

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

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

Legend:
Removed from v.13  
changed lines
  Added in v.557

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