[cvs] / xvidcore / src / encoder.c Repository:
ViewVC logotype

Diff of /xvidcore/src/encoder.c

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

revision 1.5, Sat Mar 16 09:55:19 2002 UTC revision 1.76.2.11, Thu Oct 3 12:06:42 2002 UTC
# Line 1  Line 1 
1    /*****************************************************************************
2     *
3     *  XVID MPEG-4 VIDEO CODEC
4     *  -  Encoder main module  -
5     *
6     *  This program is an implementation of a part of one or more MPEG-4
7     *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending
8     *  to use this software module in hardware or software products are
9     *  advised that its use may infringe existing patents or copyrights, and
10     *  any such use would be at such party's own risk.  The original
11     *  developer of this software module and his/her company, and subsequent
12     *  editors and their companies, will have no liability for use of this
13     *  software or modifications or derivatives thereof.
14     *
15     *  This program is free software; you can redistribute it and/or modify
16     *  it under the terms of the GNU General Public License as published by
17     *  the Free Software Foundation; either version 2 of the License, or
18     *  (at your option) any later version.
19     *
20     *  This program is distributed in the hope that it will be useful,
21     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     *  GNU General Public License for more details.
24     *
25     *  You should have received a copy of the GNU General Public License
26     *  along with this program; if not, write to the Free Software
27     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
28     *
29     ****************************************************************************/
30    
31    /*****************************************************************************
32     *
33     *  History
34     *
35     *  10.07.2002  added BFRAMES_DEC_DEBUG support
36     *              MinChen <chenm001@163.com>
37     *  20.06.2002 bframe patch
38     *  08.05.2002 fix some problem in DEBUG mode;
39     *             MinChen <chenm001@163.com>
40     *  14.04.2002 added FrameCodeB()
41     *
42     *  $Id$
43     *
44     ****************************************************************************/
45    
46    
47  #include <stdlib.h>  #include <stdlib.h>
48  #include <stdio.h>  #include <stdio.h>
49  #include <math.h>  #include <math.h>
50    #include <string.h>
51    
52  #include "encoder.h"  #include "encoder.h"
53  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
54  #include "global.h"  #include "global.h"
55  #include "utils/timer.h"  #include "utils/timer.h"
56  #include "image/image.h"  #include "image/image.h"
57    #ifdef BFRAMES
58    #include "image/font.h"
59    #include "motion/sad.h"
60    #endif
61    #include "motion/motion.h"
62  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
63  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
64  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
# Line 16  Line 68 
68  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
69  #include "quant/adapt_quant.h"  #include "quant/adapt_quant.h"
70  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
71    #include "utils/mem_align.h"
72    
73  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT  /*****************************************************************************
74     * Local macros
75     ****************************************************************************/
76    
77    #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT
78    #define SWAP(A,B)    { void * tmp = A; A = B; B = tmp; }
79    
80  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits);  /*****************************************************************************
81  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header);   * Local function prototypes
82     ****************************************************************************/
83    
84    static int FrameCodeI(Encoder * pEnc,
85                                              Bitstream * bs,
86                                              uint32_t * pBits);
87    
88    static int FrameCodeP(Encoder * pEnc,
89                                              Bitstream * bs,
90                                              uint32_t * pBits,
91                                              bool force_inter,
92                                              bool vol_header);
93    
94    static void FrameCodeB(Encoder * pEnc,
95                                               FRAMEINFO * frame,
96                                               Bitstream * bs,
97                                               uint32_t * pBits);
98    
99    /*****************************************************************************
100     * Local data
101     ****************************************************************************/
102    
103  static int DQtab[4] =  static int DQtab[4] = {
 {  
104          -1, -2, 1, 2          -1, -2, 1, 2
105  };  };
106    
107  static int iDQtab[5] =  static int iDQtab[5] = {
 {  
108          1, 0, NO_CHANGE, 2, 3          1, 0, NO_CHANGE, 2, 3
109  };  };
110    
111    
112  int encoder_create(XVID_ENC_PARAM * pParam)  static void __inline
113    image_null(IMAGE * image)
114    {
115            image->y = image->u = image->v = NULL;
116    }
117    
118    
119    /*****************************************************************************
120     * Encoder creation
121     *
122     * This function creates an Encoder instance, it allocates all necessary
123     * image buffers (reference, current and bframes) and initialize the internal
124     * xvid encoder paremeters according to the XVID_ENC_PARAM input parameter.
125     *
126     * The code seems to be very long but is very basic, mainly memory allocation
127     * and cleaning code.
128     *
129     * Returned values :
130     *    - XVID_ERR_OK     - no errors
131     *    - XVID_ERR_MEMORY - the libc could not allocate memory, the function
132     *                        cleans the structure before exiting.
133     *                        pParam->handle is also set to NULL.
134     *
135     ****************************************************************************/
136    
137    int
138    encoder_create(XVID_ENC_PARAM * pParam)
139  {  {
140          Encoder *pEnc;          Encoder *pEnc;
141          uint32_t i;          int i;
142    
143          pParam->handle = NULL;          pParam->handle = NULL;
144    
# Line 48  Line 149 
149          ENC_CHECK(!(pParam->width % 2));          ENC_CHECK(!(pParam->width % 2));
150          ENC_CHECK(!(pParam->height % 2));          ENC_CHECK(!(pParam->height % 2));
151    
152          if (pParam->fincr <= 0 || pParam->fbase <= 0)          /* Fps */
153          {  
154            if (pParam->fincr <= 0 || pParam->fbase <= 0) {
155                  pParam->fincr = 1;                  pParam->fincr = 1;
156                  pParam->fbase = 25;                  pParam->fbase = 25;
157          }          }
158    
159          // simplify the "fincr/fbase" fraction          /*
160          // (neccessary, since windows supplies us with huge numbers)           * Simplify the "fincr/fbase" fraction
161             * (neccessary, since windows supplies us with huge numbers)
162             */
163    
164          i = pParam->fincr;          i = pParam->fincr;
165          while (i > 1)          while (i > 1) {
166          {                  if (pParam->fincr % i == 0 && pParam->fbase % i == 0) {
                 if (pParam->fincr % i == 0 && pParam->fbase % i == 0)  
                 {  
167                          pParam->fincr /= i;                          pParam->fincr /= i;
168                          pParam->fbase /= i;                          pParam->fbase /= i;
169                          i = pParam->fincr;                          i = pParam->fincr;
# Line 70  Line 172 
172                  i--;                  i--;
173          }          }
174    
175          if (pParam->fbase > 65535)          if (pParam->fbase > 65535) {
         {  
176                  float div = (float)pParam->fbase / 65535;                  float div = (float)pParam->fbase / 65535;
177    
178                  pParam->fbase = (int)(pParam->fbase / div);                  pParam->fbase = (int)(pParam->fbase / div);
179                  pParam->fincr = (int)(pParam->fincr / div);                  pParam->fincr = (int)(pParam->fincr / div);
180          }          }
181    
182          if (pParam->bitrate <= 0)          /* Bitrate allocator defaults */
183                  pParam->bitrate = 900000;  
184            if (pParam->rc_bitrate <= 0)
185                    pParam->rc_bitrate = 900000;
186    
187            if (pParam->rc_reaction_delay_factor <= 0)
188                    pParam->rc_reaction_delay_factor = 16;
189    
190          if (pParam->rc_buffersize <= 0)          if (pParam->rc_averaging_period <= 0)
191                  pParam->rc_buffersize = pParam->bitrate * pParam->fbase;                  pParam->rc_averaging_period = 100;
192    
193            if (pParam->rc_buffer <= 0)
194                    pParam->rc_buffer = 100;
195    
196            /* Max and min quantizers */
197    
198          if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))          if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))
199                  pParam->min_quantizer = 1;                  pParam->min_quantizer = 1;
# Line 89  Line 201 
201          if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31))          if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31))
202                  pParam->max_quantizer = 31;                  pParam->max_quantizer = 31;
203    
         if (pParam->max_key_interval == 0)              /* 1 keyframe each 10 seconds */  
                 pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;  
   
