[svn] / tags / branch-release-1-0 / xvidcore / src / encoder.c Repository:
ViewVC logotype

Diff of /tags/branch-release-1-0/xvidcore/src/encoder.c

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

trunk/xvidcore/src/encoder.c revision 41, Wed Mar 20 00:28:02 2002 UTC branches/dev-api-3/xvidcore/src/encoder.c revision 572, Mon Sep 30 09:19:26 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.7 2002-09-30 09:19:26 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 18  Line 70 
70  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
71  #include "utils/mem_align.h"  #include "utils/mem_align.h"
72    
73  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT  /*****************************************************************************
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 49  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 71  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 90  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 *) xvid_malloc(sizeof(Encoder), 16)) == 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 110  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            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    
415            pEnc->m_framenum = 0;
416            pEnc->current->stamp = 0;
417            pEnc->reference->stamp = 0;
418    
419            pParam->handle = (void *) pEnc;
420    
421            if (pParam->rc_bitrate) {
422                    RateControlInit(&pEnc->rate_control, pParam->rc_bitrate,
423                                                    pParam->rc_reaction_delay_factor,
424                                                    pParam->rc_averaging_period, pParam->rc_buffer,
425                                                    pParam->fbase * 1000 / pParam->fincr,
426                                                    pParam->max_quantizer, pParam->min_quantizer);
427            }
428    
429            init_timer();
430    
431            return XVID_ERR_OK;
432    
433            /*
434             * We handle all XVID_ERR_MEMORY here, this makes the code lighter
435             */
436    
437      xvid_err_memory5:
438    
439    
440            if (pEnc->mbParam.max_bframes > 0) {
441    
442                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
443                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
444                                                      pEnc->mbParam.edged_height);
445                    }
446                    xvid_free(pEnc->queue);
447            }
448    
449      xvid_err_memory4:
450    
451            if (pEnc->mbParam.max_bframes > 0) {
452    
453                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
454    
455                            if (pEnc->bframes[i] == NULL)
456                                    continue;
457    
458                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
459                                                      pEnc->mbParam.edged_height);
460    
461                            xvid_free(pEnc->bframes[i]->mbs);
462    
463                            xvid_free(pEnc->bframes[i]);
464    
465                    }
466    
467                    xvid_free(pEnc->bframes);
468            }
469    
470      xvid_err_memory3:
471    #ifdef _DEBUG_PSNR
472            image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
473                                      pEnc->mbParam.edged_height);
474    #endif
475    
476            image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
477                                      pEnc->mbParam.edged_height);
478            image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
479                                      pEnc->mbParam.edged_height);
480            image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
481                                      pEnc->mbParam.edged_height);
482    
483            image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
484                                      pEnc->mbParam.edged_height);
485            image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
486                                      pEnc->mbParam.edged_height);
487            image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
488                                      pEnc->mbParam.edged_height);
489            image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
490                                      pEnc->mbParam.edged_height);
491            image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
492                                      pEnc->mbParam.edged_height);
493            image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
494                                      pEnc->mbParam.edged_height);
495            image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
496                                      pEnc->mbParam.edged_height);
497    
498      xvid_err_memory2:
499            xvid_free(pEnc->current->mbs);
500            xvid_free(pEnc->reference->mbs);
501    
502      xvid_err_memory1:
503            xvid_free(pEnc->current);
504            xvid_free(pEnc->reference);
505                  xvid_free(pEnc);                  xvid_free(pEnc);
506    
507            pParam->handle = NULL;
508    
509                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
510          }          }
511    
512          if (image_create(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)  /*****************************************************************************
513     * Encoder destruction
514     *
515     * This function destroy the entire encoder structure created by a previous
516     * successful encoder_create call.
517     *
518     * Returned values (for now only one returned value) :
519     *    - XVID_ERR_OK     - no errors
520     *
521     ****************************************************************************/
522    
523    int
524    encoder_destroy(Encoder * pEnc)
525          {          {
526                  image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          int i;
527    
528            ENC_CHECK(pEnc);
529    
530            /* B Frames specific */
531            if (pEnc->mbParam.max_bframes > 0) {
532    
533                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
534    
535                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
536                                              pEnc->mbParam.edged_height);
537                    }
538                    xvid_free(pEnc->queue);
539            }
540    
541    
542            if (pEnc->mbParam.max_bframes > 0) {
543    
544                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
545    
546                            if (pEnc->bframes[i] == NULL)
547                                    continue;
548    
549                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
550                                              pEnc->mbParam.edged_height);
551    
552                            xvid_free(pEnc->bframes[i]->mbs);
553    
554                            xvid_free(pEnc->bframes[i]);
555                    }
556    
557                    xvid_free(pEnc->bframes);
558    
559            }
560    
561            /* All images, reference, current etc ... */
562    
563            image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
564                                      pEnc->mbParam.edged_height);
565            image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
566                                      pEnc->mbParam.edged_height);
567            image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
568                                      pEnc->mbParam.edged_height);
569            image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
570                                      pEnc->mbParam.edged_height);
571            image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
572                                      pEnc->mbParam.edged_height);
573            image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
574                                      pEnc->mbParam.edged_height);
575            image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
576                                      pEnc->mbParam.edged_height);
577    
578            image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
579                                      pEnc->mbParam.edged_height);
580            image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
581                                      pEnc->mbParam.edged_height);
582            image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
583                                      pEnc->mbParam.edged_height);
584    
585    #ifdef _DEBUG_PSNR
586            image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
587                                      pEnc->mbParam.edged_height);
588    #endif
589    
590            /* Encoder structure */
591    
592            xvid_free(pEnc->current->mbs);
593            xvid_free(pEnc->current);
594    
595            xvid_free(pEnc->reference->mbs);
596            xvid_free(pEnc->reference);
597    
598                  xvid_free(pEnc);                  xvid_free(pEnc);
599                  return XVID_ERR_MEMORY;  
600            return XVID_ERR_OK;
601          }          }
602    
603          if (image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)  
604    static __inline void inc_frame_num(Encoder * pEnc)
605          {          {
606                  image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          pEnc->current->stamp = pEnc->mbParam.m_stamp;   // first frame is zero
607                  image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;
608                  xvid_free(pEnc);  }
609                  return XVID_ERR_MEMORY;  
610    
611    static __inline void
612    queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
613    {
614            if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
615            {
616                    DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL");
617                    return;
618            }
619    
620            DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
621                                    pEnc->bframenum_head, pEnc->bframenum_tail,
622                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
623    
624    
625            start_timer();
626            if (image_input
627                    (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
628                     pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
629                    return;
630            stop_conv_timer();
631    
632            pEnc->queue_size++;
633            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
634    }
635    
636    static __inline void
637    set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)
638    {
639    
640                    pCur->ticks = (int32_t)pCur->stamp % time_base;
641                    pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;
642    
643                    //HEAVY DEBUG OUTPUT    remove when timecodes prove to be stable
644    
645    /*              fprintf(stderr,"WriteVop:   %d - %d \n",
646                            ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
647                    fprintf(stderr,"set_timecodes: VOP %1d   stamp=%lld ref_stamp=%lld  base=%d\n",
648                            pCur->coding_type, pCur->stamp, pRef->stamp, time_base);
649                    fprintf(stderr,"set_timecodes: VOP %1d   seconds=%d   ticks=%d   (ref-sec=%d  ref-tick=%d)\n",
650                            pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);
651    
652    */
653    }
654    
655    
656    
657    
658    
659    /*****************************************************************************
660     * IPB frame encoder entry point
661     *
662     * Returned values :
663     *    - XVID_ERR_OK     - no errors
664     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
665     *                        format
666     ****************************************************************************/
667    
668    int
669    encoder_encode_bframes(Encoder * pEnc,
670                               XVID_ENC_FRAME * pFrame,
671                               XVID_ENC_STATS * pResult)
672    {
673            uint16_t x, y;
674            Bitstream bs;
675            uint32_t bits, mode;
676    
677            int input_valid = 1;
678    
679    #ifdef _DEBUG_PSNR
680            float psnr;
681            char temp[128];
682    #endif
683    
684            ENC_CHECK(pEnc);
685            ENC_CHECK(pFrame);
686            ENC_CHECK(pFrame->image);
687    
688            start_global_timer();
689    
690            BitstreamInit(&bs, pFrame->bitstream, 0);
691    
692    ipvop_loop:
693    
694            /*
695             * bframe "flush" code
696             */
697    
698            if ((pFrame->image == NULL || pEnc->flush_bframes)
699                    && (pEnc->bframenum_head < pEnc->bframenum_tail)) {
700    
701                    if (pEnc->flush_bframes == 0) {
702                            /*
703                             * we have reached the end of stream without getting
704                             * a future reference frame... so encode last final
705                             * frame as a pframe
706                             */
707    
708                            DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
709                                    pEnc->bframenum_head, pEnc->bframenum_tail,
710                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
711    
712                            pEnc->bframenum_tail--;
713                            SWAP(pEnc->current, pEnc->reference);
714    
715                            SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
716    
717                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
718    
719                            BitstreamPad(&bs);
720                            pFrame->length = BitstreamLength(&bs);
721                            pFrame->intra = 0;
722    
723                            return XVID_ERR_OK;
724                    }
725    
726    
727                    DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
728                                    pEnc->bframenum_head, pEnc->bframenum_tail,
729                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
730    
731                    FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
732                    pEnc->bframenum_head++;
733    
734                    BitstreamPad(&bs);
735                    pFrame->length = BitstreamLength(&bs);
736                    pFrame->intra = 0;
737    
738                    if (input_valid)
739                            queue_image(pEnc, pFrame);
740    
741                    return XVID_ERR_OK;
742            }
743    
744            if (pEnc->bframenum_head > 0) {
745                    pEnc->bframenum_head = pEnc->bframenum_tail = 0;
746    
747                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
748    
749                            DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
750                                    pEnc->bframenum_head, pEnc->bframenum_tail,
751                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
752    
753                            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
754                            BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
755                            BitstreamPad(&bs);
756                            BitstreamPutBits(&bs, 0x7f, 8);
757    
758                            pFrame->length = BitstreamLength(&bs);
759                            pFrame->intra = 0;
760    
761                            if (input_valid)
762                                    queue_image(pEnc, pFrame);
763    
764                            return XVID_ERR_OK;
765                    }
766            }
767    
768    
769    bvop_loop:
770    
771            if (pEnc->bframenum_dx50bvop != -1)
772            {
773    
774                    SWAP(pEnc->current, pEnc->reference);
775                    SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
776    
777                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
778                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
779                    }
780    
781                    if (input_valid)
782                    {
783                            queue_image(pEnc, pFrame);
784                            input_valid = 0;
785                    }
786    
787            } else if (input_valid) {
788    
789                    SWAP(pEnc->current, pEnc->reference);
790    
791                    start_timer();
792                    if (image_input
793                            (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
794                            pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
795                            return XVID_ERR_FORMAT;
796                    stop_conv_timer();
797    
798                    // queue input frame, and dequue next image
799                    if (pEnc->queue_size > 0)
800                    {
801                            image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
802                            if (pEnc->queue_head != pEnc->queue_tail)
803                            {
804                                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
805                            }
806                            pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
807                            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
808                    }
809    
810            } else if (pEnc->queue_size > 0) {
811    
812                    SWAP(pEnc->current, pEnc->reference);
813    
814                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
815                    pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
816                    pEnc->queue_size--;
817    
818            } else if (BitstreamPos(&bs) == 0) {
819    
820                    DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
821                                    pEnc->bframenum_head, pEnc->bframenum_tail,
822                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
823    
824                    pFrame->intra = 0;
825    
826                    set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
827                    BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); // write N_VOP
828                    BitstreamPad(&bs);
829                    pFrame->length = BitstreamLength(&bs);
830    
831                    return XVID_ERR_OK;
832    
833            } else {
834    
835                    pFrame->length = BitstreamLength(&bs);
836                    return XVID_ERR_OK;
837            }
838    
839            pEnc->flush_bframes = 0;
840    
841            emms();
842    
843            // only inc frame num, adapt quant, etc. if we havent seen it before
844            if (pEnc->bframenum_dx50bvop < 0 )
845            {
846                    if (pFrame->quant == 0)
847                            pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
848                    else
849                            pEnc->current->quant = pFrame->quant;
850    
851    /*              if (pEnc->current->quant < 1)
852                            pEnc->current->quant = 1;
853    
854                    if (pEnc->current->quant > 31)
855                            pEnc->current->quant = 31;
856    */
857                    pEnc->current->global_flags = pFrame->general;
858                    pEnc->current->motion_flags = pFrame->motion;
859    
860                    /* ToDo : dynamic fcode (in both directions) */
861                    pEnc->current->fcode = pEnc->mbParam.m_fcode;
862                    pEnc->current->bcode = pEnc->mbParam.m_fcode;
863    
864                    inc_frame_num(pEnc);
865    
866    #ifdef _DEBUG_PSNR
867                    image_copy(&pEnc->sOriginal, &pEnc->current->image,
868                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
869    #endif
870    
871                    emms();
872    
873                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
874                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
875                                    "%i  if:%i  st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks);
876          }          }
877    
878          if (image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
879          {           * Luminance masking
880                  image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
881                  image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
882                  image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
883                  xvid_free(pEnc);                          int *temp_dquants =
884                  return XVID_ERR_MEMORY;                                  (int *) xvid_malloc(pEnc->mbParam.mb_width *
885                                                                    pEnc->mbParam.mb_height * sizeof(int),
886                                                                    CACHE_LINE);
887    
888                            pEnc->current->quant =
889                                    adaptive_quantization(pEnc->current->image.y,
890                                                                      pEnc->mbParam.edged_width, temp_dquants,
891                                                                      pEnc->current->quant, pEnc->current->quant,
892                                                                      2 * pEnc->current->quant,
893                                                                      pEnc->mbParam.mb_width,
894                                                                      pEnc->mbParam.mb_height);
895    
896                            for (y = 0; y < pEnc->mbParam.mb_height; y++) {
897    
898    #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
899    
900                                    for (x = 0; x < pEnc->mbParam.mb_width; x++) {
901                                            MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
902    
903                                            pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
904                                    }
905    
906    #undef OFFSET
907                            }
908    
909                            xvid_free(temp_dquants);
910                    }
911    
912            }
913    
914            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
915             * ivop/pvop/bvop selection
916             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
917    
918    
919            if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||
920                    (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&
921                     pEnc->iFrameNum >= pEnc->iMaxKeyInterval)
922                    || /*image_mad(&pEnc->reference->image, &pEnc->current->image,
923                                             pEnc->mbParam.edged_width, pEnc->mbParam.width,
924                                             pEnc->mbParam.height) > 30) {*/
925                            2 == (mode = MEanalysis(&pEnc->reference->image, &pEnc->current->image,
926                                             &pEnc->mbParam, pEnc->current->mbs, pEnc->current->fcode))) {
927    
928                    /*
929                     * This will be coded as an Intra Frame
930                     */
931    
932                    DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
933                                    pEnc->bframenum_head, pEnc->bframenum_tail,
934                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
935    
936                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
937                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
938                    }
939    
940                    // when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe
941    
942                    if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
943    
944                            pEnc->bframenum_tail--;
945                            pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
946    
947                            SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
948                            if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
949                                    image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
950                            }
951                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
952    
953                            pFrame->intra = 0;
954    
955                    } else {
956    
957                            FrameCodeI(pEnc, &bs, &bits);
958                            pFrame->intra = 1;
959    
960                            pEnc->bframenum_dx50bvop = -1;
961                    }
962    
963                    pEnc->flush_bframes = 1;
964    
965                    if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
966                            BitstreamPad(&bs);
967                            input_valid = 0;
968                            goto ipvop_loop;
969                    }
970    
971                    /*
972                     * NB : sequences like "IIBB" decode fine with msfdam but,
973                     *      go screwy with divx 5.00
974                     */
975            } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) {
976                    /*
977                     * This will be coded as a Predicted Frame
978                     */
979    
980                    DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
981                                    pEnc->bframenum_head, pEnc->bframenum_tail,
982                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
983    
984                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
985                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
986          }          }
987    
988          if (image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)                  FrameCodeP(pEnc, &bs, &bits, 1, 0);
989          {                  pFrame->intra = 0;
990                  image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  pEnc->flush_bframes = 1;
991                  image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
992                  image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {
993                  image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                          BitstreamPad(&bs);
994                  xvid_free(pEnc);                          input_valid = 0;
995                  return XVID_ERR_MEMORY;                          goto ipvop_loop;
996          }          }
997    
998          pEnc->pMBs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, 16);          } else {
999          if (pEnc->pMBs == NULL)                  /*
1000          {                   * This will be coded as a Bidirectional Frame
1001                  image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                   */
1002                  image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
1003                  image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
1004                  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);  
                 xvid_free(pEnc);  
                 return XVID_ERR_MEMORY;  
