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

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

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

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

Legend:
Removed from v.87  
changed lines
  Added in v.658

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