204          if (pParam->max_quantizer < pParam->min_quantizer)          if (pParam->max_quantizer < pParam->min_quantizer)
205                  pParam->max_quantizer = pParam->min_quantizer;                  pParam->max_quantizer = pParam->min_quantizer;
206    
207          if ((pEnc = (Encoder *) malloc(sizeof(Encoder))) == NULL)          /* 1 keyframe each 10 seconds */
208    
209            if (pParam->max_key_interval <= 0)
210                    pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
211    
212            pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
213            if (pEnc == NULL)
214                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
215    
216            /* Zero the Encoder Structure */
217    
218            memset(pEnc, 0, sizeof(Encoder));
219    
220          /* Fill members of Encoder structure */          /* Fill members of Encoder structure */
221    
222          pEnc->mbParam.width = pParam->width;          pEnc->mbParam.width = pParam->width;
# Line 109  Line 228 
228          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;
229          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;
230    
231            pEnc->mbParam.fbase = pParam->fbase;
232            pEnc->mbParam.fincr = pParam->fincr;
233    
234            pEnc->mbParam.m_quant_type = H263_QUANT;
235    
236          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
237    
238          /* Fill rate control parameters */          /* Fill rate control parameters */
239    
240          pEnc->mbParam.quant = 4;          pEnc->bitrate = pParam->rc_bitrate;
   
         pEnc->bitrate = pParam->bitrate;  