1005          }          }
1006    
1007          // init macroblock array                  if (pFrame->bquant < 1) {
1008          for (i = 0; i < pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; i++)                          pEnc->current->quant =
1009          {                                  ((pEnc->reference->quant +
1010                  pEnc->pMBs[i].dquant = NO_CHANGE;                                    pEnc->current->quant) * pEnc->bquant_ratio) / 200;
1011                    } else {
1012                            pEnc->current->quant = pFrame->bquant;
1013          }          }
1014                    if (pEnc->current->quant < 1)
1015                            pEnc->current->quant = 1;
1016    
1017          pParam->handle = (void *)pEnc;                  if (pEnc->current->quant > 31)
1018                            pEnc->current->quant = 31;
1019    
         if (pParam->bitrate)  
         {  
                 RateControlInit(pParam->bitrate, pParam->rc_buffersize, pParam->fbase, pParam->width,  
                                 pParam->height, pParam->max_quantizer, pParam->min_quantizer);  
         }  
1020    
1021          create_vlc_tables();                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1022          init_timer();                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1023                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1024    
1025          return XVID_ERR_OK;  
1026    
1027                    /* store frame into bframe buffer & swap ref back to current */
1028                    SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1029                    SWAP(pEnc->current, pEnc->reference);
1030    
1031                    pEnc->bframenum_tail++;
1032    
1033                    pFrame->intra = 0;
1034                    pFrame->length = 0;
1035    
1036                    input_valid = 0;
1037                    goto bvop_loop;
1038  }  }
1039    
1040            pEnc->iFrameNum++;
1041    
1042  int encoder_destroy(Encoder * pEnc)          BitstreamPad(&bs);
1043  {          pFrame->length = BitstreamLength(&bs);
         ENC_CHECK(pEnc);  
         ENC_CHECK(pEnc->sCurrent.y);  
         ENC_CHECK(pEnc->sReference.y);  
1044    
1045          xvid_free(pEnc->pMBs);          if (pResult) {
1046          image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  pResult->quant = pEnc->current->quant;
1047          image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
1048          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  pResult->kblks = pEnc->sStat.kblks;
1049          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  pResult->mblks = pEnc->sStat.mblks;
1050          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  pResult->ublks = pEnc->sStat.ublks;
1051          xvid_free(pEnc);          }
1052    
1053            emms();
1054    
1055    #ifdef _DEBUG_PSNR
1056            psnr =
1057                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1058                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
1059                                       pEnc->mbParam.height);
1060    
1061            snprintf(temp, 127, "PSNR: %f\n", psnr);
1062            DEBUG(temp);
1063    #endif
1064    
1065            if (pFrame->quant == 0) {
1066                    RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1067                                                      pFrame->length, pFrame->intra);
1068            }
1069    
1070          destroy_vlc_tables();          stop_global_timer();
1071            write_timer();
1072    
1073          return XVID_ERR_OK;          return XVID_ERR_OK;
1074  }  }
1075    
1076  int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult)  
1077    
1078    /*****************************************************************************
1079     * "original" IP frame encoder entry point
1080     *
1081     * Returned values :
1082     *    - XVID_ERR_OK     - no errors
1083     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
1084     *                        format
1085     ****************************************************************************/
1086    
1087    int
1088    encoder_encode(Encoder * pEnc,
1089                               XVID_ENC_FRAME * pFrame,
1090                               XVID_ENC_STATS * pResult)
1091  {  {
1092          uint16_t x, y;          uint16_t x, y;
1093          Bitstream bs;          Bitstream bs;
1094          uint32_t bits;          uint32_t bits;
1095          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
1096    
1097    #ifdef _DEBUG_PSNR
1098            float psnr;
1099            uint8_t temp[128];
1100    #endif
1101    
1102          start_global_timer();          start_global_timer();
1103    
1104          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
# Line 227  Line 1106 
1106          ENC_CHECK(pFrame->bitstream);          ENC_CHECK(pFrame->bitstream);
1107          ENC_CHECK(pFrame->image);          ENC_CHECK(pFrame->image);
1108    
1109          pEnc->mbParam.global_flags = pFrame->general;          SWAP(pEnc->current, pEnc->reference);
         pEnc->mbParam.motion_flags = pFrame->motion;  
1110    
1111          start_timer();          pEnc->current->global_flags = pFrame->general;
1112          if (image_input(&pEnc->sCurrent, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width,          pEnc->current->motion_flags = pFrame->motion;
1113                          pFrame->image, pFrame->colorspace))          pEnc->mbParam.hint = &pFrame->hint;
1114    
1115            /* disable alternate scan flag if interlacing is not enabled */
1116            if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) &&
1117                    !(pEnc->current->global_flags & XVID_INTERLACING))
1118          {          {
1119                  return XVID_ERR_FORMAT;                  pEnc->current->global_flags -= XVID_ALTERNATESCAN;
1120          }          }
1121    
1122            start_timer();
1123            if (image_input
1124                    (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
1125                     pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0)
1126                    return XVID_ERR_FORMAT;
1127          stop_conv_timer();          stop_conv_timer();
1128    
1129          EMMS();  #ifdef _DEBUG_PSNR
1130            image_copy(&pEnc->sOriginal, &pEnc->current->image,
1131                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
1132    #endif
1133    
1134            emms();
1135    
1136          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
1137    
1138          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
1139          {                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
1140                  pEnc->mbParam.quant = RateControlGetQ(0);          } else {
1141          }                  pEnc->current->quant = pFrame->quant;
         else  
         {  
                 pEnc->mbParam.quant = pFrame->quant;  
1142          }          }
1143    
1144          if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0)          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1145          {                  int *temp_dquants =
1146                  int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), 16);                          (int *) xvid_malloc(pEnc->mbParam.mb_width *
1147                                                                    pEnc->mbParam.mb_height * sizeof(int),
1148                                                                    CACHE_LINE);
1149    
1150                  pEnc->mbParam.quant = adaptive_quantization(pEnc->sCurrent.y, pEnc->mbParam.width,                  pEnc->current->quant =
1151                                                              temp_dquants, pFrame->quant, pFrame->quant,                          adaptive_quantization(pEnc->current->image.y,
1152                                                              2*pFrame->quant, pEnc->mbParam.mb_width, pEnc->mbParam.mb_height);                                                                    pEnc->mbParam.edged_width, temp_dquants,
1153                                                                      pEnc->current->quant, pEnc->current->quant,
1154                                                                      2 * pEnc->current->quant,
1155                                                                      pEnc->mbParam.mb_width,
1156                                                                      pEnc->mbParam.mb_height);
1157    
1158                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                  for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1159                          for (x = 0; x < pEnc->mbParam.mb_width; x++)  
1160                          {  #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
1161                                  MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];  
1162                                  pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)];                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1163    
1164    
1165                                    MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
1166    
1167                                    pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
1168                            }
1169    
1170    #undef OFFSET
1171                          }                          }
1172    
1173                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
1174          }          }
1175    
1176          if(pEnc->mbParam.global_flags & XVID_H263QUANT) {          if (pEnc->current->global_flags & XVID_H263QUANT) {
1177                  if(pEnc->mbParam.quant_type != H263_QUANT)                  if (pEnc->mbParam.m_quant_type != H263_QUANT)
1178                          write_vol_header = 1;                          write_vol_header = 1;
1179                  pEnc->mbParam.quant_type = H263_QUANT;                  pEnc->mbParam.m_quant_type = H263_QUANT;
1180          }          } else if (pEnc->current->global_flags & XVID_MPEGQUANT) {
1181          else if(pEnc->mbParam.global_flags & XVID_MPEGQUANT) {                  int matrix1_changed, matrix2_changed;
1182                  int ret1, ret2;  
1183                    matrix1_changed = matrix2_changed = 0;
1184    
1185                  if(pEnc->mbParam.quant_type != MPEG4_QUANT)                  if (pEnc->mbParam.m_quant_type != MPEG4_QUANT)
1186                          write_vol_header = 1;                          write_vol_header = 1;
1187    
1188                  pEnc->mbParam.quant_type = MPEG4_QUANT;                  pEnc->mbParam.m_quant_type = MPEG4_QUANT;
1189    
1190                  if ((pEnc->mbParam.global_flags & XVID_CUSTOM_QMATRIX) > 0) {                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1191                          if(pFrame->quant_intra_matrix != NULL)                          if(pFrame->quant_intra_matrix != NULL)
1192                                  ret1 = set_intra_matrix(pFrame->quant_intra_matrix);                                  matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix);
1193                          if(pFrame->quant_inter_matrix != NULL)                          if(pFrame->quant_inter_matrix != NULL)
1194                                  ret2 = set_inter_matrix(pFrame->quant_inter_matrix);                                  matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix);
1195                  }                  } else {
1196                  else {                          matrix1_changed = set_intra_matrix(get_default_intra_matrix());
1197                          ret1 = set_intra_matrix(get_default_intra_matrix());                          matrix2_changed = set_inter_matrix(get_default_inter_matrix());
                         ret2 = set_inter_matrix(get_default_inter_matrix());  
1198                  }                  }
1199                  if(write_vol_header == 0)                  if(write_vol_header == 0)
1200                          write_vol_header = ret1 | ret2;                          write_vol_header = matrix1_changed | matrix2_changed;
1201          }          }
1202    
1203          if (pFrame->intra < 0)          if (pFrame->intra < 0) {
1204          {                  if ((pEnc->iFrameNum == 0)
1205                  if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0)                          || ((pEnc->iMaxKeyInterval > 0)
1206                                                 && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval)))                                  && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {
   
1207                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1208                  else                  } else {
1209                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
1210          }          }
1211          else          } else {
1212          {                  if (pFrame->intra == 1) {
                 if (pFrame->intra == 1)  
1213                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1214                  else                  } else {
1215                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);
1216          }          }
1217    
1218            }
1219    
1220          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1221          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1222          BitstreamPad(&bs);          BitstreamPad(&bs);
1223          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1224    
1225          if (pResult)          if (pResult) {
1226          {                  pResult->quant = pEnc->current->quant;
                 pResult->quant = pEnc->mbParam.quant;  
1227                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
1228                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->sStat.kblks;
1229                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->sStat.mblks;
1230                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
1231          }          }
1232    
1233          EMMS();          emms();
1234    
1235          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
1236          {                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1237                  RateControlUpdate(pEnc->mbParam.quant, pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
1238          }          }
1239    #ifdef _DEBUG_PSNR
1240            psnr =
1241                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1242                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
1243                                       pEnc->mbParam.height);
1244    
1245            snprintf(temp, 127, "PSNR: %f\n", psnr);
1246            DEBUG(temp);
1247    #endif
1248    
1249            inc_frame_num(pEnc);
1250          pEnc->iFrameNum++;          pEnc->iFrameNum++;
         image_swap(&pEnc->sCurrent, &pEnc->sReference);  
