[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

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

Legend:
Removed from v.118  
changed lines
  Added in v.354

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