241    
242          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
243          pEnc->iMaxKeyInterval = pParam->max_key_interval;          pEnc->iMaxKeyInterval = pParam->max_key_interval;
244    
245          if (image_create(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)          /* try to allocate frame memory */
246          {  
247                  free(pEnc);          pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
248            pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
249    
250            if (pEnc->current == NULL || pEnc->reference == NULL)
251                    goto xvid_err_memory1;
252    
253            /* try to allocate mb memory */
254    
255            pEnc->current->mbs =
256                    xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
257                                            pEnc->mbParam.mb_height, CACHE_LINE);
258            pEnc->reference->mbs =
259                    xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
260                                            pEnc->mbParam.mb_height, CACHE_LINE);
261    
262            if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
263                    goto xvid_err_memory2;
264    
265            /* try to allocate image memory */
266    
267    #ifdef _DEBUG_PSNR
268            image_null(&pEnc->sOriginal);
269    #endif
270    
271            image_null(&pEnc->f_refh);
272            image_null(&pEnc->f_refv);
273            image_null(&pEnc->f_refhv);
274    
275            image_null(&pEnc->current->image);
276            image_null(&pEnc->reference->image);
277            image_null(&pEnc->vInterH);
278            image_null(&pEnc->vInterV);
279            image_null(&pEnc->vInterVf);
280            image_null(&pEnc->vInterHV);
281            image_null(&pEnc->vInterHVf);
282    
283    #ifdef _DEBUG_PSNR
284            if (image_create
285                    (&pEnc->sOriginal, pEnc->mbParam.edged_width,
286                     pEnc->mbParam.edged_height) < 0)
287                    goto xvid_err_memory3;
288    #endif
289    
290            if (image_create
291                    (&pEnc->f_refh, pEnc->mbParam.edged_width,
292                     pEnc->mbParam.edged_height) < 0)
293                    goto xvid_err_memory3;
294            if (image_create
295                    (&pEnc->f_refv, pEnc->mbParam.edged_width,
296                     pEnc->mbParam.edged_height) < 0)
297                    goto xvid_err_memory3;
298            if (image_create
299                    (&pEnc->f_refhv, pEnc->mbParam.edged_width,
300                     pEnc->mbParam.edged_height) < 0)
301                    goto xvid_err_memory3;
302    
303            if (image_create
304                    (&pEnc->current->image, pEnc->mbParam.edged_width,
305                     pEnc->mbParam.edged_height) < 0)
306                    goto xvid_err_memory3;
307            if (image_create
308                    (&pEnc->reference->image, pEnc->mbParam.edged_width,
309                     pEnc->mbParam.edged_height) < 0)
310                    goto xvid_err_memory3;
311            if (image_create
312                    (&pEnc->vInterH, pEnc->mbParam.edged_width,
313                     pEnc->mbParam.edged_height) < 0)
314                    goto xvid_err_memory3;
315            if (image_create
316                    (&pEnc->vInterV, pEnc->mbParam.edged_width,
317                     pEnc->mbParam.edged_height) < 0)
318                    goto xvid_err_memory3;
319            if (image_create
320                    (&pEnc->vInterVf, pEnc->mbParam.edged_width,
321                     pEnc->mbParam.edged_height) < 0)
322                    goto xvid_err_memory3;
323            if (image_create
324                    (&pEnc->vInterHV, pEnc->mbParam.edged_width,
325                     pEnc->mbParam.edged_height) < 0)
326                    goto xvid_err_memory3;
327            if (image_create
328                    (&pEnc->vInterHVf, pEnc->mbParam.edged_width,
329                     pEnc->mbParam.edged_height) < 0)
330                    goto xvid_err_memory3;
331    
332    
333    
334            /* B Frames specific init */
335    
336            pEnc->global = pParam->global;
337            pEnc->mbParam.max_bframes = pParam->max_bframes;
338            pEnc->bquant_ratio = pParam->bquant_ratio;
339            pEnc->frame_drop_ratio = pParam->frame_drop_ratio;
340            pEnc->bframes = NULL;
341    
342            if (pEnc->mbParam.max_bframes > 0) {
343                    int n;
344    
345                    pEnc->bframes =
346                            xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),
347                                                    CACHE_LINE);
348    
349                    if (pEnc->bframes == NULL)
350                            goto xvid_err_memory3;
351    
352                    for (n = 0; n < pEnc->mbParam.max_bframes; n++)
353                            pEnc->bframes[n] = NULL;
354    
355    
356                    for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
357                            pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
358    
359                            if (pEnc->bframes[n] == NULL)
360                                    goto xvid_err_memory4;
361    
362                            pEnc->bframes[n]->mbs =
363                                    xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
364                                                            pEnc->mbParam.mb_height, CACHE_LINE);
365    
366                            if (pEnc->bframes[n]->mbs == NULL)
367                                    goto xvid_err_memory4;
368    
369                            image_null(&pEnc->bframes[n]->image);
370    
371                            if (image_create
372                                    (&pEnc->bframes[n]->image, pEnc->mbParam.edged_width,
373                                     pEnc->mbParam.edged_height) < 0)
374                                    goto xvid_err_memory4;
375    
376                    }
377            }
378    
379            pEnc->bframenum_head = 0;
380            pEnc->bframenum_tail = 0;
381            pEnc->flush_bframes = 0;
382            pEnc->bframenum_dx50bvop = -1;
383    
384            pEnc->queue = NULL;
385    
386    
387            if (pEnc->mbParam.max_bframes > 0) {
388                    int n;
389    
390                    pEnc->queue =
391                            xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE),
392                                                    CACHE_LINE);
393    
394                    if (pEnc->queue == NULL)
395                            goto xvid_err_memory4;
396    
397                    for (n = 0; n < pEnc->mbParam.max_bframes; n++)
398                            image_null(&pEnc->queue[n]);
399    
400                    for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
401                            if (image_create
402                                    (&pEnc->queue[n], pEnc->mbParam.edged_width,
403                                     pEnc->mbParam.edged_height) < 0)
404                                    goto xvid_err_memory5;
405    
406                    }
407            }
408    
409            pEnc->queue_head = 0;
410            pEnc->queue_tail = 0;
411            pEnc->queue_size = 0;
412    
413            pEnc->mbParam.m_stamp = 0;
414    
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);
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                  free(pEnc);  
528                  return XVID_ERR_MEMORY;          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          if (image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)          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);
599    
600            return XVID_ERR_OK;
601    }
602    
603    
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                  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                    /* write an empty marker to the bitstream.
748    
749                       for divx5 decoder compatibility, this marker must consist
750                       of a not-coded p-vop, with a time_base of zero, and time_increment
751                       indentical to the future-referece frame.
752                    */
753    
754                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
755                            int tmp;
756    
757                            DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
758                                    pEnc->bframenum_head, pEnc->bframenum_tail,
759                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
760    
761                            BitstreamPad(&bs);
762    
763                            tmp = pEnc->current->seconds;
764                            pEnc->current->seconds = 0; /* force time_base = 0 */
765                            BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
766                            pEnc->current->seconds = tmp;
767    
768                            pFrame->length = BitstreamLength(&bs);
769                            pFrame->intra = 0;
770    
771                            if (input_valid)
772                                    queue_image(pEnc, pFrame);
773    
774                            return XVID_ERR_OK;
775                    }
776            }
777    
778    
779    bvop_loop:
780    
781            if (pEnc->bframenum_dx50bvop != -1)
782            {
783    
784                    SWAP(pEnc->current, pEnc->reference);
785                    SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
786    
787                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
788                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
789                    }
790    
791                    if (input_valid)
792                    {
793                            queue_image(pEnc, pFrame);
794                            input_valid = 0;
795                    }
796    
797            } else if (input_valid) {
798    
799                    SWAP(pEnc->current, pEnc->reference);
800    
801                    start_timer();
802                    if (image_input
803                            (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
804                            pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
805                            return XVID_ERR_FORMAT;
806                    stop_conv_timer();
807    
808                    // queue input frame, and dequue next image
809                    if (pEnc->queue_size > 0)
810                    {
811                            image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
812                            if (pEnc->queue_head != pEnc->queue_tail)
813                            {
814                                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
815                            }
816                            pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
817                            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
818                    }
819    
820            } else if (pEnc->queue_size > 0) {
821    
822                    SWAP(pEnc->current, pEnc->reference);
823    
824                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
825                    pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
826                    pEnc->queue_size--;
827    
828            } else {
829    
830                    /* if nothing was encoded, write an 'ignore this frame' flag
831                       to the bitstream */
832    
833                    if (BitstreamPos(&bs) == 0) {
834    
835                            DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
836                                    pEnc->bframenum_head, pEnc->bframenum_tail,
837                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
838    
839                            BitstreamPutBits(&bs, 0x7f, 8);
840                            pFrame->intra = 0;
841                    }
842    
843                    pFrame->length = BitstreamLength(&bs);
844                    return XVID_ERR_OK;
845            }
846    
847            pEnc->flush_bframes = 0;
848    
849            emms();
850    
851            // only inc frame num, adapt quant, etc. if we havent seen it before
852            if (pEnc->bframenum_dx50bvop < 0 )
853            {
854                    if (pFrame->quant == 0)
855                            pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
856                    else
857                            pEnc->current->quant = pFrame->quant;
858    
859    /*              if (pEnc->current->quant < 1)
860                            pEnc->current->quant = 1;
861    
862                    if (pEnc->current->quant > 31)
863                            pEnc->current->quant = 31;
864    */
865                    pEnc->current->global_flags = pFrame->general;
866                    pEnc->current->motion_flags = pFrame->motion;
867    
868                    /* ToDo : dynamic fcode (in both directions) */
869                    pEnc->current->fcode = pEnc->mbParam.m_fcode;
870                    pEnc->current->bcode = pEnc->mbParam.m_fcode;
871    
872                    inc_frame_num(pEnc);
873    
874    #ifdef _DEBUG_PSNR
875                    image_copy(&pEnc->sOriginal, &pEnc->current->image,
876                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
877    #endif
878    
879                    emms();
880    
881                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
882                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
883                                    "%i  if:%i  st:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->stamp);
884                    }
885    
886            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
887             * Luminance masking
888             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
889    
890                    if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
891                            int *temp_dquants =
892                                    (int *) xvid_malloc(pEnc->mbParam.mb_width *
893                                                                    pEnc->mbParam.mb_height * sizeof(int),
894                                                                    CACHE_LINE);
895    
896                            pEnc->current->quant =
897                                    adaptive_quantization(pEnc->current->image.y,
898                                                                      pEnc->mbParam.edged_width, temp_dquants,
899                                                                      pEnc->current->quant, pEnc->current->quant,
900                                                                      2 * pEnc->current->quant,
901                                                                      pEnc->mbParam.mb_width,
902                                                                      pEnc->mbParam.mb_height);
903    
904                            for (y = 0; y < pEnc->mbParam.mb_height; y++) {
905    
906    #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
907    
908                                    for (x = 0; x < pEnc->mbParam.mb_width; x++) {
909                                            MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
910    
911                                            pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
912                                    }
913    
914    #undef OFFSET
915                            }
916    
917                            xvid_free(temp_dquants);
918                    }
919    
920            }
921    
922            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
923             * ivop/pvop/bvop selection
924             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
925    
926    
927            if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||
928                    (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&
929                     pEnc->iFrameNum >= pEnc->iMaxKeyInterval)
930                    || /*image_mad(&pEnc->reference->image, &pEnc->current->image,
931                                             pEnc->mbParam.edged_width, pEnc->mbParam.width,
932                                             pEnc->mbParam.height) > 30) {*/
933                            2 == (mode = MEanalysis(&pEnc->reference->image, &pEnc->current->image,
934                                             &pEnc->mbParam, pEnc->current->mbs, pEnc->current->fcode))) {
935    
936                    /*
937                     * This will be coded as an Intra Frame
938                     */
939    
940                    DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
941                                    pEnc->bframenum_head, pEnc->bframenum_tail,
942                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
943    
944                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
945                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
946                    }
947    
948                    // when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe
949    
950                    if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
951    
952                            pEnc->bframenum_tail--;
953                            pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
954    
955                            SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
956                            if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
957                                    image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
958                            }
959                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
960    
961                            pFrame->intra = 0;
962    
963                    } else {
964    
965                            FrameCodeI(pEnc, &bs, &bits);
966                            pFrame->intra = 1;
967    
968                            pEnc->bframenum_dx50bvop = -1;
969                    }
970    
971                    pEnc->flush_bframes = 1;
972    
973                    if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
974                            BitstreamPadAlways(&bs);
975                            input_valid = 0;
976                            goto ipvop_loop;
977          }          }
978    
979          if (image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)                  /*
980          {                   * NB : sequences like "IIBB" decode fine with msfdam but,
981                  image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                   *      go screwy with divx 5.00
982                  image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                   */
983                  image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) {
984                  free(pEnc);                  /*
985                  return XVID_ERR_MEMORY;                   * This will be coded as a Predicted Frame
986                     */
987    
988                    DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
989                                    pEnc->bframenum_head, pEnc->bframenum_tail,
990                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
991    
992                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
993                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
994          }          }
995    
996          if (image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)                  FrameCodeP(pEnc, &bs, &bits, 1, 0);
997          {                  pFrame->intra = 0;
998                  image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  pEnc->flush_bframes = 1;
999                  image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
1000                  image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1001                  image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                          BitstreamPadAlways(&bs);
1002                  free(pEnc);                          input_valid = 0;
1003                  return XVID_ERR_MEMORY;                          goto ipvop_loop;
1004          }          }
1005    
1006          pEnc->pMBs = malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);          } else {
1007          if (pEnc->pMBs == NULL)                  /*
1008          {                   * This will be coded as a Bidirectional Frame
1009                  image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                   */
1010                  image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
1011                  image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
1012                  image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
                 image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 free(pEnc);  
                 return XVID_ERR_MEMORY;  
1013          }          }
1014    
1015          // init macroblock array                  if (pFrame->bquant < 1) {
1016          for (i = 0; i < pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; i++)                          pEnc->current->quant =
1017          {                                  ((pEnc->reference->quant +
1018                  pEnc->pMBs[i].dquant = NO_CHANGE;                                    pEnc->current->quant) * pEnc->bquant_ratio) / 200;
1019                    } else {
1020                            pEnc->current->quant = pFrame->bquant;
1021          }          }
1022                    if (pEnc->current->quant < 1)
1023                            pEnc->current->quant = 1;
1024    
1025          pParam->handle = (void *)pEnc;                  if (pEnc->current->quant > 31)
1026                            pEnc->current->quant = 31;
1027    
         if (pParam->bitrate)  
         {  
                 RateControlInit(pParam->bitrate, pParam->rc_buffersize, pParam->fbase, pParam->width,  
                                 pParam->height, pParam->max_quantizer, pParam->min_quantizer);  
         }  