1251    
1252          stop_global_timer();          stop_global_timer();
1253          write_timer();          write_timer();
# Line 343  Line 1256 
1256  }  }
1257    
1258    
1259  static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) {  static __inline void
1260    CodeIntraMB(Encoder * pEnc,
1261                            MACROBLOCK * pMB)
1262    {
1263    
1264          pMB->mode = MODE_INTRA;          pMB->mode = MODE_INTRA;
1265    
1266          if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) {          /* zero mv statistics */
1267                  if(pMB->dquant != NO_CHANGE)          pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0;
1268                  {          pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0;
1269            pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;
1270            pMB->sad16 = 0;
1271    
1272            if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1273                    if (pMB->dquant != NO_CHANGE) {
1274                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
1275                          pEnc->mbParam.quant += DQtab[pMB->dquant];                          pEnc->current->quant += DQtab[pMB->dquant];
1276    
1277                            if (pEnc->current->quant > 31)
1278                                    pEnc->current->quant = 31;
1279                            if (pEnc->current->quant < 1)
1280                                    pEnc->current->quant = 1;
1281                    }
1282            }
1283    
1284            pMB->quant = pEnc->current->quant;
1285    }
1286    
1287    
1288    #define FCODEBITS       3
1289    #define MODEBITS        5
1290    
1291    void
1292    HintedMESet(Encoder * pEnc,
1293                            int *intra)
1294    {
1295            HINTINFO *hint;
1296            Bitstream bs;
1297            int length, high;
1298            uint32_t x, y;
1299    
1300            hint = pEnc->mbParam.hint;
1301    
1302            if (hint->rawhints) {
1303                    *intra = hint->mvhint.intra;
1304            } else {
1305                    BitstreamInit(&bs, hint->hintstream, hint->hintlength);
1306                    *intra = BitstreamGetBit(&bs);
1307            }
1308    
1309            if (*intra) {
1310                    return;
1311            }
1312    
1313            pEnc->current->fcode =
1314                    (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs,
1315                                                                                                                                     FCODEBITS);
1316    
1317            length = pEnc->current->fcode + 5;
1318            high = 1 << (length - 1);
1319    
1320            for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1321                    for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1322                            MACROBLOCK *pMB =
1323                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1324                            MVBLOCKHINT *bhint =
1325                                    &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1326                            VECTOR pred;
1327                            VECTOR tmp;
1328                            int vec;
1329    
1330                            pMB->mode =
1331                                    (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs,
1332                                                                                                                                      MODEBITS);
1333    
1334                            pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
1335                            pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
1336    
1337                            if (pMB->mode == MODE_INTER) {
1338                                    tmp.x =
1339                                            (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs,
1340                                                                                                                                                      length);
1341                                    tmp.y =
1342                                            (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs,
1343                                                                                                                                                      length);
1344                                    tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1345                                    tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1346    
1347                                    pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,0);
1348    
1349                                    for (vec = 0; vec < 4; ++vec) {
1350                                            pMB->mvs[vec].x = tmp.x;
1351                                            pMB->mvs[vec].y = tmp.y;
1352                                            pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
1353                                            pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
1354                                    }
1355                            } else if (pMB->mode == MODE_INTER4V) {
1356                                    for (vec = 0; vec < 4; ++vec) {
1357                                            tmp.x =
1358                                                    (hint->rawhints) ? bhint->mvs[vec].
1359                                                    x : BitstreamGetBits(&bs, length);
1360                                            tmp.y =
1361                                                    (hint->rawhints) ? bhint->mvs[vec].
1362                                                    y : BitstreamGetBits(&bs, length);
1363                                            tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1364                                            tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1365    
1366                                            pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,vec);
1367    
1368                                            pMB->mvs[vec].x = tmp.x;
1369                                            pMB->mvs[vec].y = tmp.y;
1370                                            pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
1371                                            pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
1372                                    }
1373                            } else                          // intra / stuffing / not_coded
1374                            {
1375                                    for (vec = 0; vec < 4; ++vec) {
1376                                            pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
1377                                    }
1378                            }
1379    
1380                            if (pMB->mode == MODE_INTER4V &&
1381                                    (pEnc->current->global_flags & XVID_LUMIMASKING)
1382                                    && pMB->dquant != NO_CHANGE) {
1383                                    pMB->mode = MODE_INTRA;
1384    
1385                                    for (vec = 0; vec < 4; ++vec) {
1386                                            pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
1387                                    }
1388                            }
1389                    }
1390            }
1391    }
1392    
1393    
1394    void
1395    HintedMEGet(Encoder * pEnc,
1396                            int intra)
1397    {
1398            HINTINFO *hint;
1399            Bitstream bs;
1400            uint32_t x, y;
1401            int length, high;
1402    
1403            hint = pEnc->mbParam.hint;
1404    
1405            if (hint->rawhints) {
1406                    hint->mvhint.intra = intra;
1407            } else {
1408                    BitstreamInit(&bs, hint->hintstream, 0);
1409                    BitstreamPutBit(&bs, intra);
1410            }
1411    
1412            if (intra) {
1413                    if (!hint->rawhints) {
1414                            BitstreamPad(&bs);
1415                            hint->hintlength = BitstreamLength(&bs);
1416                    }
1417                    return;
1418            }
1419    
1420            length = pEnc->current->fcode + 5;
1421            high = 1 << (length - 1);
1422    
1423            if (hint->rawhints) {
1424                    hint->mvhint.fcode = pEnc->current->fcode;
1425            } else {
1426                    BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);
1427            }
1428    
1429            for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1430                    for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1431                            MACROBLOCK *pMB =
1432                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1433                            MVBLOCKHINT *bhint =
1434                                    &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1435                            VECTOR tmp;
1436    
1437                            if (hint->rawhints) {
1438                                    bhint->mode = pMB->mode;
1439                            } else {
1440                                    BitstreamPutBits(&bs, pMB->mode, MODEBITS);
1441                            }
1442    
1443                            if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
1444                                    tmp.x = pMB->mvs[0].x;
1445                                    tmp.y = pMB->mvs[0].y;
1446                                    tmp.x += (tmp.x < 0) ? high * 2 : 0;
1447                                    tmp.y += (tmp.y < 0) ? high * 2 : 0;
1448    
1449                                    if (hint->rawhints) {
1450                                            bhint->mvs[0].x = tmp.x;
1451                                            bhint->mvs[0].y = tmp.y;
1452                                    } else {
1453                                            BitstreamPutBits(&bs, tmp.x, length);
1454                                            BitstreamPutBits(&bs, tmp.y, length);
1455                                    }
1456                            } else if (pMB->mode == MODE_INTER4V) {
1457                                    int vec;
1458    
1459                                    for (vec = 0; vec < 4; ++vec) {
1460                                            tmp.x = pMB->mvs[vec].x;
1461                                            tmp.y = pMB->mvs[vec].y;
1462                                            tmp.x += (tmp.x < 0) ? high * 2 : 0;
1463                                            tmp.y += (tmp.y < 0) ? high * 2 : 0;
1464    
1465                          if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31;                                          if (hint->rawhints) {
1466                          if (pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1;                                                  bhint->mvs[vec].x = tmp.x;
1467                                                    bhint->mvs[vec].y = tmp.y;
1468                                            } else {
1469                                                    BitstreamPutBits(&bs, tmp.x, length);
1470                                                    BitstreamPutBits(&bs, tmp.y, length);
1471                                            }
1472                                    }
1473                            }
1474                  }                  }
1475          }          }
1476    
1477          pMB->quant = pEnc->mbParam.quant;          if (!hint->rawhints) {
1478                    BitstreamPad(&bs);
1479                    hint->hintlength = BitstreamLength(&bs);
1480            }
1481  }  }
1482    
1483    
1484  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits)  static int
1485    FrameCodeI(Encoder * pEnc,
1486                       Bitstream * bs,
1487                       uint32_t * pBits)
1488  {  {
1489          int16_t dct_codes[6][64];  
1490          int16_t qcoeff[6][64];          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1491            DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1492    
1493          uint16_t x, y;          uint16_t x, y;
1494    
1495          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
1496          pEnc->mbParam.rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1497          pEnc->mbParam.coding_type = I_VOP;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1498            pEnc->current->coding_type = I_VOP;
1499    
1500          BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1501          BitstreamWriteVopHeader(bs, I_VOP, pEnc->mbParam.rounding_type,  
1502                                  pEnc->mbParam.quant,  #define DIVX501B481P "DivX501b481p"
1503                                  pEnc->mbParam.fixed_code);          if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1504                    BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));
1505            }
1506    
1507            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1508            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1509    
1510          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1511    
# Line 384  Line 1514 
1514          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1515    
1516          for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++)
1517                  for (x = 0; x < pEnc->mbParam.mb_width; x++)                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1518                  {                          MACROBLOCK *pMB =
1519                          MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1520    
1521                          CodeIntraMB(pEnc, pMB);                          CodeIntraMB(pEnc, pMB);
1522    
1523                          MBTransQuantIntra(&pEnc->mbParam, x, y, dct_codes, qcoeff, &pEnc->sCurrent);                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1524                                                              dct_codes, qcoeff);
1525    
1526                          start_timer();                          start_timer();
1527                          MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1528                          stop_prediction_timer();                          stop_prediction_timer();
1529    
1530                          start_timer();                          start_timer();
1531                          MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat);                          if (pEnc->current->global_flags & XVID_GREYSCALE)
1532                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1533                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1534                                    qcoeff[5*64+0]=0;
1535                            }
1536                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1537                          stop_coding_timer();                          stop_coding_timer();
1538                  }                  }
1539    
# Line 407  Line 1543 
1543          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
1544          pEnc->sStat.iMvSum = 0;          pEnc->sStat.iMvSum = 0;
1545          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
1546          pEnc->mbParam.fixed_code = 2;          pEnc->mbParam.m_fcode = 2;
1547    
1548            if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1549                    HintedMEGet(pEnc, 1);
1550            }
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;
1575    
1576          IMAGE *pCurrent = &pEnc->sCurrent;          /* IMAGE *pCurrent = &pEnc->current->image; */
1577          IMAGE *pRef = &pEnc->sReference;          IMAGE *pRef = &pEnc->reference->image;
1578    
1579          image_setedges(pRef,pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height);          start_timer();
1580            image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1581          pEnc->mbParam.rounding_type = 1 - pEnc->mbParam.rounding_type;                                     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    
1628          BitstreamWriteVopHeader(bs, P_VOP, pEnc->mbParam.rounding_type,          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1629                                  pEnc->mbParam.quant,          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
                                 pEnc->mbParam.fixed_code);  
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          return 0;                                        // inter          return 0;                                        // inter
1798  }  }
1799    
1800    
1801    static __inline void
1802    FrameCodeB(Encoder * pEnc,
1803                       FRAMEINFO * frame,
1804                       Bitstream * bs,
1805                       uint32_t * pBits)
1806    {
1807            int16_t dct_codes[6 * 64];
1808            int16_t qcoeff[6 * 64];
1809            uint32_t x, y;
1810    
1811            IMAGE *f_ref = &pEnc->reference->image;
1812            IMAGE *b_ref = &pEnc->current->image;
1813    
1814    #ifdef BFRAMES_DEC_DEBUG
1815            FILE *fp;
1816            static char first=0;
1817    #define BFRAME_DEBUG    if (!first && fp){ \
1818                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
1819            }
1820    
1821            if (!first){
1822                    fp=fopen("C:\\XVIDDBGE.TXT","w");
1823            }
1824    #endif
1825    
1826            // forward
1827            image_setedges(f_ref, pEnc->mbParam.edged_width,
1828                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1829                                       pEnc->mbParam.height);
1830            start_timer();
1831            image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1832                                              pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1833                                              0);
1834            stop_inter_timer();
1835    
1836            // backward
1837            image_setedges(b_ref, pEnc->mbParam.edged_width,
1838                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1839                                       pEnc->mbParam.height);
1840            start_timer();
1841            image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1842                                              pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1843                                              0);
1844            stop_inter_timer();
1845    
1846            start_timer();
1847    
1848            MotionEstimationBVOP(&pEnc->mbParam, frame,
1849                    ((int32_t)(pEnc->current->stamp - frame->stamp)),                               // time_bp
1850                    ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),     // time_pp
1851                            pEnc->reference->mbs, f_ref,
1852                                                     &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1853                                                     pEnc->current->mbs, b_ref, &pEnc->vInterH,
1854                                                     &pEnc->vInterV, &pEnc->vInterHV);
1855    
1856    
1857            stop_motion_timer();
1858    
1859            /*if (test_quant_type(&pEnc->mbParam, pEnc->current))
1860               {
1861               BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);
1862               } */
1863    
1864            frame->coding_type = B_VOP;
1865    
1866            set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
1867            BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1868    
1869            *pBits = BitstreamPos(bs);
1870    
1871            pEnc->sStat.iTextBits = 0;
1872            pEnc->sStat.iMvSum = 0;
1873            pEnc->sStat.iMvCount = 0;
1874            pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1875    
1876    
1877            for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1878                    for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1879                            MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1880                            int direction = pEnc->global & XVID_ALTERNATESCAN ? 2 : 0;
1881    
1882                            // decoder ignores mb when refence block is INTER(0,0), CBP=0
1883                            if (mb->mode == MODE_NOT_CODED) {
1884                                    //mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0;
1885                                    continue;
1886                            }
1887    
1888                            if (mb->mode != MODE_DIRECT_NONE_MV) {
1889                                    MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1890                                                                             f_ref, &pEnc->f_refh, &pEnc->f_refv,
1891                                                                             &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1892                                                                             &pEnc->vInterV, &pEnc->vInterHV,
1893                                                                             dct_codes);
1894    
1895                                    if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
1896                                    mb->quant = frame->quant;
1897    
1898                                    mb->cbp =
1899                                            MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);
1900    
1901                                    if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1902                                            && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
1903                                            mb->mode = MODE_DIRECT_NONE_MV; // skipped
1904                                    }
1905                            }
1906    
1907    #ifdef BFRAMES_DEC_DEBUG
1908            BFRAME_DEBUG
1909    #endif
1910                            start_timer();
1911                            MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1912                                                     &pEnc->sStat, direction);
1913                            stop_coding_timer();
1914                    }
1915            }
1916    
1917            emms();
1918    
1919            // TODO: dynamic fcode/bcode ???
1920    
1921            *pBits = BitstreamPos(bs) - *pBits;
1922    
1923    #ifdef BFRAMES_DEC_DEBUG
1924            if (!first){
1925                    first=1;
1926                    if (fp)
1927                            fclose(fp);
1928            }
1929    #endif
1930    }
1931    
1932    
1933    /*      in case internal output is needed somewhere... */
1934    /*      {
1935            FILE *filehandle;
1936            filehandle=fopen("last-b.pgm","wb");
1937            if (filehandle)
1938            {
1939                    fprintf(filehandle,"P5\n\n");           //
1940                    fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height);
1941                    fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle);
1942                    fclose(filehandle);
1943                    }
1944            }
1945    */

Legend:
Removed from v.41  
changed lines
  Added in v.572

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