1028    
1029          create_vlc_tables();                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1030                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1031                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1032    
1033          return XVID_ERR_OK;  
1034    
1035                    /* store frame into bframe buffer & swap ref back to current */
1036                    SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1037                    SWAP(pEnc->current, pEnc->reference);
1038    
1039                    pEnc->bframenum_tail++;
1040    
1041                    pFrame->intra = 0;
1042                    pFrame->length = 0;
1043    
1044                    input_valid = 0;
1045                    goto bvop_loop;
1046  }  }
1047    
1048            pEnc->iFrameNum++;
1049    
1050  int encoder_destroy(Encoder * pEnc)          BitstreamPad(&bs);
1051  {          pFrame->length = BitstreamLength(&bs);
1052          ENC_CHECK(pEnc);  
1053          ENC_CHECK(pEnc->sCurrent.y);          if (pResult) {
1054          ENC_CHECK(pEnc->sReference.y);                  pResult->quant = pEnc->current->quant;
1055                    pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
1056                    pResult->kblks = pEnc->sStat.kblks;
1057                    pResult->mblks = pEnc->sStat.mblks;
1058                    pResult->ublks = pEnc->sStat.ublks;
1059            }
1060    
1061            emms();
1062    
1063          free(pEnc->pMBs);  #ifdef _DEBUG_PSNR
1064          image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          psnr =
1065          image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1066          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
1067          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                                     pEnc->mbParam.height);
1068          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
1069          free(pEnc);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1070            DEBUG(temp);
1071    #endif
1072    
1073            if (pFrame->quant == 0) {
1074                    RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1075                                                      pFrame->length, pFrame->intra);
1076            }
1077    
1078          destroy_vlc_tables();          stop_global_timer();
1079            write_timer();
1080    
1081          return XVID_ERR_OK;          return XVID_ERR_OK;
1082  }  }
1083    
1084  int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult)  
1085    
1086    /*****************************************************************************
1087     * "original" IP frame encoder entry point
1088     *
1089     * Returned values :
1090     *    - XVID_ERR_OK     - no errors
1091     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
1092     *                        format
1093     ****************************************************************************/
1094    
1095    int
1096    encoder_encode(Encoder * pEnc,
1097                               XVID_ENC_FRAME * pFrame,
1098                               XVID_ENC_STATS * pResult)
1099  {  {
1100          uint16_t x, y;          uint16_t x, y;
1101          Bitstream bs;          Bitstream bs;
1102          uint32_t bits;          uint32_t bits;
1103          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
1104    
1105    #ifdef _DEBUG_PSNR
1106            float psnr;
1107            uint8_t temp[128];
1108    #endif
1109    
1110          start_global_timer();          start_global_timer();
1111    
1112          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
# Line 225  Line 1114 
1114          ENC_CHECK(pFrame->bitstream);          ENC_CHECK(pFrame->bitstream);
1115          ENC_CHECK(pFrame->image);          ENC_CHECK(pFrame->image);
1116    
1117          pEnc->mbParam.global_flags = pFrame->general;          SWAP(pEnc->current, pEnc->reference);
         pEnc->mbParam.motion_flags = pFrame->motion;  
1118    
1119          start_timer();          pEnc->current->global_flags = pFrame->general;
1120          if (image_input(&pEnc->sCurrent, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width,          pEnc->current->motion_flags = pFrame->motion;
1121                          pFrame->image, pFrame->colorspace))          pEnc->mbParam.hint = &pFrame->hint;
1122    
1123            inc_frame_num(pEnc);
1124    
1125            /* disable alternate scan flag if interlacing is not enabled */
1126            if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) &&
1127                    !(pEnc->current->global_flags & XVID_INTERLACING))
1128          {          {
1129                  return XVID_ERR_FORMAT;                  pEnc->current->global_flags -= XVID_ALTERNATESCAN;
1130          }          }
1131    
1132            start_timer();
1133            if (image_input
1134                    (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
1135                     pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0)
1136                    return XVID_ERR_FORMAT;
1137          stop_conv_timer();          stop_conv_timer();
1138    
1139    #ifdef _DEBUG_PSNR
1140            image_copy(&pEnc->sOriginal, &pEnc->current->image,
1141                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
1142    #endif
1143    
1144            emms();
1145    
1146          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
1147    
1148          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
1149          {                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
1150                  pEnc->mbParam.quant = RateControlGetQ(0);          } else {
1151          }                  pEnc->current->quant = pFrame->quant;
         else  
         {  
                 pEnc->mbParam.quant = pFrame->quant;  
1152          }          }
1153    
1154          if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0)          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1155          {                  int *temp_dquants =
1156                  int * temp_dquants = (int *) malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int));                          (int *) xvid_malloc(pEnc->mbParam.mb_width *
1157                                                                    pEnc->mbParam.mb_height * sizeof(int),
1158                                                                    CACHE_LINE);
1159    
1160                  pEnc->mbParam.quant = adaptive_quantization(pEnc->sCurrent.y, pEnc->mbParam.width,                  pEnc->current->quant =
1161                                                              temp_dquants, pFrame->quant, pFrame->quant,                          adaptive_quantization(pEnc->current->image.y,
1162                                                              2*pFrame->quant, pEnc->mbParam.mb_width, pEnc->mbParam.mb_height);                                                                    pEnc->mbParam.edged_width, temp_dquants,
1163                                                                      pEnc->current->quant, pEnc->current->quant,
1164                                                                      2 * pEnc->current->quant,
1165                                                                      pEnc->mbParam.mb_width,
1166                                                                      pEnc->mbParam.mb_height);
1167    
1168                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                  for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1169                          for (x = 0; x < pEnc->mbParam.mb_width; x++)  
1170                          {  #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
1171                                  MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];  
1172                                  pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)];                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1173    
1174    
1175                                    MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
1176    
1177                                    pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
1178                          }                          }
1179                  free(temp_dquants);  
1180    #undef OFFSET
1181          }          }
1182    
1183          if(pEnc->mbParam.global_flags & XVID_H263QUANT) {                  xvid_free(temp_dquants);
                 if(pEnc->mbParam.quant_type != H263_QUANT)  
                         write_vol_header = 1;  
                 pEnc->mbParam.quant_type = H263_QUANT;  
1184          }          }
         else if(pEnc->mbParam.global_flags & XVID_MPEGQUANT) {  
                 int ret1, ret2;  
1185    
1186                  if(pEnc->mbParam.quant_type != MPEG4_QUANT)          if (pEnc->current->global_flags & XVID_H263QUANT) {
1187                    if (pEnc->mbParam.m_quant_type != H263_QUANT)
1188                          write_vol_header = 1;                          write_vol_header = 1;
1189                    pEnc->mbParam.m_quant_type = H263_QUANT;
1190            } else if (pEnc->current->global_flags & XVID_MPEGQUANT) {
1191                    int matrix1_changed, matrix2_changed;
1192    
1193                  pEnc->mbParam.quant_type = MPEG4_QUANT;                  matrix1_changed = matrix2_changed = 0;
1194    
1195                  if ((pEnc->mbParam.global_flags & XVID_CUSTOM_QMATRIX) > 0) {                  if (pEnc->mbParam.m_quant_type != MPEG4_QUANT)
1196                            write_vol_header = 1;
1197    
1198                    pEnc->mbParam.m_quant_type = MPEG4_QUANT;
1199    
1200                    if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1201                          if(pFrame->quant_intra_matrix != NULL)                          if(pFrame->quant_intra_matrix != NULL)
1202                                  ret1 = set_intra_matrix(pFrame->quant_intra_matrix);                                  matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix);
1203                          if(pFrame->quant_inter_matrix != NULL)                          if(pFrame->quant_inter_matrix != NULL)
1204                                  ret2 = set_inter_matrix(pFrame->quant_inter_matrix);                                  matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix);
1205                  }                  } else {
1206                  else {                          matrix1_changed = set_intra_matrix(get_default_intra_matrix());
1207                          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());  
1208                  }                  }
1209                  if(write_vol_header == 0)                  if(write_vol_header == 0)
1210                          write_vol_header = ret1 | ret2;                          write_vol_header = matrix1_changed | matrix2_changed;
1211          }          }
1212    
1213          if (pFrame->intra < 0)          if (pFrame->intra < 0) {
1214          {                  if ((pEnc->iFrameNum == 0)
1215                  if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0)                          || ((pEnc->iMaxKeyInterval > 0)
1216                                                 && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval)))                                  && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {
   
1217                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1218                  else                  } else {
1219                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
1220          }          }
1221          else          } else {
1222          {                  if (pFrame->intra == 1) {
                 if (pFrame->intra == 1)  
1223                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1224                  else                  } else {
1225                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);
1226          }          }
1227    
1228            }
1229    
1230          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1231          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1232          BitstreamPad(&bs);          BitstreamPad(&bs);
1233          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1234    
1235          if (pResult)          if (pResult) {
1236          {                  pResult->quant = pEnc->current->quant;
                 pResult->quant = pEnc->mbParam.quant;  
1237                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
1238                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->sStat.kblks;
1239                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->sStat.mblks;
1240                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
1241          }          }
1242    
1243          if (pFrame->quant == 0)          emms();
1244          {  
1245                  RateControlUpdate(pEnc->mbParam.quant, pFrame->length, pFrame->intra);          if (pFrame->quant == 0) {
1246          }                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1247                                                      pFrame->length, pFrame->intra);
1248            }
1249    #ifdef _DEBUG_PSNR
1250            psnr =
1251                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1252                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
1253                                       pEnc->mbParam.height);
1254    
1255            snprintf(temp, 127, "PSNR: %f\n", psnr);
1256            DEBUG(temp);
1257    #endif
1258    
1259          pEnc->iFrameNum++;          pEnc->iFrameNum++;
         image_swap(&pEnc->sCurrent, &pEnc->sReference);  
1260    
1261          stop_global_timer();          stop_global_timer();
1262          write_timer();          write_timer();
# Line 337  Line 1265 
1265  }  }
1266    
1267    
1268  static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) {  static __inline void
1269    CodeIntraMB(Encoder * pEnc,
1270                            MACROBLOCK * pMB)
1271    {
1272    
1273          pMB->mode = MODE_INTRA;          pMB->mode = MODE_INTRA;
1274    
1275          if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) {          /* zero mv statistics */
1276                  if(pMB->dquant != NO_CHANGE)          pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0;
1277                  {          pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0;
1278            pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;
1279            pMB->sad16 = 0;
1280    
1281            if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1282                    if (pMB->dquant != NO_CHANGE) {
1283                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
1284                          pEnc->mbParam.quant += DQtab[pMB->dquant];                          pEnc->current->quant += DQtab[pMB->dquant];
1285    
1286                            if (pEnc->current->quant > 31)
1287                                    pEnc->current->quant = 31;
1288                            if (pEnc->current->quant < 1)
1289                                    pEnc->current->quant = 1;
1290                    }
1291            }
1292    
1293            pMB->quant = pEnc->current->quant;
1294    }
1295    
1296    
1297    #define FCODEBITS       3
1298    #define MODEBITS        5
1299    
1300    void
1301    HintedMESet(Encoder * pEnc,
1302                            int *intra)
1303    {
1304            HINTINFO *hint;
1305            Bitstream bs;
1306            int length, high;
1307            uint32_t x, y;
1308    
1309            hint = pEnc->mbParam.hint;
1310    
1311            if (hint->rawhints) {
1312                    *intra = hint->mvhint.intra;
1313            } else {
1314                    BitstreamInit(&bs, hint->hintstream, hint->hintlength);
1315                    *intra = BitstreamGetBit(&bs);
1316            }
1317    
1318            if (*intra) {
1319                    return;
1320            }
1321    
1322            pEnc->current->fcode =
1323                    (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs,
1324                                                                                                                                     FCODEBITS);
1325    
1326            length = pEnc->current->fcode + 5;
1327            high = 1 << (length - 1);
1328    
1329            for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1330                    for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1331                            MACROBLOCK *pMB =
1332                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1333                            MVBLOCKHINT *bhint =
1334                                    &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1335                            VECTOR pred;
1336                            VECTOR tmp;
1337                            int vec;
1338    
1339                            pMB->mode =
1340                                    (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs,
1341                                                                                                                                      MODEBITS);
1342    
1343                            pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
1344                            pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
1345    
1346                            if (pMB->mode == MODE_INTER) {
1347                                    tmp.x =
1348                                            (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs,
1349                                                                                                                                                      length);
1350                                    tmp.y =
1351                                            (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs,
1352                                                                                                                                                      length);
1353                                    tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1354                                    tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1355    
1356                                    pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,0);
1357    
1358                                    for (vec = 0; vec < 4; ++vec) {
1359                                            pMB->mvs[vec].x = tmp.x;
1360                                            pMB->mvs[vec].y = tmp.y;
1361                                            pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
1362                                            pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
1363                                    }
1364                            } else if (pMB->mode == MODE_INTER4V) {
1365                                    for (vec = 0; vec < 4; ++vec) {
1366                                            tmp.x =
1367                                                    (hint->rawhints) ? bhint->mvs[vec].
1368                                                    x : BitstreamGetBits(&bs, length);
1369                                            tmp.y =
1370                                                    (hint->rawhints) ? bhint->mvs[vec].
1371                                                    y : BitstreamGetBits(&bs, length);
1372                                            tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1373                                            tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1374    
1375                                            pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,vec);
1376    
1377                                            pMB->mvs[vec].x = tmp.x;
1378                                            pMB->mvs[vec].y = tmp.y;
1379                                            pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
1380                                            pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
1381                                    }
1382                            } else                          // intra / stuffing / not_coded
1383                            {
1384                                    for (vec = 0; vec < 4; ++vec) {
1385                                            pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
1386                                    }
1387                            }
1388    
1389                            if (pMB->mode == MODE_INTER4V &&
1390                                    (pEnc->current->global_flags & XVID_LUMIMASKING)
1391                                    && pMB->dquant != NO_CHANGE) {
1392                                    pMB->mode = MODE_INTRA;
1393    
1394                                    for (vec = 0; vec < 4; ++vec) {
1395                                            pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
1396                                    }
1397                            }
1398                    }
1399            }
1400    }
1401    
1402    
1403    void
1404    HintedMEGet(Encoder * pEnc,
1405                            int intra)
1406    {
1407            HINTINFO *hint;
1408            Bitstream bs;
1409            uint32_t x, y;
1410            int length, high;
1411    
1412            hint = pEnc->mbParam.hint;
1413    
1414            if (hint->rawhints) {
1415                    hint->mvhint.intra = intra;
1416            } else {
1417                    BitstreamInit(&bs, hint->hintstream, 0);
1418                    BitstreamPutBit(&bs, intra);
1419            }
1420    
1421            if (intra) {
1422                    if (!hint->rawhints) {
1423                            BitstreamPad(&bs);
1424                            hint->hintlength = BitstreamLength(&bs);
1425                    }
1426                    return;
1427            }
1428    
1429            length = pEnc->current->fcode + 5;
1430            high = 1 << (length - 1);
1431    
1432            if (hint->rawhints) {
1433                    hint->mvhint.fcode = pEnc->current->fcode;
1434            } else {
1435                    BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);
1436            }
1437    
1438            for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1439                    for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1440                            MACROBLOCK *pMB =
1441                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1442                            MVBLOCKHINT *bhint =
1443                                    &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1444                            VECTOR tmp;
1445    
1446                            if (hint->rawhints) {
1447                                    bhint->mode = pMB->mode;
1448                            } else {
1449                                    BitstreamPutBits(&bs, pMB->mode, MODEBITS);
1450                            }
1451    
1452                            if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
1453                                    tmp.x = pMB->mvs[0].x;
1454                                    tmp.y = pMB->mvs[0].y;
1455                                    tmp.x += (tmp.x < 0) ? high * 2 : 0;
1456                                    tmp.y += (tmp.y < 0) ? high * 2 : 0;
1457    
1458                                    if (hint->rawhints) {
1459                                            bhint->mvs[0].x = tmp.x;
1460                                            bhint->mvs[0].y = tmp.y;
1461                                    } else {
1462                                            BitstreamPutBits(&bs, tmp.x, length);
1463                                            BitstreamPutBits(&bs, tmp.y, length);
1464                                    }
1465                            } else if (pMB->mode == MODE_INTER4V) {
1466                                    int vec;
1467    
1468                                    for (vec = 0; vec < 4; ++vec) {
1469                                            tmp.x = pMB->mvs[vec].x;
1470                                            tmp.y = pMB->mvs[vec].y;
1471                                            tmp.x += (tmp.x < 0) ? high * 2 : 0;
1472                                            tmp.y += (tmp.y < 0) ? high * 2 : 0;
1473    
1474                          if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31;                                          if (hint->rawhints) {
1475                          if (pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1;                                                  bhint->mvs[vec].x = tmp.x;
1476                                                    bhint->mvs[vec].y = tmp.y;
1477                                            } else {
1478                                                    BitstreamPutBits(&bs, tmp.x, length);
1479                                                    BitstreamPutBits(&bs, tmp.y, length);
1480                                            }
1481                                    }
1482                            }
1483                  }                  }
1484          }          }
1485    
1486          pMB->quant = pEnc->mbParam.quant;          if (!hint->rawhints) {
1487                    BitstreamPad(&bs);
1488                    hint->hintlength = BitstreamLength(&bs);
1489            }
1490  }  }
1491    
1492    
1493  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits)  static int
1494    FrameCodeI(Encoder * pEnc,
1495                       Bitstream * bs,
1496                       uint32_t * pBits)
1497  {  {
1498          int16_t dct_codes[6][64];  
1499          int16_t qcoeff[6][64];          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1500            DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1501    
1502          uint16_t x, y;          uint16_t x, y;
1503    
1504          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
1505          pEnc->mbParam.rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1506          pEnc->mbParam.coding_type = I_VOP;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1507            pEnc->current->coding_type = I_VOP;
1508    
1509          BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1510          BitstreamWriteVopHeader(bs, I_VOP, pEnc->mbParam.rounding_type,  
1511                                  pEnc->mbParam.quant,  #define DIVX501B481P "DivX501b481p"
1512                                  pEnc->mbParam.fixed_code);          if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1513                    BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));
1514            }
1515    
1516            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1517            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1518    
1519          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1520    
# Line 378  Line 1523 
1523          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1524    
1525          for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++)
1526                  for (x = 0; x < pEnc->mbParam.mb_width; x++)                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1527                  {                          MACROBLOCK *pMB =
1528                          MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1529    
1530                          CodeIntraMB(pEnc, pMB);                          CodeIntraMB(pEnc, pMB);
1531    
1532                          MBTransQuantIntra(&pEnc->mbParam, x, y, dct_codes, qcoeff, &pEnc->sCurrent);                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1533                                                              dct_codes, qcoeff);
1534    
1535                          start_timer();                          start_timer();
1536                          MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1537                          stop_prediction_timer();                          stop_prediction_timer();
1538    
1539                          start_timer();                          start_timer();
1540                          MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat);                          if (pEnc->current->global_flags & XVID_GREYSCALE)
1541                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1542                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1543                                    qcoeff[5*64+0]=0;
1544                            }
1545                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1546                          stop_coding_timer();                          stop_coding_timer();
1547                  }                  }
1548    
# Line 401  Line 1552 
1552          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
1553          pEnc->sStat.iMvSum = 0;          pEnc->sStat.iMvSum = 0;
1554          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
1555          pEnc->mbParam.fixed_code = 2;          pEnc->mbParam.m_fcode = 2;
1556    
1557            if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1558                    HintedMEGet(pEnc, 1);
1559            }
1560    
1561          return 1;                                        // intra          return 1;                                        // intra
1562  }  }
1563    
1564    
1565  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1566    #define BFRAME_SKIP_THRESHHOLD 30
1567    
1568  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header)  static int
1569    FrameCodeP(Encoder * pEnc,
1570                       Bitstream * bs,
1571                       uint32_t * pBits,
1572                       bool force_inter,
1573                       bool vol_header)
1574  {  {
1575          float fSigma;          float fSigma;
1576          int16_t dct_codes[6][64];  
1577          int16_t qcoeff[6][64];          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1578            DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1579    
1580          int iLimit;          int iLimit;
1581          uint32_t x, y;          int x, y, k;
1582          int iSearchRange;          int iSearchRange;
1583          bool bIntra;          int bIntra;
1584    
1585          IMAGE *pCurrent = &pEnc->sCurrent;          /* IMAGE *pCurrent = &pEnc->current->image; */
1586          IMAGE *pRef = &pEnc->sReference;          IMAGE *pRef = &pEnc->reference->image;
1587    
1588          image_setedges(pRef,pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height);          start_timer();
1589            image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1590          pEnc->mbParam.rounding_type = 1 - pEnc->mbParam.rounding_type;                                     pEnc->mbParam.width, pEnc->mbParam.height);
1591            stop_edges_timer();
1592    
1593            pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
1594            pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1595            pEnc->current->fcode = pEnc->mbParam.m_fcode;
1596    
1597          if (!force_inter)          if (!force_inter)
1598                  iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD);                  iLimit =
1599                            (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height *
1600                                       INTRA_THRESHOLD);
1601          else          else
1602                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;
1603    
1604          if ((pEnc->mbParam.global_flags & XVID_HALFPEL) > 0) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
1605                  start_timer();                  start_timer();
1606                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1607                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1608                                    pEnc->mbParam.rounding_type);                                                    pEnc->mbParam.edged_height,
1609                                                      pEnc->current->rounding_type);
1610                  stop_inter_timer();                  stop_inter_timer();
1611          }          }
1612    
1613          start_timer();          start_timer();
1614          bIntra = MotionEstimation(pEnc->pMBs, &pEnc->mbParam, &pEnc->sReference,          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
1615                                    &pEnc->vInterH, &pEnc->vInterV,                  HintedMESet(pEnc, &bIntra);
1616                                    &pEnc->vInterHV, &pEnc->sCurrent, iLimit);          if (bIntra == 0) {
1617                            pEnc->current->fcode = FindFcode(&pEnc->mbParam, pEnc->current);
1618                            MotionEstimationHinted(&pEnc->mbParam, pEnc->current, pEnc->reference,
1619                                                                                            &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);
1620                    }
1621    
1622            } else {
1623    
1624            bIntra =
1625                    MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1626                             &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1627                             iLimit);
1628    
1629            }
1630          stop_motion_timer();          stop_motion_timer();
1631    
1632          if (bIntra == 1)          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
                 return FrameCodeI(pEnc, bs, pBits);  
1633    
1634          pEnc->mbParam.coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
1635    
1636          if(vol_header)          if(vol_header)
1637                  BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1638    
1639    
1640          BitstreamWriteVopHeader(bs, P_VOP, pEnc->mbParam.rounding_type,          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1641                                  pEnc->mbParam.quant,          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
                                 pEnc->mbParam.fixed_code);  
1642    
1643          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1644    
1645          pEnc->sStat.iTextBits = 0;          pEnc->sStat.iTextBits = pEnc->sStat.iMvSum = pEnc->sStat.iMvCount =
         pEnc->sStat.iMvSum = 0;  
         pEnc->sStat.iMvCount = 0;  
1646          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1647    
1648          for(y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1649          {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1650                  for(x = 0; x < pEnc->mbParam.mb_width; x++)                          MACROBLOCK *pMB =
1651                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
                         MACROBLOCK * pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];  
1652    
1653                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1654    
1655                          if (!bIntra)                          if (!bIntra) {
                         {  
1656                                  start_timer();                                  start_timer();
1657                                  MBMotionCompensation(pMB, x, y, &pEnc->sReference,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1658                                                       &pEnc->vInterH, &pEnc->vInterV,                                                       &pEnc->vInterH, &pEnc->vInterV,
1659                                                       &pEnc->vInterHV, &pEnc->sCurrent, dct_codes,                                                                           &pEnc->vInterHV, &pEnc->current->image,
1660                                                       pEnc->mbParam.width,                                                                           dct_codes, pEnc->mbParam.width,
1661                                                       pEnc->mbParam.height,                                                       pEnc->mbParam.height,
1662                                                       pEnc->mbParam.edged_width,                                                       pEnc->mbParam.edged_width,
1663                                                       pEnc->mbParam.rounding_type);                                                                           pEnc->current->rounding_type);
1664                                  stop_comp_timer();                                  stop_comp_timer();
1665    
1666                                  if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) {                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1667                                          if(pMB->dquant != NO_CHANGE) {                                          if(pMB->dquant != NO_CHANGE) {
1668                                                  pMB->mode = MODE_INTER_Q;                                                  pMB->mode = MODE_INTER_Q;
1669                                                  pEnc->mbParam.quant += DQtab[pMB->dquant];                                                  pEnc->current->quant += DQtab[pMB->dquant];
1670                                                  if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31;                                                  if (pEnc->current->quant > 31)
1671                                                  else if(pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1;                                                          pEnc->current->quant = 31;
1672                                          }                                                  else if (pEnc->current->quant < 1)
1673                                  }                                                          pEnc->current->quant = 1;
1674                                  pMB->quant = pEnc->mbParam.quant;                                          }
1675                                    }
1676                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, x, y, dct_codes, qcoeff, pCurrent);                                  pMB->quant = pEnc->current->quant;
1677                          }  
1678                          else                                  pMB->field_pred = 0;
1679                          {  
1680                                    if (pMB->mode != MODE_NOT_CODED)
1681                                            pMB->cbp =
1682                                                    MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1683                                                                                      dct_codes, qcoeff);
1684                            } else {
1685                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
1686                                  MBTransQuantIntra(&pEnc->mbParam, x, y, dct_codes, qcoeff, pCurrent);                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1687                                                                      dct_codes, qcoeff);
1688                          }                          }
1689    
1690                          start_timer();                          start_timer();
1691                          MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1692                          stop_prediction_timer();                          stop_prediction_timer();
1693    
1694                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
                         {  
1695                                  pEnc->sStat.kblks++;                                  pEnc->sStat.kblks++;
1696                            } else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1697                                               pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1698                                               pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1699                                    pEnc->sStat.mblks++;
1700                            }  else {
1701                                    pEnc->sStat.ublks++;
1702                          }                          }
1703                          else if (pMB->cbp ||  
1704                                   pMB->mvs[0].x || pMB->mvs[0].y ||                          start_timer();
1705                                   pMB->mvs[1].x || pMB->mvs[1].y ||  
1706                                   pMB->mvs[2].x || pMB->mvs[2].y ||                          /* Finished processing the MB, now check if to CODE or SKIP */
1707                                   pMB->mvs[3].x || pMB->mvs[3].y)  
1708                            if ((pMB->mode == MODE_NOT_CODED) ||
1709                                    (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 &&
1710                                    pMB->mvs[0].y == 0 && pMB->dquant == NO_CHANGE)) {
1711    
1712    /* This is a candidate for SKIPping, but check intermediate B-frames first */
1713    
1714                                    int bSkip = 1;
1715                                    pMB->mode = MODE_NOT_CODED;
1716    
1717                                    for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1718                          {                          {
1719                                  pEnc->sStat.mblks++;                                          int iSAD;
1720                                            iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1721                                                                    pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1722                                                                    pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
1723                                            if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)
1724                                            {       bSkip = 0;
1725                                                    break;
1726                          }                          }
1727                          else                                  }
1728                                    if (!bSkip)
1729                          {                          {
1730                                  pEnc->sStat.ublks++;                                          VECTOR predMV;
1731                                            predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1732                                            pMB->pmvs[0].x = -predMV.x; pMB->pmvs[0].y = -predMV.y;
1733                                            pMB->mode = MODE_INTER;
1734                                            pMB->cbp = 0;
1735                                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1736                                    }
1737                                    else MBSkip(bs);
1738    
1739                            } else {
1740                                    if (pEnc->current->global_flags & XVID_GREYSCALE)
1741                                    {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1742                                            qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1743                                            qcoeff[5*64+0]=0;
1744                                    }
1745                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1746                          }                          }
1747    
                         start_timer();  
                         MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat);  
1748                          stop_coding_timer();                          stop_coding_timer();
1749                  }                  }
1750          }          }
1751    
1752          emms();          emms();
1753    
1754            if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1755                    HintedMEGet(pEnc, 0);
1756            }
1757    
1758          if (pEnc->sStat.iMvCount == 0)          if (pEnc->sStat.iMvCount == 0)
1759                  pEnc->sStat.iMvCount = 1;                  pEnc->sStat.iMvCount = 1;
1760    
1761          fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);          fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);
1762    
1763          iSearchRange = 1 << (3 + pEnc->mbParam.fixed_code);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
1764    
1765          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
1766              && (pEnc->mbParam.fixed_code <= 3)) // maximum search range 128                  && (pEnc->mbParam.m_fcode <= 3))        // maximum search range 128
1767          {          {
1768                  pEnc->mbParam.fixed_code++;                  pEnc->mbParam.m_fcode++;
1769                  iSearchRange *= 2;                  iSearchRange *= 2;
1770          }          } else if ((fSigma < iSearchRange / 6)
         else if ((fSigma < iSearchRange / 6)  
1771                   && (pEnc->sStat.fMvPrevSigma >= 0)                   && (pEnc->sStat.fMvPrevSigma >= 0)
1772                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)
1773                   && (pEnc->mbParam.fixed_code >= 2))    // minimum search range 16                             && (pEnc->mbParam.m_fcode >= 2))     // minimum search range 16
1774          {          {
1775                  pEnc->mbParam.fixed_code--;                  pEnc->mbParam.m_fcode--;
1776                  iSearchRange /= 2;                  iSearchRange /= 2;
1777          }          }
1778    
1779          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->sStat.fMvPrevSigma = fSigma;
1780    
1781    #ifdef FRAMEDROP
1782            /* frame drop code */
1783            // DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks);
1784            if (pEnc->sStat.kblks + pEnc->sStat.mblks <
1785                    (pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100)
1786            {
1787                    pEnc->sStat.kblks = pEnc->sStat.mblks = 0;
1788                    pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1789    
1790                    BitstreamReset(bs);
1791    
1792                    set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1793                    BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
1794    
1795                    // copy reference frame details into the current frame
1796                    pEnc->current->quant = pEnc->reference->quant;
1797                    pEnc->current->motion_flags = pEnc->reference->motion_flags;
1798                    pEnc->current->rounding_type = pEnc->reference->rounding_type;
1799                    pEnc->current->fcode = pEnc->reference->fcode;
1800                    pEnc->current->bcode = pEnc->reference->bcode;
1801                    image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
1802                    memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);
1803    
1804            }
1805    #endif
1806    
1807          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1808    
1809          return 0;                                        // inter          return 0;                                        // inter
1810  }  }
1811    
1812    
1813    static __inline void
1814    FrameCodeB(Encoder * pEnc,
1815                       FRAMEINFO * frame,
1816                       Bitstream * bs,
1817                       uint32_t * pBits)
1818    {
1819            int16_t dct_codes[6 * 64];
1820            int16_t qcoeff[6 * 64];
1821            uint32_t x, y;
1822    
1823            IMAGE *f_ref = &pEnc->reference->image;
1824            IMAGE *b_ref = &pEnc->current->image;
1825    
1826    #ifdef BFRAMES_DEC_DEBUG
1827            FILE *fp;
1828            static char first=0;
1829    #define BFRAME_DEBUG    if (!first && fp){ \
1830                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
1831            }
1832    
1833            if (!first){
1834                    fp=fopen("C:\\XVIDDBGE.TXT","w");
1835            }
1836    #endif
1837    
1838            // forward
1839            image_setedges(f_ref, pEnc->mbParam.edged_width,
1840                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1841                                       pEnc->mbParam.height);
1842            start_timer();
1843            image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1844                                              pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1845                                              0);
1846            stop_inter_timer();
1847    
1848            // backward
1849            image_setedges(b_ref, pEnc->mbParam.edged_width,
1850                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1851                                       pEnc->mbParam.height);
1852            start_timer();
1853            image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1854                                              pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1855                                              0);
1856            stop_inter_timer();
1857    
1858            start_timer();
1859    
1860            MotionEstimationBVOP(&pEnc->mbParam, frame,
1861                    ((int32_t)(pEnc->current->stamp - frame->stamp)),                               // time_bp
1862                    ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),     // time_pp
1863                            pEnc->reference->mbs, f_ref,
1864                                                     &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1865                                                     pEnc->current->mbs, b_ref, &pEnc->vInterH,
1866                                                     &pEnc->vInterV, &pEnc->vInterHV);
1867    
1868    
1869            stop_motion_timer();
1870    
1871            /*if (test_quant_type(&pEnc->mbParam, pEnc->current))
1872               {
1873               BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);
1874               } */
1875    
1876            frame->coding_type = B_VOP;
1877    
1878            set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
1879            BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1880    
1881            *pBits = BitstreamPos(bs);
1882    
1883            pEnc->sStat.iTextBits = 0;
1884            pEnc->sStat.iMvSum = 0;
1885            pEnc->sStat.iMvCount = 0;
1886            pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1887    
1888    
1889            for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1890                    for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1891                            MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1892                            int direction = pEnc->global & XVID_ALTERNATESCAN ? 2 : 0;
1893    
1894                            // decoder ignores mb when refence block is INTER(0,0), CBP=0
1895                            if (mb->mode == MODE_NOT_CODED) {
1896                                    //mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0;
1897                                    continue;
1898                            }
1899    
1900                            if (mb->mode != MODE_DIRECT_NONE_MV) {
1901                                    MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1902                                                                             f_ref, &pEnc->f_refh, &pEnc->f_refv,
1903                                                                             &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1904                                                                             &pEnc->vInterV, &pEnc->vInterHV,
1905                                                                             dct_codes);
1906    
1907                                    if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
1908                                    mb->quant = frame->quant;
1909    
1910                                    mb->cbp =
1911                                            MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);
1912    
1913                                    if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1914                                            && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
1915                                            mb->mode = MODE_DIRECT_NONE_MV; // skipped
1916                                    }
1917                            }
1918    
1919    #ifdef BFRAMES_DEC_DEBUG
1920            BFRAME_DEBUG
1921    #endif
1922                            start_timer();
1923                            MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1924                                                     &pEnc->sStat, direction);
1925                            stop_coding_timer();
1926                    }
1927            }
1928    
1929            emms();
1930    
1931            // TODO: dynamic fcode/bcode ???
1932    
1933            *pBits = BitstreamPos(bs) - *pBits;
1934    
1935    #ifdef BFRAMES_DEC_DEBUG
1936            if (!first){
1937                    first=1;
1938                    if (fp)
1939                            fclose(fp);
1940            }
1941    #endif
1942    }
1943    
1944    
1945    /*      in case internal output is needed somewhere... */
1946    /*      {
1947            FILE *filehandle;
1948            filehandle=fopen("last-b.pgm","wb");
1949            if (filehandle)
1950            {
1951                    fprintf(filehandle,"P5\n\n");           //
1952                    fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height);
1953                    fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle);
1954                    fclose(filehandle);
1955                    }
1956            }
1957    */

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.76.2.11

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